Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8939411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:49:05+00:00 2026-06-15T10:49:05+00:00

I know that let rec is used when I want recursive . For example,

  • 0

I know that let rec is used when I want recursive.

For example,

let rec power i x = if i = 0 then 1.0 else x *. (power (i-1) x);;

Ok, I understand that.


But how about this one:

let x y = y + y in x 2?

Should I use rec inside?

I think I should, because it has x 2 inside, loading itself, but it seems it is fine with compiler.

So when I should use let rec and shouldn’t?


Also, what is the difference between

let (-) x y = y - x in 1-2-3;;

and

let rec (-) x y = y - x in 1-2-3;;

Are they both legal?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:49:06+00:00Added an answer on June 15, 2026 at 10:49 am

    You need to understand the scoping rules of OCaml first.

    When you write let f XXX = YYY in ZZZ, if you use f in YYY then you need rec. In both cases (ie with or without rec),f will be defined in ZZZ.

    So:

    let x y = y + y in
    x 2
    

    is perfectly valid.

    For you second question: no it is not equivalent, if you try it on the toplevel, the second statement loop for ever and is equivalent to let rec loop x y = loop y x in (). To understand why it is looping for ever, you can understand the application of loop as an expansion where the identifier is replaced by its body. so:

    So loop body is function x y -> loop y x, which can be expanded to
    function x y -> (function a b -> loop b a) y x (I’ve renamed the parameter names to avoid ambiguity), which is equivalent to function x y -> loop x y when you apply the body and so on and so on. So this function never does anything, it just loops forever by trying to expand/apply its body and swapping its arguments.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's get this out of the way first: I know that SessionFactory is immutable
I want to add a feature to our application that let's a user know
Let's say I know two points that make up a line. I want to
I know that this sort of question has been asked here before, but still
I know that I am for responsible for this in the code, but in
I am trying to understand what this block of code is doing: let rec
Say I have a recursive function that I want to know how many times
I know that if I want to have a html field that let's the
Before I start, I should let you know that I'm hardly experienced in PHP.
I know there are tools out there that will let you see the content

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.