listfunctional-programmingsumpartialsml

Partial sum in Standard ML?


Im new to functional programming and I have an assignment to compute partial sum of a list. E.g. - psum [1,1,1,1,1]; val it = [1,2,3,4,5] : int list

Here is the my code so far. However in function psum2[L] i dont know how to go through each value and add them up so I just print the list.

fun psum2(L) : int list = 
   if L=nil then []
   else L;

fun pSum(L) : int list = 
   psum2(L);

exception Empty_List;

psum([2,3,4]);

Solution

  • Your question is a little broad, but here's one way to sum a list. Perhaps you can adapt it to your purposes:

    fun sum [] = 0
      | sum (h::t) = h + sum t