gpt4 book ai didi

haskell - Haskell : Syntax error in input (unexpected `=' )

转载 作者:行者123 更新时间:2023-12-03 08:15:21 27 4
gpt4 key购买 nike

我正在尝试实现一个比较2个列表以查看它们是否相同的功能。语法对我来说看起来不错:

compare :: String -> String -> Bool

compare [] [] = True -- error here
compare (x,xs) (y,ys) = if x == y
then compare xs ys
else False

但我在上面标记的行中不断收到此错误:

Syntax error in input (unexpected `=')



当我尝试用'->'替换'='时,它工作正常,但在下一行中给出了相同的错误。所以我做了同样的事情:
compare :: String -> String -> Bool

compare [] [] -> True
compare (x,xs) (y,ys) -> if x == y -- new error here
then compare xs ys
else False

但是我遇到了另一个错误:

Syntax error in type signature (unexpected keyword "if")



现在我真的不知道发生了什么。

最佳答案

您最初的第一个功能是正确的,除了您在模式上匹配错误。应该是这样的:

compare (x:xs) (y:ys)    -- Not compare (x,xs) (y,ys)

同样按照@ThreeFx的建议,正确格式化代码。最终它应该看起来像这样:
compare :: String -> String -> Bool
compare [] [] = True
compare (x:xs) (y:ys) = if x == y
then compare xs ys
else False

关于haskell - Haskell : Syntax error in input (unexpected `=' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27071190/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com