Closed m-spitfire closed 1 year ago
The and
keyword also supports the recursive function.
Therefore,
let rec f1 x =
if x == 0 then x
else if x > 0 then f1 (x-1)
else f3 x
and f2 x =
if x < 0 then f2 (x+1)
else if x == 0 then 0
else f3 x
and f3 x =
if x > 0 then f1 x
else if x < 0 then f2 x
else x
will work well.
Oh thanks for the quick reply!! I was adding rec
after and
and getting error... Seems interesting that Ocaml requires rec
after let-style functions, and not when used with and
. Do you think this kind of style is okay? Is using more than one and common?
Hi. Keyword and
in let-definition is used to define mutually recursive values and types.
Name: Murad Bashirov
Hello! When I was doing homework 0-2, I faced a problem with ocaml language. Because the language symbols are defined and available from top to bottom, the function on the top cannot call function on the bottom. And this I needed this while doing calculator problem. I found that when I deal with 2 functions I can use
and
keyword such (I use:
showing that this function calls listed functions in their body:But I have a bit more complicated situation:
In this case I cannot arrange functions and use
and
keyword to callf3
from both functions and callf1
andf2
fromf3
. I am not sure what to do.An example for second case(I know this looks silly):
ocamlc x.ml
gives