1

I am currently working 99 haskell problems

I cannot understand why am I getting an error in this function :-

repli :: [a] -> Int -> [a]
repli xs n  = concatMap (take n . repeat) xs
Ada Xu
  • 953
  • 4
  • 14
  • 31

1 Answers1

5

If you are using the REPL, try

>>> let repli xs n = concatMap (take n . repeat) xs

Writing Haskell in the REPL (ake GHCi) is a bit different to writing it in a file. For one thing, variable bindings and function definitions have to be prefixed with let as in

>>> let a = 1
>>> let f x = x + a

For another, you generally have to enter definitions all on one line. You can separate separate definitions with a semicolon, like this

>>> let a = 1; b = 2

or you can use multi-line mode, like this

>>> :{
>>> let c = 3
>>>     d = 4
>>> :}

If you've learnt about monads (have you?) then you can imagine that everything you write in the REPL is part of a do block which is of type IO (), with the statements executed as you type them. So in a file you might write

main :: IO ()
main = do
  name <- getLine
  let greeting = "Hello " ++ name ++ "!"
  putStrLn greeting

whereas in the REPL you would write

>>> name <- getLine
Chris
>>> let greeting = "Hello " ++ name ++ "!"
>>> putStrLn greeting
Hello Chris!
>>> 
Chris Taylor
  • 46,912
  • 15
  • 110
  • 154