0
votes

So I have this function here, and I cant seem to understand why it wont work.

let rec recSum n = 
  if n <= 0 then
   0
  else
   recSum n*(n+1)/2
 recSum 4

I dont get an error, it just crashes. Can anyone find the mistake? I have been starring at this for so long.

I need it to be recursive.

Alright so I changed it to:

let rec recSum n = 
 if n > 0 then
  recSum n*(n+1)/2
 else
  n
 n
recSum 4

Because as you guys pointed out, the n would only increase. Now I get the error ´FS0001: The type unit does not match int?

2
Can you clarify what you're trying to accomplish? As mentioned below this is infinite recursion so you'll need to implement a different formula, if we have more details about the requirements maybe we can help figure out a new formula. - EJoshuaS - Reinstate Monica
A similar process that terminates for all known inputs is the function at the heart of the Collatz conjecture: half-or-triple-plus-one. I'm not sure what the limit of your stack is, but if you can stack 1228 function calls, you can reach 0 with any input up to 100 billion. - Juan Tomas

2 Answers

5
votes

The problem is, n*(n+1)/2 results in constantly increasing values being passed in the recursion. There's no way ever to get n <= 0. The sequence starts:

4 -> 10 -> 55 -> etc...

The value passed to recSum will only ever increase.

1
votes

The terminating condition will never be met, so this is infinite recursion. (I'm actually surprised that there's not a Stack Overflow Exception). The terminating condition is when n = 0, but every recursive call just adds 1: n*(n+1)/2

Also, did you mean to include the multiplication as an argument to the next recursive call rather than something like

n * recSum (n+1)/2

(Please excuse me if the syntax isn't perfect, I'm not in front of an IDE).

Perhaps you meant to subtract instead of add?

As a bit of a digression, what you end up choosing as your terminating condition will depend on what you're trying to accomplish and what formula you're trying to implement. A lot of people think about "going down" from the current value to the terminating condition. For example, if you're talking about Fibonacci numbers, most people will think of this as simply being "the sum of the two previous Fibonacci numbers" (both of which are, in turn, the sum of the two previous Fibonacci numbers, all the way "down" to the terminating condition). This is perfectly correct; for example, it is perfectly true to define 5! as 5 * 4!.

You can also think of it as "working up" from the terminating condition. Think about it this way: if I asked you to tell me the value of 10!, you almost certainly wouldn't be able to tell me what the value was without using a calculator. However, what if I told you that 9! is 362,880? Well, then, you can obviously just multiply that by 10 to get 10!, so obviously the answer has to be 3,628,800. That being the case, what's 11!? Well, obviously, 11 * 3,628,800. So, if I give you a value, you can use that value to "generate" more values. Given the value of 9!, you don't need to do anything other than to multiply it by 10 to get 10!.

For that matter, actually, given your knowledge of the fact that 10! = 3,628,800, you could easily calculate 9! from that by dividing 3,628,800 by 10.

Either way, the point is the same: given any value in the "sequence," you have a rule that you can apply to those values to calculate more values in the sequence.

In that sense, you could actually call the terminating condition the "initial condition" of sorts I guess.

Hopefully that digression kind of makes sense, if not I'd be glad to clarify it some as needed.