Posted: Tue May 10, 2005 5:18 pm Post subject: (No subject)
A Huge Avantage
Ever have a function that takes multiple arguments, but then forget which order the arguments should be in? While O'Caml is not the only language to offer a way out of this quandry, it also a solution while maintaining the option for high performance code. Few other languages can claim the same.
So, let's take a look at just a few programs:
c++:
#include <iostream> #include <string>
void print_info( const std::string& first_name,
const std::string& last_name,
constint age,
constint income;
constbool is_married) {
std::cout
<< first_name << " " << last_name
<< " is " << age << " years old, makes $"
<< income << " a year, and "
<< (is_married ? " is married." : " is not married.")
<< std::endl;
}
int main() {
print_info("Bob", "Smith", 43, 100000, true);
return0;
}
And the naive O'Caml version.
code:
include Printf;;
let print_info fname lname age income is_married =
printf "%s %s is %d years old, makes $%d a year, and %s.\n"
fname lname age income
(if is_married then "is married" else "is not married");;
print_info "Bob" "Smith" 43 100_000 true;;
But this doesn't use labels at all, and we still have to remember the order of the arguments (or check documentation). Let's redo this with labels.
code:
include Printf;;
let print_info ~fname ~lname ~age ~income ~is_married =
printf "%s %s is %d years old, makes $%d a year, and %s.\n"
fname lname age income
(if is_married then "is married" else "is not married");;
Posted: Tue May 10, 2005 8:20 pm Post subject: (No subject)
so ocaml lets you specify with argument is which, while C++ does not, but rather just assumes whatever is in the first spot is the "first" argument etc?
wtd
Posted: Tue May 10, 2005 8:53 pm Post subject: (No subject)
Vladimir wrote:
so ocaml lets you specify with argument is which, while C++ does not, but rather just assumes whatever is in the first spot is the "first" argument etc?
Yes. Of course, O'Caml will let you not use labels, and rely on the position of the arguments, if you want to.
Going back to one of my usual examples, the name class, let's look at how this could remove a common source of confusion.
code:
class name ~first ~last =
object
val first = first
val last = last
method first = first
method last = last
method full_name = first ^ " " ^ last
end
Now, creating a name, I don't need to remember if the first name comes first, or the last name.
code:
new name "Bob" "Smith"
And
code:
new name ~first:"Bob" ~last:"Smith"
Are equivalent.
This is also handy for partial function application.
code:
let smith = new name ~last:"Smith"
let bob_smith = smith "Bob"
let alice_smith = smith "Alice"
wtd
Posted: Wed May 11, 2005 4:37 pm Post subject: (No subject)
A Fun Trick With Exiting Programs
The Pervasives module, included by default in every O'Caml program, and somewhat akin to Haskell's Prelude module, or Java's java.lang package, provides the handy function "at_exit".
This function takes as its argument a function which takes "unit" as an argument and returns unit. It then calls this function when the program exits.