Where is Reason's cons (::) operator?
Asked Answered
D

1

5

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?

Didymium answered 18/5, 2016 at 21:58 Comment(4)
What is Reason? I've not heard of it, and can't find anything on Google.Selfaggrandizement
facebook.github.io/reasonDidymium
TIL. Thanks! I'd love to see a tag wiki about the language.Selfaggrandizement
@MattBall was just released a couple of days ago, so it's very new. This question was mainly just to get Reason on the SO table. I've submitted a (brief) draft of the tag wiki.Didymium
D
12

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
};
Didymium answered 18/5, 2016 at 22:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.