Let the syntax in Haskell

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.

+3
source share
2 answers

, , , , rem, , , let. , Haskell, Real World Haskell.

let, , :

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
+7

@templatetypedef, . rem = num =.

+4

Source: https://habr.com/ru/post/1784684/


All Articles