Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 O'Caml Flexibility: Why You Should Check It Out
Index -> Programming, General Programming -> Functional Programming
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: 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,
   const int age,
   const int income;
   const bool 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);

   return 0;
}


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");;

print_info ~lname:"Smith" ~fname:"Bob" ~is_married:true ~income:100_000 ~age:43;;


Much better, isn't it?
Sponsor
Sponsor
Sponsor
sponsor
Naveg




PostPosted: 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




PostPosted: 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




PostPosted: 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.

Let's take a look at this in action.

code:
C:\>ocaml
        Objective Caml version 3.08.3

# at_exit (function _ -> print_endline "Bye bye!");;
- : unit = ()
# exit 0;;
Bye bye!

C:\>


We can also register multiple functoions this way. Keep in mind that the last function registered will be the first called.

code:
C:\>ocaml
        Objective Caml version 3.08.3

# at_exit (function _ -> print_endline "Bye bye!");;
- : unit = ()
# at_exit (function _ -> print_endline "All done?");;
- : unit = ()
# exit 0;;
All done?
Bye bye!

C:\>
Display posts from previous:   
   Index -> Programming, General Programming -> Functional Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 2  [ 19 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: