Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell parser error in where clause

Tags:

syntax

haskell

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'
like image 234
Hynek -Pichi- Vychodil Avatar asked Jan 05 '09 11:01

Hynek -Pichi- Vychodil


1 Answers

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

like image 181
Johannes Schaub - litb Avatar answered Sep 19 '22 10:09

Johannes Schaub - litb