What is wrong with rs
definition in first where section?
rs定义首先在哪个部分有什么问题?
palindrome :: [a] -> [a]
palindrome xs = con xs rs
where con a b = rev (rev a []) b
rs = rev xs -- here
where rev [] rs = rs
rev (x:xs) rs = rev xs (x:rs)
I'm just learning Haskell but its syntax rules confuse me. The error message is
我只是在学习Haskell,但它的语法规则让我很困惑。错误消息是
[1 of 1] Compiling Main ( pelindrome.hs, interpreted )
pelindrome.hs:5:8: parse error on input `rs'
2 个解决方案
#1
Your indentation was wrong and i think you can only have one where
in there (i could be very well wrong. I'm not a haskell guy). There was also a argument missing for the call to rev
(an empty list):
你的缩进是错误的,我认为你只能在那里有一个(我可能非常错误。我不是一个哈克尔家伙)。对rev(空列表)的调用也缺少一个参数:
palindrome :: [a] -> [a]
palindrome xs = con xs rs
where con a b = rev (rev a []) b
rs = rev xs [] -- here
rev [] rs = rs
rev (x:xs) rs = rev xs (x:rs)
main = print (palindrome "hello")
Prints out:
"helloolleh"
I'm going to try to understand it now. Anyway, have fun!
我现在要尝试理解它。无论如何,玩得开心!
Edit: Makes perfect sense to me now. I think that's the right version. For Haskell indentation rules, read Haskell Indentation
编辑:现在对我来说很有意义。我认为这是正确的版本。对于Haskell缩进规则,请阅读Haskell缩进
#2
@litb: You can rewrite con in way
@litb:你可以改写con
palindrome :: [a] -> [a]
palindrome xs = con xs rs
where con [] b = b
con (x:xs) b = x:con xs b
rs = rev xs [] -- here
rev [] rs = rs
rev (x:xs) rs = rev xs (x:rs)
which is how ++ is implemented in prelude. My previous version is way how to write it in non lazy functional or logical languages in tail call fashion (e.g. Erlang).
这就是++在前奏中实现的方式。我之前的版本是如何以尾部调用方式(例如Erlang)以非惰性函数或逻辑语言编写它。
#1
Your indentation was wrong and i think you can only have one where
in there (i could be very well wrong. I'm not a haskell guy). There was also a argument missing for the call to rev
(an empty list):
你的缩进是错误的,我认为你只能在那里有一个(我可能非常错误。我不是一个哈克尔家伙)。对rev(空列表)的调用也缺少一个参数:
palindrome :: [a] -> [a]
palindrome xs = con xs rs
where con a b = rev (rev a []) b
rs = rev xs [] -- here
rev [] rs = rs
rev (x:xs) rs = rev xs (x:rs)
main = print (palindrome "hello")
Prints out:
"helloolleh"
I'm going to try to understand it now. Anyway, have fun!
我现在要尝试理解它。无论如何,玩得开心!
Edit: Makes perfect sense to me now. I think that's the right version. For Haskell indentation rules, read Haskell Indentation
编辑:现在对我来说很有意义。我认为这是正确的版本。对于Haskell缩进规则,请阅读Haskell缩进
#2
@litb: You can rewrite con in way
@litb:你可以改写con
palindrome :: [a] -> [a]
palindrome xs = con xs rs
where con [] b = b
con (x:xs) b = x:con xs b
rs = rev xs [] -- here
rev [] rs = rs
rev (x:xs) rs = rev xs (x:rs)
which is how ++ is implemented in prelude. My previous version is way how to write it in non lazy functional or logical languages in tail call fashion (e.g. Erlang).
这就是++在前奏中实现的方式。我之前的版本是如何以尾部调用方式(例如Erlang)以非惰性函数或逻辑语言编写它。