you can also write either of:
- (1 >) 2
- (> 2) 1
+ (2 >) 1
+ (> 1) 2
In OCaml one has to write these out longhand:
- (fun y -> 1 > y) 2;;
- (fun x -> x > 2) 1;;
+ (fun y -> 2 > y) 1;;
+ (fun x -> x > 1) 2;;
- Also, in Haskell, there's a special syntax for using what are ordinarily prefix functions into infix operators:
+ Also, in Haskell, there's a special syntax for using what are ordinarily prefix functions as infix operators:
Prelude> elem 1 [1, 2]
True
* Some functions are predefined in Haskell but not in OCaml. Here are OCaml definitions for some common ones:
- let id x = x;;
- let const x _ = x;;
- let flip f x y = f y x;;
- let curry (f : ('a, 'b) -> 'c) = fun x y -> f (x, y);;
- let uncurry (f : 'a -> 'b -> 'c) = fun (x, y) -> f x y;;
- let null lst = lst = [];;
+ let id x = x;;
+ let const x _ = x;;
+ let flip f x y = f y x;;
+ let curry (f : ('a, 'b) -> 'c) = fun x y -> f (x, y);;
+ let uncurry (f : 'a -> 'b -> 'c) = fun (x, y) -> f x y;;
+ let null lst = lst = [];;
`fst` and `snd` (defined only on pairs) are provided in both languages. Haskell has `head` and `tail` for lists; these will raise an exception if applied to []. In OCaml the corresponding functions are `List.hd` and `List.tl`. Many other Haskell list functions like `length` are available in OCaml as `List.length`, but OCaml's standard libraries are leaner that Haskell's.