0

I am currently working through the Introduction to programming using SML book released back in 1999. I would like your help on how to declare a recursive function for exercise 1.3, which I have been stuck on for some time now.

1.3: Declare a recursive function f: int -> int, where f(ᶯ) = 1 + 2 + ... + (ᶯ - 1) + ᶯ for ᶯ ≥ 0

Hint: Use two clauses with 0 and ᶯ as argument patterns. State the recursion formula corresponding to the declaration.

If anyone can provide some guidance on this that would be great.

2
  • And what have you tried? Commented Jun 17, 2014 at 13:19
  • I have not written any code for this function yet because I am trying to understand what it does. Commented Jun 17, 2014 at 13:22

1 Answer 1

2

More literal hint:

The solution is supposed to look like this:

fun sum 0 = ... something ...
  | sum n = ... something else ...

You'll need to figure out

  1. What is sum 0?
  2. What information do you need in order to be able to calculate sum n?

If you're stuck, write down the calculation of f(1), f(2), f(3), and f(4) on a piece of paper and see if you can spot a pattern.

Sign up to request clarification or add additional context in comments.

1 Comment

I think this may have helped me. Here is what I have: fun f 0 = 0 | f n = n + f(n-1);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.