Reputation: 99
The code as follows:
chop [n] lev = [n-1]
chop (n:m:xs) lev = n-1:lev +m:xs
what does n-1:lev +m:xs
mean?
I know m:xs
is list, how can a list add a int (n-1:lev)
?
Thanks you!
Upvotes: 1
Views: 150
Reputation: 74384
The (+)
associates more tightly than the (:)
does. If we write that function with some more parentheses it'd be
chop [n] lev = [n-1]
chop (n : m : xs) lev = (n-1) : (lev + m) : xs
So when the list has 2 or more elements chop
modifies the first two. If it has just one element then only that one is modified.
Upvotes: 11