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 8208859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:31:00+00:00 2026-06-07T09:31:00+00:00

I was told in class that the following function is not tail recursive due

  • 0

I was told in class that the following function is not tail recursive due to the boolean operator being evaluated after the recursive call:

let rec exists p = function
    [] -> false
  | a::l -> p a || exists p l

But this doesn’t blow the stack on a ten-million size list, and what’s more, it is the implementation in the standard library. If it weren’t tail recursive, there would be no reason to use this form instead of the seemingly equivalent and clearly tail recursive

let rec exists p = function
    [] -> false
  | a::l -> if p a then true else exists p l

so it seems like the OCaml compiler is capable of optimizing boolean ops in simple cases like this to take advantage of tail recursion. But I noticed that if I switch the order of operands like so

let rec exists p = function
    [] -> false
  | a::l -> exists p l || p a

then the stack is indeed blown on 10m elements. So it looks like OCaml is only able to take advantage of this when the recursive call appears on the right, which makes me suspect that all the compiler does is replace the boolean op with the equivalent if expression. Can someone confirm or refute this?

  • 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-07T09:31:01+00:00Added an answer on June 7, 2026 at 9:31 am

    The person who told you this was wrong.

    In fact, || is not translated into an if/then/else right away, but preserved a bit through the intermediate language of the compiler, to easily enable two different transformations:

    1. as you said, a || b in expression position is translated into if a then true else b
    2. but a || b in test position, that is, if a || b then c else d is translated differently, into something like if a then goto c else if b then goto c else d, when goto c is a jump to the computation of c (just translating into if a then c else if b then c would duplicate the code of c). This optimization is more arcane and the users don’t need to be aware of it to reason about the performance of their programs.

    You can see for yourself in the sources of the compiler. The || primitive is represented as Psequor, and the files of interest are asmcomp/cmmgen.ml for native compilation ((1), (2)]), and bytecomp/bytegen.ml for the bytecode compilation (both aspects are handled at the same time, by instruction of the bytecode produced to use the result).

    A small point: you seem to say that OCaml is able to optimize a tail-call “on the right” because this case is “simple enough”, but not “on the left” because the compiler is not clever enough. If the call appears on the left, it is not a tail call, so it must not be optimized. This is not a question of being a “simple” tail call or not.

    Finally, if you want to check whether a tail is tail-call or not, you can use OCaml tools for that: compiling with the -annot option will produce an annotation file foo.annot (if your source was foo.ml) that has information about the types of program expressions and, for function calls, about whether they’re tail-calls or not. With the caml-mode in Emacs for example, the command M-x caml-types-show-call pointed about the exists after the || will confirm me that this is a “tail call”, while when called on p x it returns “stack call”.

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

Sidebar

Related Questions

My teacher told us that there is a default class in Java for binary
I was told that a function defined as static in one .c file is
Some people have told me that the following code is bad for HTML validation:
The following code does not compile in gcc: namespace One{ class A{ }; };
I am told I have to define an assignment operator for my Bullet class,
Previously on #ruby somebody told me that I do not need to use @name
Coming to Python from Java, I've been told that factories are not Pythonic. Thus,
In my team I've been told to write resource class like this style: class
Someone told me that openGL is for graphic only, and that it's very bad
I was told that using break and continue labels in an OOP language is

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.