Valentin ROBERT wrote:
> I guess you can write it like:
> 
> let a = (if out then [o] else []) @ (if value then [v] else [])
>
> But it's not particularly more pleasant to the eye.
> Still it reduces the exponential explosion of the code, at a small additional 
> cost (the @), I believe.

Actually, it's possible that with more cases it might be faster - it's 
eliminating the allocation (at some point) of all the tuples needed for the 
match case, it potentially eliminates a lot of linear comparisons to find the 
correct match case (I don't think that the compiler would be able to optimise 
that to a hash-based or index-based lookup) and [@]'s running time is 
proportional to the length of its first argument only - which here is always a 
singleton or empty list. The only extra cost is in space - you allocate a cons 
cell for each item which is included in the list twice (once to put it in the 
singleton list and once to append the rest of the list to it).

If the actual scenario is lots more of these put together, you could profitably 
define a better special-case operator:

let (@@) a b = match a with [] -> b | [a] -> a::b | _ -> assert false

(a dirty - though still safe - solution with Obj.magic can reduce the [match] 
to an [if]...)


David


-- 
Caml-list mailing list.  Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

Reply via email to