The binding of x
in the list comprehension is hiding the variable x
from the pattern. Try this instead:
addFirstEl [] = []
addFirstEl (x1:xs) = [x1 + x2 | x2 <- xs]
Edit
In response to you comment
the first element still gets removed from the returned list
In (x1:xs)
,xs
is the remainder or tail
of the list. It is all the elements after x1
, which is the head
. If you want to add x1
to all the elements including itself, you could write
addFirstEl [] = []
addFirstEl (x1:xs) = [x1 + x2 | x2 <- (x1:xs)]
or in terms of head
addFirstEl [] = []
addFirstEl xs = [head xs + x | x <- xs]