Warning: Creating default object from empty value in /home/patricknevindwyer/digilutionary.com/wp-includes/functions.php on line 292
Patrick Dwyer


Programming examples and work in the functional programming language OCaml

Echo :: OCaml

in News, Comparative Programming, OCaml by patrick

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/patricknevindwyer/digilutionary.com/wp-includes/functions-formatting.php on line 76

This program is part of the Comparative Programming :: Echo set of examples.

The OCaml (Object-Caml) echo example is in a functional language, far different in style from our other examples. The semantics and syntax of a functional language can seem strange comparaed to an example in a procedural (or object oriented) language like our other programs. Functional languages work with data and mathematically styled functions. The focus in a functional program is on the results of a function or group of functions, rather than state-controlled flow like in a procedural language.

Our echo program defines four functions to complete our command line echo. Two of the functions are recursive: reverse and print_list. The other two functions, main and trim, are non-recursive. The two recursive functions operate on a list provided as a parameter. In this case the list will be the command line arguments (converted from an OCaml Array to an unmutable list on line 18).

Printing our command line arguments falls to the print_list function. This function takes one parameter, a list (line 9), and has two possible operations. If the list is empty (line 11) then nothing happens, and the function returns. If this list is not empty it is broken into two pieces (line 12) the head of the list (the first item), and the remainder of the list (tail). The first item of the list is printed out with a trailing space, while the remainder of the list is passed to a recursive call to print_list. Each call to the print_list function prints the first element in the list, but the recursive call on line 12 happens before the print statement. This means that our list is effectively printed out in reverse order.

We want our command line arguments printed out in the correct order, so we need a way to reverse our list of arguments before passing them to the print function. Our reverse function takes a list as a parameter, and reverses the elements of the list (line 7).

The third function we defined (line 13) is designed to return a list with the leading element removed. The command line arguments to our OCaml program (line 18) include the name of the program, which we don’t want to echo. The trim function removes this element from our list.

The final function in our program is the main function, in which we convert the Array of command arguments (line 18) into a list, and pass the list to our print_list and reverse functions. The final two lines of our program invoke the main method, and once completed print a new line. Our entire program is just a series of functions all triggered through our main function; no loops, whiles or ifs.

01(* File echo.ml *)
02open Printf;;
04let rec reverse lst =
05 match lst with
06 [] -> []
07 | head :: tail -> reverse tail @ [head];;
09let rec print_list lst =
10 match lst with
11 [] -> []
12 | head :: tail -> printf "%s " head :: print_list tail;;
13let trim lst =
14 match lst with
15 [] -> []
16 | head :: tail -> tail;;
17let main () =
18 let a = Array.to_list(Sys.argv) in
19 print_list( reverse( trim(a) ) );;
21main ();;
[+-] Toggle Line Numbers

Program Source: echo.ml

Compiling and testing the OCaml example requires installation of an OCaml compiler.

Compiling the example

From the command line:

/usr/local/bin/ocamlc -o echo.ocaml echo.ml

Running the example

From the command line:

./echo.ocaml Hello World