1

I am somewhat new to F#, and I came across some strange behaviour when I was working with some recursive functions. I have two different versions of it below:

Version 1:
This causes a stack overflow, though it seems that it shouldn't (at least to my noob eyes)

let rec iMake acc =
  match acc with
  | 10 -> 100
  | _ -> iMake acc+1

Version2:
This one works as I would expect it to.

let rec iMake acc =
  match acc with
  | 10 -> 100
  | _ -> iMake (acc+1)

The only difference is that version 2 puts the acc+1 expression into parenthesis. So my question is, why does the first version not work, but the second one does? Does this mean that I should put all of my function arguments into parenthesis to avoid this type of stuff in the future ?

1 Answer 1

3

Function call has higher precedence than binary operator +. So the first function actually works like:

let rec iMake acc =
    match acc with
    | 10 -> 100
    | _ -> (iMake acc)+1
Sign up to request clarification or add additional context in comments.

1 Comment

I see, so it is trying to add one to the result of (iMake acc), and that is where the stackoverflow is coming from. acc is effectively never incremeted, interesting.

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.