Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell Increment by One

Tags:

haskell

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!

like image 555
Justin Tyler Avatar asked Dec 01 '22 20:12

Justin Tyler


1 Answers

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.

like image 181
jdevelop Avatar answered Dec 04 '22 09:12

jdevelop