Browse thread
[Caml-list] Function definition with multiple patterns in multiple equations
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Xavier Leroy <xavier.leroy@i...> |
| Subject: | Re: [Caml-list] Function definition with multiple patterns in multiple equations |
> Anyway, the quick-n-dirty method of doing multiple patterns like
> your example is to write:
>
> let f a b = match (a,b) with
> | (0, 0) -> 1
> | (_, _) -> 0
>
> But, this may very well have the overhead of actually building a
> pair.
Not in Objective Caml. There's a special optimization in both the
bytecode and native-code compilers to avoid the construction of the
pair in this case.
You can also decompose it into something like:
>
> let f = function
> | 0 -> (function
> | 0 -> 1
> | _ -> 0)
> | _ -> 0
>
> Which is, yes, rather unattractive for such a small example.
That should be:
let f = function
| 0 -> (function
| 0 -> 1
| _ -> 0)
| _ -> (function _ -> 0)
And it would be less efficient due to the extra function invocation
involved. (Assuming f is generally applied to two arguments; if you
do a lot of partial applications of f, the latter might be slightly
more efficient.)
- Xavier Leroy
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr