Reputation: 120516
I'm used to using ( + )
to reference the pervasive integer addition operator, but this doesn't work for ( :: )
:
OCaml version 4.01.0 # (+);; - : int -> int -> int = <fun> # ( :: );; Error: Syntax error: operator expected. # ( := );; - : 'a ref -> 'a -> unit = <fun>
expr ::= ... | expr :: expr ... ∣ [ expr { ; expr } [;] ] ... | expr infix-op expr ...
and lexical conventions says
infix-symbol ::= (= ∣ < ∣ > ∣ @ ∣ ^ ∣ | ∣ & ∣ + ∣ - ∣ * ∣ / ∣ $ ∣ %) { operator-char }
which seems to exclude both ::
and :=
as infix operators, even though ( := )
works just fine.
What is ::
's status as an operator?
Is there a convenient handle for the list prepend operator or is (fun el ls -> el::ls)
the best one can do?
Upvotes: 1
Views: 152
Reputation: 122439
which seems to exclude both :: and := as infix operators, even though ( := ) works just fine.
You made a jump there. You cite
expr ::= ...
| expr infix-op expr
But then you did not look at infix-op
, which is defined as
infix-op ::= infix-symbol
∣ * ∣ + ∣ - ∣ -. ∣ = ∣ != ∣ < ∣ > ∣ or ∣ || ∣ & ∣ && ∣ :=
∣ mod ∣ land ∣ lor ∣ lxor ∣ lsl ∣ lsr ∣ asr
So there, :=
is a infix operator, along with other ones like mod
, etc. infix-symbol
is only for custom infix operators.
Upvotes: 2
Reputation: 5612
The cons operator ::
is a constructor, and it can't be applied as an infix operator.
Have a look at the pervasive module for a list of all the ones you can use like that.
Upvotes: 2
Reputation: 6379
::
is a base construction that can't be implemented with other constructions. :=
is an operator that can be implemented as let (:=) r v = r.contents <- v
. But I agree that this contradicts the lexical conventions described in the manual.
For your problem of using (::)
, the best you can do is to give it a short name if you want to use it multiple times. let cons h t = h :: t
Upvotes: 2