What is wrong with rs
definition in first where section?
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
[1 of 1] Compiling Main ( pelindrome.hs, interpreted )
pelindrome.hs:5:8: parse error on input `rs'
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):
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With