1 Replies - 1629 Views - Last Post: 17 January 2013 - 05:01 PM

#1 hey92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 07-November 11

Partial sum in Standard ML?

Posted 17 January 2013 - 04:52 PM

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 my function just returns the list as it is.

 fun psum([])=[]
| ppsum2(x::L) = x::ppsum2(L);


Is This A Good Question/Topic? 0
  • +

Replies To: Partial sum in Standard ML?

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2298
  • View blog
  • Posts: 3,557
  • Joined: 21-June 11

Re: Partial sum in Standard ML?

Posted 17 January 2013 - 05:01 PM

The code you've posted won't compile. You name your function psum in the first clause of your function definition and ppsum2 in the second. All clauses in a function definition have to use the same function name.

As for calculating the partial sums: Your first step should be to define a helper function that takes the sum up to now as a second argument. That should help you get to a solution.

PS: Did you also ask this question or was that someone else with the same assignment?
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1