List Info

Thread: "ocaml_beginners"::[] if then else vs match




"ocaml_beginners"::[] if then else vs match
country flaguser name
United States
2007-03-21 07:34:53

Folks,

Is there any drawback to using match instead of "if then else"?

I have a sizeable chunk of code to run in the "then" clause and
rather than use a begin ... begin I would prefer to just use match.

Thanks, Joel

--
http://wagerlabs.com/

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] Re: if then else vs match
country flaguser name
United States
2007-03-22 22:29:01

There's really no difference.

"if test then exp1 else exp2"

is the same as

"match test with
true -> exp1
false -> exp2"

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] if then else vs match
country flaguser name
United Kingdom
2007-03-23 05:39:05

On Wed, Mar 21, 2007 at 12:34:53PM +0000, Joel Reymont wrote:
> Folks,
>
> Is there any drawback to using match instead of "if then else"?
>
> I have a sizeable chunk of code to run in the "then" clause and
> rather than use a begin ... begin I would prefer to just use match.

If it's really just 'if ... then ... else ...' versus a match clause
with two cases, then it's unlikely to make any difference.

If the match clause has many cases, then in theory it can be
optimised better, so for example:

match i with
| 0 -> ...
| 1 -> ...
(* ... *)
| 100 -> ...

is potentially better than a lot of 'if ... else if ... else if ...'.

Whether the compiler actual does the right optimisations is another
matter. Apparently with string matching it does not (according to a
recent mail on caml-list).

Note that instead of begin ... end, you can use parentheses. I prefer
them, but that's a matter of style.

if test then (
...
)

Rich.

--
Richard Jones
Red Hat

__._,_.___
.

__,_._,___
[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )