| 値 |
1, 2.0, true, "text" |
整数、浮動小数、真偽値、文字列。 |
| 束縛 |
my x = 1 + 2 |
ローカルな名前を作る。 |
| 関数 |
my id x = x, \x -> x + 1 |
名前付き関数とラムダ。 |
| 条件分岐 |
if x { 1 } else 0 |
条件で値を選ぶ。 |
| パターン |
case xs: [] -> 0; [x,..xs] -> x |
リスト、enum、record を分岐できる。 |
| リスト |
[1, 2, 3], [..xs, x] |
リスト literal と spread。 |
| リスト関数 |
map(xs, f), filter(xs, pred), sort(xs) |
empty, singleton, cons, uncons, fold, rev, first, last もある。 |
| 加算 |
1 + 2, "a" + "b", [1] + [2] |
Add role。int、float、str、list で使える。 |
| 比較 |
x == y, x <= y |
Eq と Ord role。int と float が主な対象。 |
| 長さ |
[1, 2, 3].len, "abc".len, len xs |
Len role。list と str に実装されている。method と関数呼び出しのどちらでも使える。 |
| 範囲 |
1..3, 1..<3, ..3, 1.. |
range 値を作る。list/str index と組み合わせる。有限範囲は Fold でも使える。 |
| range fold |
(((0..<5).fold) 0) (\acc i -> acc + i) |
有限範囲を int の列としてたたむ。この例は 0 + 1 + 2 + 3 + 4。 |
| index |
xs[0], xs[1..3], s[0], s[1..3] |
Index role。xs.index 0 のような method 呼び出しもできる。 |
| opt |
opt::nil, opt::just x |
first, last, uncons, .once などが返す。 |
| 非決定 |
each [1, 2, 3] |
playground では std::undet::* を暗黙 import している。 |
| guard |
guard (x == 3) |
std::undet::guard は条件が false のとき fail() する。each と組み合わせて候補を絞る。 |
| 非決定 handler |
(each [1, 2] + each [4, 5]).list |
.list は全解、.logic は探索順、.once は最初の解。 |
| junction |
if all [1, 2, 3] < any [2, 3, 4]: 1 else: 0 |
any と all で比較式を分岐させる。 |
| record |
{ width = 1, height = 2 } |
pattern では { width = 1, ..rest } のように default/spread も使える。 |
| role |
role Display 'a: our a.show: str |
impl Display int: our x.show = ... のように method を足す。 |
| effect |
act io: our read: () -> int |
catch io::read(): io::read(), k -> k 1 のように捕まえる。 |
| for control |
for x in xs: if x == 2 { next } else () |
for は内部で std::flow::loop::for_in を使う。last は break、next は continue、redo は同じ要素をやり直す。 |
| sub return |
sub::sub: xs.fold nil: \_ x -> if f x { return x } else nil |
sub は return x を捕まえて値 x にする。探索や fold の途中脱出を値として扱うための handler。 |