Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is Reason's cons (::) operator?

The cons (::) operator is a fundamental part of 1) writing recursive list functions in OCaml and similar languages, and 2) pattern matching on lists. However, I can't find anything in Reason's documentation concerning cons, and in the REPL, this throws an error:

Reason # let myList = [2, 3, 4];
let myList : list int = [2, 3, 4]
Reason # 1 :: myList;
Error: Syntax error

Is there a replacement for the cons operator?

like image 595
jayelm Avatar asked May 18 '16 21:05

jayelm


1 Answers

Ah, it's aliased as the "immutable list append" operator in Reason's list of primitives:

OCaml:

1 :: 2 :: myList
1 :: 2 :: [3, 4, 5]

Reason:

[1, 2, ...myList]
[1, 2, ...[3, 4, 5]]

Oddly, at least in the current version (0.0.6) you can use both syntaxes when pattern matching:

let head = fun lst => switch lst {
  | [] => failwith "Empty list"
  | [hd, ...tl] => hd
};

let head = fun lst => switch lst {
  | [] => failwith "Empty list"
  | hd::tl => hd
};
like image 151
jayelm Avatar answered Oct 21 '22 19:10

jayelm