I’m starting learning Ocaml, using hickey book, and I’m stuck on Exercise 3.4, part 9
let x x = x + 1 in x 2
The result of the operation is 3, but I don’t understand why?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
When you write
let x x = ...you are defining a function calledxwhich binds the namexto its argument.Since you used
letinstead oflet rec, the function doesn’t know its own name, so as far as it knows, the onlyxworth knowing about is the one passed in as an argument.Therefore when you call the function with
x 2, it binds the value2to the namexand evaluatesx+1, getting3as the result.