I teach myself Haskell, and the best way to learn any programming language is to use it. My current “exercise” is the realization of taking. Pseudocode:
take(0, list) = [] --empty list
take(n, list) = const(head(list), take(n-1, tail(list))
What I developed in Haskell:
myTake :: (Num a) => a -> [b] -> [b]
myTake 0 l = []
myTake n (l:ls) = l : myTake n-1 ls
This does not compile when I upload a file to GHCi. This is the error message I get:
Couldn't match expected type `[b]'
against inferred type `[b1] -> [b1]'
In the second argument of `(:)', namely `myTake n - 1 ls'
In the expression: l : myTake n - 1 ls
In the definition of `myTake':
myTake n (l : ls) = l : myTake n - 1 ls
My current Haskell resource is “Find out that you are a Haskell for the great good!” and I read the types section several times, trying to figure it out. Google was unusually useless. I think that I still do not quite understand how to print. Can someone explain what is going wrong?
source
share