Skip to content

Instantly share code, notes, and snippets.

type state = int;
type action =
| Inc
| IncBy int
| Dec
| DecBy int
| Reset;
let update state action =>
let rec nth l n =>
switch l {
| [] => None
| [h, ...t] => n === 0 ? Some h : nth t (n - 1)
};
let x = nth [0, 1, 2, 3] 999; /* option int: None */
switch x {
| Some i => print_int i
let nth l n =
if n < 0 then invalid_arg "List.nth" else
let rec nth_aux l n =
match l with
| [] -> failwith "nth"
| a::l -> if n = 0 then a else nth_aux l (n-1)
in nth_aux l n
let rec firstN n l =>
switch (n, l) {
| (0, _) => []
| (_, []) => []
| (n, [h, ...t]) => [h, ...firstN (n - 1) t]
};
firstN 2 [1, 2, 3, 4, 5]; /* => list int: [1, 2] */
let rec firstN n l =>
if (n === 0 || List.length l === 0) {
[]
} else {
let h = List.hd l;
let t = List.tl l;
List.append [h] (firstN (n - 1) t)
};
firstN 2 [1, 2, 3, 4, 5]; /* => list int: [1, 2] */
type result 'a = {mutable list: list 'a};
let firstN (n: int) (l: list 'a) :list 'a => {
let result: result 'a = {list: []};
let start = 0;
let stop = min (n - 1) (List.length l - 1);
for i in start to stop {
result.list = List.append result.list [List.nth l i]
};
result.list
This file has been truncated, but you can view the full file.
{"objects":{"id76q4bmj0blvfhe0":{"editingElement":{"__ref":"id7cwk5ej0bmw03v1u8"},"selectedParticularElement":null,"createPanelElements":[{"__ref":"Rectangle"},{"__ref":"Circle"},{"__ref":"Text"},{"__ref":"Image"},{"__ref":"idikabraj0blvfhe1"},{"__ref":"iddguqtej0bm17osdh"},{"__ref":"id23c44rj0bmdxmau0"},{"__ref":"idcbtfhxj0bmgqpg122"},{"__ref":"id0jctv6j0bmhhqg19r"},{"__ref":"id7cwk5ej0bmw03v1u8"},{"__ref":"iduy6mu2j0bofh8e77"},{"__ref":"idnma7i7j0bsiada86"}],"id":"id76q4bmj0blvfhe0","__proto":{"__ref":"Project"}},"id7cwk5ej0bmw03v1u8":{"_master":{"__ref":"Group"},"_head":{"__ref":"id7cwk5ej0bmw03v1u8"},"_variants":[{"__ref":"idskxudlj0bsihcz94"}],"_parent":null,"_children":[{"__ref":"idli3je4j0bmw05t1uf"},{"__ref":"id13ea62j0bmw05q1u9"},{"__ref":"idki6iwcj0bmw05r1uc"},{"__ref":"idcjkgjyj0bmwd2o1v6"},{"__ref":"id1ze4mzj0bmwht21v7"},{"__ref":"idejv7w8j0bmy9de1w3"},{"__ref":"idp0aacpj0bmyw5f1x8"},{"__ref":"id018i3ij0bn27kp1yv"},{"__ref":"ids49flwj0bncpo722d"},{"__ref":"idq0jznbj0bne4py2ns"},{"__ref":"idsw1e57j
Star = \*
Text = .+
Grammar = Star Star (Star Text Star)* Text Star Star
| Star (Star Star Text Star Star)* Text Star
const here = between(
zeroOrMore(not(string('<here>'))),
between(
string('<here>'),
zeroOrMore(not(string('</here>'))),
string('</here>')
),
always()
)
const here = between(
zeroOrMore(not(string('<here>'))),
between(
string('<here>'),
zeroOrMore(not(string('</here>'))),
string('</here>')
),
always()
)