Reputation: 1008
I am attempting to write a function so that it can find the first two elements of a list, add them, and then place them at the head of the list. However I am running into unexpected errors when I try to do so.
The first function for the single element list works just fine, however the second one where the actual operation should occur does not. I thought it would take that the x off the list, then take the following element using head(xs), thus the first two off the list are available, add them and then put them in the front of the list as I wanted.
When I run it over command Plus [4,5,6]
I should get [9,6] However I get this error:
Couldn't match expected type `Int' with actual type `[Int]'
In the expression: xs
In the second argument of `(:)', namely `[xs]'
In the expression: (x + head (xs)) : [xs]
Failed, modules loaded: none.
If anyone can give me some insight, I'd really appreciate it!
Upvotes: 0
Views: 2121
Reputation: 1622
[xs]
is a lisf of .. list in the expression (x:xs) ~ ((x :: a) : (xs :: [a]))
and compiler say that:
Couldn't match expected type `Int' with actual type `[Int]'
Function looks like this:
command :: Operation -> [Int] -> [Int]
command Plus (x:y:xs) = x + y : xs
command Plus _ = error "Not enough to conduct a plus operation"
Upvotes: 6