merits of Lisp vs Python

Jon Harrop jon at ffconsultancy.com
Sun Dec 10 04:39:32 EST 2006


Paul Rubin wrote:
> Jon Harrop <jon at ffconsultancy.com> writes:
>> # cond 2
>>     [( = ) 1, "one";
>>      ( = ) 2, "two";
>>      ( = ) 3, "three"]
>>     "neither one, two nor three";;
>> - : string = "two"
> 
> I'm missing something.  Doesn't Ocaml have strict evaluation?

Yes.

> That means if you use function calls instead of string constants in those
> values, they all get called.

True.

> You haven't really done what cond does. 

Good point. How about this:

# let rec cond x rules default = match rules with
    | [] -> default
    | f :: t -> match f x with
        | Some e -> e
        | None -> cond x t default;;
val cond : 'a -> ('a -> 'b option) list -> 'b -> 'b = <fun>

# cond 2
    [(fun n -> if n=1 then Some "one" else None);
     (fun n -> if n=2 then Some "two" else None);
     (fun n -> if n=3 then Some "three" else None)]
    "neither one, two nor three";;
- : string = "two"

The 'Some "one"' is only called if its predicate matched. Anyway, you don't
need macros to write COND and you don't need COND if you have pattern
matching.

-- 
Dr Jon D Harrop, Flying Frog Consultancy
Objective CAML for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists/index.html?usenet



More information about the Python-list mailing list