Conditionals/Program Control
Having seen the basic syntax for defining functions, we need to be able to write more complicated functions that actually do things.
If/Else Expressions
OCaml has the following syntax for if/else expressions. (The line breaks are not necessary, merely for visual aid given the lack of coloring)
if boolean |
if boolean |
Note that we can use some of the following operators which give us back booleans, >, <, >=, <=, =, ==.
Let's see this in action: We can write a program that takes in two ints and doubles the lowest one.
Let's see this in action: We can write a program that takes in two ints and doubles the lowest one.
let double_least : int -> int -> int = fun x y ->
if x < y
then 2 * x
else 2 * y
;;
We can do else if statements as well.
The following program doubles the lower argument unless they are equal, in which case it simply returns that value.
The following program doubles the lower argument unless they are equal, in which case it simply returns that value.
let double_least : int -> int -> int = fun x y ->
if x < y
then 2 * x
else if y < x
then 2 * y
else x
;;
Awesome! If/Then/Else expressions can be combined with Begin/End syntax. Begin/End syntax are simply replacements for parentheses. Because OCaml will return the first value following a branch, in order to get the proper results back, we need to use parentheses or begin/end. This is demonstrated as follows.
let double_least : int -> int -> int = fun x y -> |
let double_least : int -> int -> int = fun x y -> |
There are a couple of interesting details here. If we want OCaml to separately evaluate the printf and x, we separate the two expression with a single semicolon (;).
Loops
We'll cover recursion a little bit later on. Recursion is the method used in functional programming to do what loops do in iterative programming. It is not necessary to know how to loop iteratively in OCaml to cover the rest of the material, but since OCaml has the ability to do iterative-like programming, I'll cover it briefly here.
For Loops in OCaml are written as follows
For Loops in OCaml are written as follows
for variable = begin_value to end_value do
expression
done
The loop will increment our variable each time we complete the expression. For instance, if we want to print out a statement 10 times
for i = 1 to 10 do
Printf.printf "Loop number %d\n" i
done;;
Loop number 1
Loop number 2
...
...
...
Loop number 10
Which is equivalent to the following:
let i = 1 in
Printf.printf "Loop number %d\n" i;
let i = 2 in
Printf.printf "Loop number %d\n" i;
...
...
...
let i = 10 in
Printf.printf "Loop number %d\n" i
;;
Note that the expression inside the loop should return unit, otherwise OCaml will register a type error because loops return unit by implementation and thanks to strictly typed functions, we must also return units. As a result, loops are weak and ineffective due to the fact that they don't return values.
While Loops are written like such
While Loops are written like such
while boolean-condition do
expression
done
While loops seem ineffective because of the immutable principle of functional programming. We can never change the boolean-condition because we have functions that don't cause any side effects. That is, unless we use references, an anomaly. I won't be diving into that here, but just know that it is possible.
There's more information on conditionals/etc. here.
We've seen that loops are pretty weak and, as a result, they are not often used. Instead, we'll be covering recursion in a few sections, the powerful looping mechanism in functional programming. Before we do that, we have to cover one last conditional that does not have a good equivalent in languages like C: Matching.
We've seen that loops are pretty weak and, as a result, they are not often used. Instead, we'll be covering recursion in a few sections, the powerful looping mechanism in functional programming. Before we do that, we have to cover one last conditional that does not have a good equivalent in languages like C: Matching.