This is probably a fairly obvious question, but I just can't figure it out.
I am trying to write a function that squares the even numbers in a list. When I try to run it, I am getting an error about my use of the even function. How can I fix this?
module SquareEvens where
squareEvens :: [Integer] -> [Integer]
squareEvens n = [ns * ns | ns <- n, even n]
The code works fine if you change even n
to even ns
:
squareEvens n = [ns * ns | ns <- n, even ns]
But note that the convention is to use the plural to name the list and the singular to name an element from that list. So swap n
and ns
to follow idiomatic Haskell usage:
squareEvens ns = [n * n | n <- ns, even n]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With