Trying to create a Haskell program that increments every number in a list by one.
module Add1List where
add1_list_comp :: [Integer] -> [Integer]
add1_list_comp [x] = [x + 1| x <- [x]]
It works when I call this add1_list_comp [3]
... it gives me [4]
But when I do add1_list_comp [3, 4, 5]
... it throws me an error saying
"non-exhaustive patterns in function add1_list_comp"
Any help would be much appreciated!
add1_list_comp = map succ
that simple
or, in your way
add1_list_comp xs = [x + 1| x <- xs]
the problem with your code is that
add1_list_comp [x]
does pattern match on list with single item, that's why it fails on list with several items.
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