In OCaml, if I have to write a function using many if-then-else, below is my stupid and ugly solution.
let foo () =
let a1 = ... in
if (a1) then
result1
else
let a2 = ... in
if (a2) then
result2
else
let a3 = ... in
if (a3) then
result3
else
let a4 = ... in
if (a4) then
result4
else
result5.
How to beautify the code above? I like C/C++ & Java style which use "return" to save indentation of next if-statement. Can I do the same thing with OCaml?
int foo () = {
bool a1 = ...;
if (a1)
return result1;
bool a2 = ...;
if (a2)
return result2;
bool a3 = ...;
if (a3)
return result3;
bool a4 = ...;
if (a4)
return result4;
return result5;
}
A nested if statement is an if statement placed inside another if statement. Nested if statements are often used when you must test a combination of conditions before deciding on the proper action.
It is possible to nest multiple IF functions within one Excel formula. You can nest up to 7 IF functions to create a complex IF THEN ELSE statement.
You can use an if statement without a else if it returns a value of type unit (basically when it only does something). However, in your case, you want to return a value of type int and as such the else branch is mandatory. It can nonetheless be shortened using else if statements.
A nested if statement is an if-else statement with another if statement as the if body or the else body. Here's an example: if ( num > 0 ) // Outer if if ( num < 10 ) // Inner if System.
If statements (actually, these are if expressions) OCaml has an if statement with two variations, and the obvious meaning: if boolean-condition then expression if boolean-condition then expression else other-expression. Unlike in the conventional languages you'll be used to, if statements are really expressions.
The primary piece of OCaml syntax is the expression. Just like programs in imperative languages are primarily built out of commands, programs in functional languages are primarily built out of expressions. Examples of expressions include 2+2 and increment 21. The OCaml manual has a complete definition of all the expressions in the language.
While loops in OCaml are written: As with for loops, there is no way provided by the language to break out of a while loop, except by throwing an exception, and this means that while loops have fairly limited use. Again, remember that functional programmers like recursion, and so while loops are second-class citizens in the language.
Type int: Integers. OCaml integers are written as usual: 1, 2, etc. The usual operators are available: +, -, *, /, and mod. The latter two are integer division and modulus: Exception: Division_by_zero.
There is no return
statement in OCaml, though you can emulate one with the help of exceptions:
exception Ret of t
let my_ret x = raise (Ret x)
let foo () =
try
let a1 = ... in
if a1 then my_ret result1;
let a2 = ... in
if a2 then my_ret result2;
...
with Ret x -> x
Another helpful solution would be to use lazy evaluation:
let foo () =
let a1 = lazy ...
and a2 = lazy ...
and a3 = lazy ...
in
match a1, a2, a3 with
| lazy true, _, _ -> result1
| _, lazy true, _ -> result2
| _, _, lazy true -> result3
| _, _, _ -> result4
This is one of the examples using lazy, there probably are more concise way of expressing your calculation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With