1

私は( + )パーベイシブ整数加算演算子を参照するために使用することに慣れていますが、これは機能しません( :: ):

        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
        ...

語彙の慣習は言う

infix-symbol  ::=  (= ∣  < ∣  > ∣  @ ∣  ^ ∣  | ∣  & ∣  + ∣  - ∣  * ∣  / ∣  $ ∣  %) { operator-char }

正常に動作しますが、中置演算子として両方::を除外しているようです。:=( := )


::のオペレーターとしてのステータスは?

リストの先頭に追加する演算子に便利なハンドルはあり(fun el ls -> el::ls)ますか、それともできる最善の方法はありますか?

4

3 に答える 3

2

:: 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

于 2013-10-30T18:50:07.643 に答える