I'm currently learning Haskell and I must say, I am having a terrible time.
I've been tasked with the exercise of creating a function, evens, which takes a value, x
, and returns a list of all even values from 0
to x
.
For example:
> evens 10 > [2,4,6,8,10]
I have been attempting to modify some example functions using list comprehension to achieve my goal however I have simply been riddled with errors, worse even I've tried so many things everything is one big confusing blur.
My last attempt went as follows:
evens :: int -> [int]
evens n = [x | x <- [0..n], filter even x]
It produced the error:
ex1.hs:9:29: error:
• Couldn't match expected type ‘Bool’ with actual type ‘[Integer]’
• In the expression:
filter even x
In a stmt of a list comprehension:
filter even x
In the expression:
[x | x <- [0 .. n], filter even x]
I do see its expecting a Boolean but quite frankly I don't understand where or why.
Any help would be largely appreciated. I have never had any experience with functional programming languages and I am having a hard time figuring out my errors in thought. I feel like I've overthought this one simple question to a point beyond belief.