Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write pretty multilevel nested if-then-else code in OCaml?

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;
}
like image 326
Trung Ta Avatar asked Feb 14 '15 12:02

Trung Ta


People also ask

What is nested if/then else?

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.

Can you string multiple if else statements together?

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.

Do you need an else in OCaml?

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.

What is nested if else explain with an example?

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.

How do you use if statements in OCaml?

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.

What is the syntax of OCaml?

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.

What is a while loop in OCaml?

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.

What are the types of integers in OCaml?

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.


1 Answers

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.

like image 73
PatJ Avatar answered Oct 04 '22 16:10

PatJ