Browse thread
what do I need to know to understand camlp4
[
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: | 2010-09-24 (07:35) |
From: | David House <dmhouse@g...> |
Subject: | Re: [Caml-list] what do I need to know to understand camlp4 |
On 24 September 2010 01:15, Elias Gabriel Amaral da Silva <tolkiendili@gmail.com> wrote: > [1] Pervasives should define it. In fact, even though ** is > right-associative, it looks like any user-defined operator is > left-associative by default. So it works like Haskell: > > # let ($) a b = a b;; > val ( $ ) : ('a -> 'b) -> 'a -> 'b = <fun> > # fun x y z -> x $ y $ z;; > - : ('a -> 'b -> 'c) -> 'a -> 'b -> 'c = <fun> > # let q = fun x y z -> x $ y $ z;; > val q : ('a -> 'b -> 'c) -> 'a -> 'b -> 'c = <fun> > # let q' = fun x y z -> (x $ y) $ z;; > val q' : ('a -> 'b -> 'c) -> 'a -> 'b -> 'c = <fun> > # let q'' = fun x y z -> x $ (y $ z);; > val q'' : ('a -> 'b) -> ('c -> 'a) -> 'c -> 'b = <fun> > # let q''' = fun x y z -> x $ y z;; > val q''' : ('a -> 'b) -> ('c -> 'a) -> 'c -> 'b = <fun> Incidentally, ($) in Haskell is right-associative; however the consensus in the Haskell community (in my experience) is that this is a mistake. If it were left-associative, you would lose the ability to say f $ g $ x, but this can be written f . g $ x anyway (dot is function composition (a -> b) -> (b -> c) -> a -> c, and does right-associate), but many things would require fewer parentheses, e.g. f (g x) (h y) can be written f $ g x $ h y. In fact, the strict function application operator ($!) *is* left-associative. See http://hackage.haskell.org/trac/haskell-prime/wiki/ChangeDollarAssociativity for more information. Also, you say "Pervasives should define it" -- the important thing about dollar in Haskell is that it has very low precedence, hence its ability to save parentheses. I didn't think OCaml allowed us to specify the operator precedence of the infix operators we define.