You are thinking along the right lines, but there are a couple of issues.
The code you have shown will complain about a type error. This is because the type of map (+5)
is [a] -> [a]
, but you want just [a]
. To fix this you just need to change that line to
addFive a = map (+5) a
Look at the types and think about why this works.
You may also have tried this:
addFive :: [a] -> [a]
addFive [] = []
addFive = map(+5)
This gives you a different error because every version of a function like this needs to have the same number of arguments. Your null case takes one argument, so the other one has to take an argument too.
However you don't need the null case. map
already has that built in. So what you actually need is
addFive :: [a] -> [a]
addFive = map (+5)
By the way, one stylistic point. In Haskell we use a
, b
etc for type variables, but x
, xs
, y
etc for value variables. So the version above would be more idiomatic with
addFive xs = map (+5) xs