[
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: | 2001-09-23 (19:40) |
From: | Florian Hars <florian@h...> |
Subject: | Re: [Caml-list] Haskell features in O'Caml |
Arturo Borquez schrieb am Sat, Sep 22, 2001 at 07:56:36AM -0700: > On Sat, 22 September 2001, Steven Murdoch wrote: > > The main one I would like is the type assertion facility of Haskell. > > For example, one might write: > > mul :: Int -> Int -> Int > > mul a b = a * b > > As Ocaml is strong typed function mul is resolved > to be an integer function (denoted by operator * only > valid to integers). Haskel is strongly typed, too, and does the same type checking. The question was about type annotations, not type inference. The correct answer might have been a reference to the module system. You can specify the type of a function either in a separate *.mli-file or in an explicit module declaration: # module M : sig val mul : int -> int -> int end = struct let mul a b = a * b end;; module M : sig val mul : int -> int -> int end # M.mul 2 3;; - : int = 6 # module N : sig val mul : int -> int -> int end = struct let mul a b = a *. b (* Arithmetic is monomorphic - no type classes *) end;; Signature mismatch: Modules do not match: [...] The other question was about infix notation: > > For example if max gives the maximum of two arguments > > it can be applied normally, i.e. "max 2 3" will return 3, but "2 > > `max` 3" will also return 3. This is missing in Ocaml (some regard this as an asset, don't ask me why). > > Also if a function is named using operator symbols it can be used as > > an operator, e.g if &&& is defined as: > Ocaml: > # let ( &&& ) x y = if x >= y then x else y;; But you cannot change the fixity, it is defined by the first char of the operator, ie. ++, +*/- and +@%&!- all have the same precedence and associativity as +. Of course, these are syntax issues, and you can change the syntax using camlp4: # EXTEND expr: AFTER "apply" [[ f=expr; "{"; "["; g=expr; "]"; "}"; h=expr -> <:expr< $g$ $f$ $h$ >> ]]; END;; - : unit = () # let mul a b = a * b;; val mul : int -> int -> int = <fun> # 3 {[mul]} 4;; - : int = 12 # 3 {[fun a b -> a * a + 2 * a * b + b * b]} 4;; - : int = 49 > > This can lead to some very easy to read programs Yes. And messing around with delimeters may introduce subtle bugs elsewhere... [[f=expr; "<|"; g=LIDENT; "|>"; h=expr -> <:expr< $lid:g$ $f$ $h$ >> ]]; might be better if you do not want to write applications of anonymous functions in infix notation. Yours, Florian. -- #!/bin/sh - set - `type -p $0` 'tr [a-m][n-z][A-M][N-Z]@/ [n-z][a-m][N-Z][A-M]/@' fu '$UBZ\ R@.fvtangher' echo;while [ "$5" != "" ];do shift;done;$4 "gbhpu $3;znvy s/unef\ .qr<$3&&frq -a -rc "`$4 " $0"|$1`">$3;rpub 'Jr ner Fvt bs Obet.'"|$1|`$4 $2|$1` ------------------- 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