Reputation: 18642
I created this haskell function to remove odd numbers from a list and was trying in ghci. I keep getting the error below though I have enabled the multiline mode and used 4 spaces for indentation.
Prelude> :set +m
Prelude> let removeOdds nums =
Prelude| if null nums
Prelude| then []
Prelude| else
Prelude| if (mod (head nums)/2) == 0
Prelude| then (head nums) : (removeOdds(tail nums))
Prelude| else removeOdds(tail nums)
Prelude|
:11:5: parse error (possibly incorrect indentation or mismatched brackets)
I read this page about the common mistable newbie's will make and I changed my code as below
Prelude> let removeOdds nums =
Prelude| do if null nums
Prelude| then []
Prelude| else
Prelude| do if mod((head nums)/2) == 0
Prelude| then head nums: removeOdds(tail nums)
Prelude| else removeOdds(tail nums)
Prelude|
<interactive>:47:5:
parse error (possibly incorrect indentation or mismatched brackets)
Now I ended with a new error. It appears the indentation is a tough thing to get around in haskell.
Upvotes: 2
Views: 1087
Reputation: 1107
I use curly braces in GHCi when defining multiline functions. They allow you to just paste in a big block of Haskell code like you would usually write.
λ :{
| let
| -- type or paste here, e.g.,
| removeOdds :: Integral a => [a] -> [a]
| removeOdds nums =
| if null nums
| then []
| else -- ...
| :}
λ removeOdds [1,2,3,4]
[2,4]
Upvotes: 3
Reputation: 52029
Your if
statement needs to be indented at least one more space:
Prelude> let removeOdds nums =
Prelude| if null nums
Complete example:
Prelude> let removeOdds nums =
Prelude| if null nums
Prelude| then []
Prelude| else
Prelude| if (mod (head nums) 2) == 0
Prelude| then (head nums) : (removeOdds(tail nums))
Prelude| else removeOdds(tail nums)
Prelude|
Prelude>
Upvotes: 4
Reputation: 6249
It may also be good to note that when you aren't in the REPL and therefore don't require let
, then you don't need so much indentation:
removeOdds nums =
if null nums
then []
else
if (mod (head nums) 2) == 0
then (head nums) : (removeOdds(tail nums))
else removeOdds(tail nums)
Upvotes: 1