Does anyone know how I sort sequence as a lazy list as well as a eager list, sort [3,4,2,1]
, with this definition of sort
有谁知道我如何将序列排序为一个惰性列表以及一个急切的列表,排序[3,4,2,1],这个排序的定义
sort = foldr insert []
where
insert x [] = [x]
insert x (y:ys) =
if x<y
then x:y:ys
else y:insert x ys
2 个解决方案
#1
You have written an implementation of insertion sort, which, like all other deterministic comparison-based sorting algorithms, is strict. The first value returned by sort
is defined to be the minimum of the input list, which means that sort
has to inspect every element of the list to be sure it's found the minimum. You can't return a minimum value early because the actual minimum value might be right at the end of the list - you'll never know until you test them all.
您已经编写了插入排序的实现,与所有其他确定性的基于比较的排序算法一样,它是严格的。 sort返回的第一个值被定义为输入列表的最小值,这意味着sort必须检查列表的每个元素,以确保它找到最小值。您不能提前返回最小值,因为实际的最小值可能正好在列表的末尾 - 在您全部测试之前,您永远不会知道。
So sort
can't possibly be lazy. If the list is infinite, it'll never return a value. If any elements of the list are undefined
, it'll crash.
所以排序不可能是懒惰的。如果列表是无限的,它将永远不会返回值。如果列表中的任何元素未定义,它将崩溃。
In other words, your version of sort
(and, as I say, any deterministic comparison-based sort
) treats lazy lists just the same as eager lists (those that have been fully evaluated to normal form) - it will always evaluate every element of the input before it produces any output.
换句话说,您的排序版本(并且,正如我所说,任何基于确定性比较的排序)将惰性列表与急切列表(已经完全评估为正常形式的那些)相同 - 它将始终评估每个元素在输出产生任何输出之前的输入。
#2
Selection sort can be implemented both
non-strictly
incrementally and strictly.
选择排序可以非严格地递增和严格地实施。
Incremental selection sort
sort :: Ord a => [a] -> [a]
sort [] = []
sort (x:xs) = let (y, ys) = foldr select (x, []) xs
in y : sort ys
select :: Ord a => a -> (a, [a]) -> (a, [a])
select x (y, ys)
| x < y = (x, y:ys)
| otherwise = (y, x:ys)
Strict selection sort
sort :: Ord a => [a] -> [a]
sort [] = []
sort (x:xs) = let (y, ys) = foldr select (x, []) xs
in let zs = sort ys in seq zs (y:zs)
select :: Ord a => a -> (a, [a]) -> (a, [a])
select x (y, ys)
| x < y = (x, y:ys)
| otherwise = (y, x:ys)
However, selection sort always takes O(n^2)
time for all cases. For most cases, it would be better to use the sort
function exported by the Data.List
module.
但是,对于所有情况,选择排序总是花费O(n ^ 2)时间。对于大多数情况,最好使用Data.List模块导出的sort函数。
#1
You have written an implementation of insertion sort, which, like all other deterministic comparison-based sorting algorithms, is strict. The first value returned by sort
is defined to be the minimum of the input list, which means that sort
has to inspect every element of the list to be sure it's found the minimum. You can't return a minimum value early because the actual minimum value might be right at the end of the list - you'll never know until you test them all.
您已经编写了插入排序的实现,与所有其他确定性的基于比较的排序算法一样,它是严格的。 sort返回的第一个值被定义为输入列表的最小值,这意味着sort必须检查列表的每个元素,以确保它找到最小值。您不能提前返回最小值,因为实际的最小值可能正好在列表的末尾 - 在您全部测试之前,您永远不会知道。
So sort
can't possibly be lazy. If the list is infinite, it'll never return a value. If any elements of the list are undefined
, it'll crash.
所以排序不可能是懒惰的。如果列表是无限的,它将永远不会返回值。如果列表中的任何元素未定义,它将崩溃。
In other words, your version of sort
(and, as I say, any deterministic comparison-based sort
) treats lazy lists just the same as eager lists (those that have been fully evaluated to normal form) - it will always evaluate every element of the input before it produces any output.
换句话说,您的排序版本(并且,正如我所说,任何基于确定性比较的排序)将惰性列表与急切列表(已经完全评估为正常形式的那些)相同 - 它将始终评估每个元素在输出产生任何输出之前的输入。
#2
Selection sort can be implemented both
non-strictly
incrementally and strictly.
选择排序可以非严格地递增和严格地实施。
Incremental selection sort
sort :: Ord a => [a] -> [a]
sort [] = []
sort (x:xs) = let (y, ys) = foldr select (x, []) xs
in y : sort ys
select :: Ord a => a -> (a, [a]) -> (a, [a])
select x (y, ys)
| x < y = (x, y:ys)
| otherwise = (y, x:ys)
Strict selection sort
sort :: Ord a => [a] -> [a]
sort [] = []
sort (x:xs) = let (y, ys) = foldr select (x, []) xs
in let zs = sort ys in seq zs (y:zs)
select :: Ord a => a -> (a, [a]) -> (a, [a])
select x (y, ys)
| x < y = (x, y:ys)
| otherwise = (y, x:ys)
However, selection sort always takes O(n^2)
time for all cases. For most cases, it would be better to use the sort
function exported by the Data.List
module.
但是,对于所有情况,选择排序总是花费O(n ^ 2)时间。对于大多数情况,最好使用Data.List模块导出的sort函数。