制御構文
制御構文は、特に断らない限り式として使える。
if
if x > 0: "positive" else: "non-positive"
if cond:
a
else:
b
if cond { a } else { b }if の条件は bool である。else がない場合は statement-like になり、then branch は効果のために評価され、値は捨てられ、式全体は () を返す。
case
case value:
0 -> "zero"
n if n < 0 -> "negative"
_ -> "other"case arm は上から順に試される。guard は pattern の後ろに if を書く。
catch
catch action:
console::read(), k -> k "42"
value -> valueoperation arm は operation の payload と continuation k を受け取る。k value を呼ぶと計算を再開する。value arm は通常終了を処理する。
for
for x in 0..10: // 11 回反復: 0..10 は閉区間 (半開は 0..<10)
say xfor x in xs: は Fold を実装する値を走査する。body は関数へ lower され、plain な for expression は () を返す。
loop control は prelude から入る。
for x in 0..:
if x == 10: lastlast、next、redo は、現在の loop から抜ける、次の反復へ進む、反復をやり直すための操作である。
ラベル
for 'outer x in 0..:
for y in 0..:
if y == 3: last 'outerlabelled loop は label 値を body に渡す。last 'outer、next 'outer、redo 'outer は、その label の loop を対象にする。
sub と return
sub:
for x in 0..:
if x == 5: return x
0sub: は早期 return scope を作る。return value は直近の sub: から抜ける。nullfix の return は () を返す。
labelled sub もある。
sub 'done:
'done.return 42
0sub、return、last、next、redo は標準ライブラリ / prelude の surface であり、parser 専用 keyword ではない。
Block と Lambda
{
my x = 1
x + 1
}
\x -> x + 1
\x y -> x + yblock は statement を順に評価し、最後の式を返す。lambda は \ で始まり、複数引数は curried function として扱われる。