haskell->打印排序列表 - 错误:输入上的解析错误‘打印’
我试图在GHCI上运行此程序,在那里它以其姓氏的上升顺序将名称重新定位。但是,当我运行它时,我会得到此错误“错误:输入'print'''上的解析错误。
如果您能帮助我,我将非常感谢。谢谢![在此处输入图像说明] [1]
import Data.List
main :: IO ()
names = [("Tatsunori", "Ono"), ("Kishore", "Palanisamy"), ("Calder", "Hosgood"), ("Yiling", "Zhuang")]
main = do
let compareLastNames name1 name2 = if lastName1 > lastName2
then GT
else if lastName1 < lastName2
then LT
else EQ
where lastName1 = snd name1
lastName2 = snd name2
print (sortBy compareLastNames names)
I was trying to run this program on ghci, where it reorders the names in the ascending order of their last names. However, when I run it, I get this error " error: parse error on input ‘print’ ".
I would truly appreciate it if you can help me out with this. Thank you![enter image description here][1]
import Data.List
main :: IO ()
names = [("Tatsunori", "Ono"), ("Kishore", "Palanisamy"), ("Calder", "Hosgood"), ("Yiling", "Zhuang")]
main = do
let compareLastNames name1 name2 = if lastName1 > lastName2
then GT
else if lastName1 < lastName2
then LT
else EQ
where lastName1 = snd name1
lastName2 = snd name2
print (sortBy compareLastNames names)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
原因是
在
LET
的相同级别上被缩进的,而不是AccompellastNames
函数。您可以用:
但是您使它变得太复杂了,可以与
on ::(b - &gt; b - &gt; c) - &gt;上(a - &gt; b) - &gt; a - &gt; a - &gt; C
:或使用
sorton :: ord b =&gt; (a - &gt; b) - &gt; [a] - &gt; [a]
:The reason is the
where
that is indented at the same level of thelet
, and not more indented than thecompareLastNames
function.You can indent this with:
But you make this too complicated, you can work with
on :: (b -> b -> c) -> (a -> b) -> a -> a -> c
:or use
sortOn :: Ord b => (a -> b) -> [a] -> [a]
: