Given a list of numbers , I want to create a new list where the element at index i
is the sum of all the i-1
elements before .
For example :
[1,4,6,9] -> [1,5,11,20]
I've written the following code :
fun sum nil = 0
| sum [x]=x
| sum(x::rest)=(x+hd(rest))::sum(rest);
but I got this :
- fun sum nil = 0
= | sum [x]=x
= | sum(x::rest)=(x+hd(rest))::sum(rest);
stdIn:306.16-306.39 Error: operator and operand don't agree [literal]
operator domain: int * int list
operand: int * int
in expression:
x + hd rest :: sum rest
I can see that the recursive rule of (x+hd(rest))::sum(rest);
is the reason for
the problem , but how can I fix it ?
Regards