Computer Science Canada [O'Caml-tut] Imperative Syntax |
Author: | wtd [ Fri Jul 23, 2004 9:20 pm ] | ||||||||||||||||||||||||||||||||||
Post subject: | [O'Caml-tut] Imperative Syntax | ||||||||||||||||||||||||||||||||||
Along with its support for functiuonal programming techniques, O'Caml also offers syntax more familiar to those who have coded in imperative programming languages. If ... Else
This is considered by O'Caml to be a single expression, though in it we see three expressions.
This expression tests to see if 1 equals 0.
Just a literal value... a string.
Again, a literal value. Any single expression can follow "then". Thus, to get "else if" is as simple as:
If any of the string literals used in the above code needs to be multiple expressions, then "begin" and "end" can be used to group multiple expressions into a single expression. As an example, the below checs to see if a user input the correct password. It's extremely simple. Prompt and read a password once. If the input if correct, output that input. If it's not, prompt again and return the new input.
Loops O'Caml contains both a while loop and a for loop. Both are quite versatile. They evaluate a sequence of expressions until a condition becomes false. The only real requirement is that the seqence of expressions evaluates to "unit" (O'Caml's equivalent to "void"). I'll start out with something as simple as a count from 0 to 10.
Or we could go from 10 to 0.
And a while loop can do either, when combined with a reference. A reference is essential in this case because with normal O'Caml behavior, a name bound to a value can only be rebound to a new value, and rebinding within the loop wouldn't have any effect the next time around. The ! and := are the only strange looking bits of code in this case. ! dereferences a reference, and := assigns to a reference.
Using functional patterns, we could simply write out a list of ints with the following.
But perhaps we want to prove that it can be done in a procedural/imperative way.
Exceptions Exceptions are delightfully simple in O'Caml. I used exception handling in creating a recursive function which would read all lines of input from a file. The exception my function depends on is End_of_file, which is raised whenever you try to read beyond the end of a file. I pass a file into a function. The program then tries to read a line from that file. If it's successful, it adds to that line the result of calling the function again with the same file. If it raises the End_of_file exception, as it will once the file has been completely read, it returns an empty list. This ends the recursion.
Of course, it's equally easy to create our own exceptions.
This is about as simple an exception as anyone could hope for. Raising it is as smple as:
And catching it is as simple as:
Of course, this doesn't provide any information about what went wrong. To have an argument for an exception that can be set when the exception is raised, we simply change the above to:
Now, what if we want to handle different error messages differently?
|