I was told in the 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 that doesn't hit the stack on the ten millionth list of sizes, and whatβs more, this is an implementation in the standard library. If it were not tail recursive, there would be no reason to use this form instead of the seemingly equivalent and explicitly tail recursive
let rec exists p = function
[] -> false
| a::l -> if p a then true else exists p l
therefore, it seems that the OCaml compiler is able to optimize logical operators in simple cases like this to take advantage of tail recursion. But I noticed that if I switch the order of the operands like this
let rec exists p = function
[] -> false
| a::l -> exists p l || p a
10- . , , OCaml , , , op if. - ?