I wrote the following solution for problem 10 (99 questions) in Haskell :
{-- using dropWhile and takeWhile --}
enc :: (Eq a) => [a] -> [(Int, a)]
enc [] = []
enc (x:xs) = (length $ takeWhile (==x) (x:xs), x) : enc (dropWhile (==x) xs)
I wanted to rewrite the same solution, but this time using let syntax.
{-- using dropWhile and takeWhile / more readable --}
enc' :: (Eq a) => [a] -> [(Int, a)]
enc' [] = []
enc' (x:xs) = let num = length $ takeWhile (==x) (x:xs)
rem = dropWhile (==x) (x:xs)
in (num, x) : enc' rem
The second example does not work. Error:
*Main> :l Problem10.hs
Compiling Main ( Problem10.hs, interpreted )
Problem10.hs:16:38: parse error on input `='
Failed, modules loaded: none.
If line 16 is as follows: rem = dropWhile (==x) (x:xs)
Any suggestions?
LE: Yes, it was an indent problem. It seems that my editor (Notepad ++) should be set up a bit to avoid such problems.
source
share