Partial sum in Standard ML?

2019-03-06 14:48发布

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]);

1条回答
【Aperson】
2楼-- · 2019-03-06 15:24

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
查看更多
登录 后发表回答