list 如何在命令行 ghci 中对 Haskell 中的列表进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19082953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to sort a list in Haskell in command line ghci
提问by Iceandele
I am new to Haskell, and I want to make 1 function that will take two lists and merge then together, and then sort the combined list from smallest to biggest. this should be done in the command line without using modules.
我是 Haskell 的新手,我想制作 1 个函数,该函数将采用两个列表并将其合并在一起,然后将组合列表从最小到最大排序。这应该在命令行中完成而不使用模块。
This is what i currently have, I am having trouble getting the "sortList" function to work, and also I do not know how to combine these 3 lines into 1 function.
这就是我目前拥有的,我无法使“sortList”函数正常工作,而且我不知道如何将这 3 行合并为 1 个函数。
let combineList xs ys = xs++ys
let zs = combineList xs ys
let sortList (z:zs) = if (head zs) < z then (zs:z) else (z:(sortList zs))
采纳答案by Boris
It's a bit awkward to define a sorting function within the ghci. I thing the easiest way to do it would be to write the sorting function in a file, and then loading it into ghci. For instance, you could write this concise (though not in-place!) version of quicksort in a file called sort.hs
(taken from the HaskellWiki):
在ghci中定义排序函数有点尴尬。我认为最简单的方法是在文件中编写排序函数,然后将其加载到 ghci 中。例如,您可以在名为sort.hs
(取自HaskellWiki)的文件中编写这个简洁(尽管不是就地!)快速排序版本:
quicksort :: Ord a => [a] -> [a]
quicksort [] = []
quicksort (p:xs) = (quicksort lesser) ++ [p] ++ (quicksort greater)
where
lesser = filter (< p) xs
greater = filter (>= p) xs
and load it into ghci:
并将其加载到 ghci 中:
> :l sort.hs
If you really want to define the function in ghci, you can do something like this (from the Haskell user's guide):
如果你真的想在 ghci 中定义函数,你可以这样做(来自Haskell 用户指南):
> :{
> let { quicksort [] = []
> ; quicksort (p:xs) = (quicksort (filter (< p) xs)) ++ [p] ++ (quicksort (filter (>= p) xs))
> }
> :}
once this is defined, you can do
一旦定义,你就可以做
> let combineAndSort xs ys = quicksort (xs ++ ys)
As another answer already explained, it would of course be quicker to just import sort from Data.List
, but it is definitely a good exercise to do it manually.
正如另一个答案已经解释过的那样,只导入 sort from 当然会更快 Data.List
,但手动执行它绝对是一个很好的练习。
Your question suggests that you are a bit confused about the scope of variables in Haskell. In this line
您的问题表明您对 Haskell 中的变量范围有点困惑。在这一行
> let combineList xs ys = xs++ys
you introduce the variables xs
and ys
. Mentioning them to the left of the equals sign just means that combineList
takes two variables, and in the body of that function, you are going to refer to these variables as xs
and ys
. It doesn't introduce the names outside of the function, so the next line
你引入了变量xs
和ys
。在等号的左边提到它们只是意味着它combineList
需要两个变量,并且在该函数的主体中,您将把这些变量称为xs
and ys
。它不引入函数外的名称,所以下一行
> let zs = combineList xs ys
doesn't really make sense, because the names xs
and ys
are only valid within the scope of combineList
. To make zs
have a value, you need to give combineList
some concrete arguments, eg.:
没有真正意义,因为名称xs
和ys
仅在combineList
. 要使zs
具有值,您需要给出combineList
一些具体参数,例如:
> let zs = combineList [2,4,6] [1,3,5] --> [2,4,6,1,3,5]
But since the body of combineList
is so simple, it would actually be easier to just do:
但由于主体combineList
如此简单,实际上这样做会更容易:
> let zs = [2,4,6] ++ [1,3,5] --> [2,4,6,1,3,5]
The last line is
最后一行是
> let sortList (z:zs) = if (head zs) < z then (zs:z) else (z:(sortList zs))
I think this line has confused you a lot, because there are quite a lot of different errors here. The answer by ДМИТРИЙ МАЛИКОВ mentions most of them, I would encourage you to try understand each of the errors he mentions.
我认为这条线让你很困惑,因为这里有很多不同的错误。ДМИТРИЙ МАЛИКОВ 的答案提到了其中的大部分,我鼓励您尝试理解他提到的每个错误。
回答by ДМИТРИЙ МАЛИКОВ
How to sort list in ghci
:
如何对列表进行排序ghci
:
Prelude> :m + Data.List
Prelude Data.List> sort [1,4,2,0]
[0,1,2,4]
About your functions
关于您的职能
let combineList xs ys = xs++ys
What is a point to create another alias for append function? But if you're really wants one - it could be defined like let combineList = (++)
.
为 append 函数创建另一个别名有什么意义?但如果你真的想要一个 - 它可以定义为let combineList = (++)
.
let zs = combineList xs ys
It makes no sense because xs
and ys
are unknown outside of your combineList
.
这是没有意义的,因为xs
和ys
在您的combineList
.
let sortList (z:zs) = if (head zs) < z then (zs:z) else (z:(sort zs))
This definition is not valid because it doesn't cover and empty list case and (zs:z)
produces infinite type and sort
is not defined yet. And you can get head
of zs
just by another pattern matching. And maybe you don't wanna to make another recursive call in the then
part of if
statement. And finally I should admit that this sorting algorithm doesn't work at all.
此定义无效,因为它不涵盖空列表情况并(zs:z)
产生无限类型且sort
尚未定义。你可以得到head
的zs
另一个模式匹配而已。也许您不想在语句then
部分进行另一个递归调用if
。最后我应该承认这种排序算法根本不起作用。