Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Guards vs. conditionals
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Matt Gushee <mgushee@h...>
Subject: [Caml-list] Guards vs. conditionals
Hi, all--

I'm just curious about something. I've been noticing that in many cases
(if not always) a pattern match with guard expressions in OCaml is
equivalent to a conditional statement. E.g.:

  let foo =
    | i when i >= 500 -> true
    | i -> false
does the same thing as

  let foo i =
    if i >= 500 then true
    else false

And I wrote a pair of small programs, each of which invokes one of these
functions on a random integer 100,000 times. The executables, whether
byte-compiled or native, are almost exactly the same size and, according
to a round of informal tests with GNU time, run at the same speed. This
suggests that the compiled code is for all practical purposes the same.

So, my question is, is there any objective reason to prefer the
pattern-match version over the conditional, or vice versa? Or is it just
a matter of coding style?
Matt Gushee                 When a nation follows the Way,
Englewood, Colorado, USA    Horses bear manure through           its fields;   When a nation ignores the Way,
                            Horses bear soldiers through
                                its streets.
                            --Lao Tzu (Peter Merel, trans.)
To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: