haskell->打印排序列表 - 错误:输入上的解析错误‘打印’

发布于 2025-02-06 03:32:49 字数 701 浏览 1 评论 0原文

我试图在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

So尛奶瓶 2025-02-13 03:32:51

原因是LET的相同级别上被缩进的,而不是AccompellastNames函数。

您可以用:

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)

但是您使它变得太复杂了,可以与 on ::(b - &gt; b - &gt; c) - &gt;上(a - &gt; b) - &gt; a - &gt; a - &gt; C

import Data.Function(on)

main = print (sortBy (compare `on` snd) names)

或使用 sorton :: ord b =&gt; (a - &gt; b) - &gt; [a] - &gt; [a]

import Data.List(sortOn)

main = print (sortOn snd names)

The reason is the where that is indented at the same level of the let, and not more indented than the compareLastNames function.

You can indent this with:

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)

But you make this too complicated, you can work with on :: (b -> b -> c) -> (a -> b) -> a -> a -> c:

import Data.Function(on)

main = print (sortBy (compare `on` snd) names)

or use sortOn :: Ord b => (a -> b) -> [a] -> [a]:

import Data.List(sortOn)

main = print (sortOn snd names)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文