5. イテレータ
• for文から呼び出す • まつもとゆきひろ著「Rubyist
for i:int in int$from_to(1,100) do のための他言語探訪 【第 2
... 回】 CLU」より
end – イテレータの定義の仕方は驚く
ほど Ruby に似ています。真似
したんだから当然です。元々
• ユーザ定義可能 Ruby のブロックは CLU のイテ
from_to=iter(first:int, last:int) レータに似たものを実現するた
yields(int) めにデザインされたからです。
n:int := first
while n <= last
yield(n)
n := n + 1
end
end from_to
6. 例外処理
• signal文で例外発生 • まつもとゆきひろ著「
Rubyist のための他言語
signal 例外名[(値, …)] 探訪 【第 2 回】 CLU」よ
り
• except文で例外捕捉 – Ruby が例外処理を持つ
のは CLU の影響だと思
例外を発生する文 います。
except when 例外1: 文…
when 例外2:文…
…
others: 文…
end
10. クラスタの例(int型のスタック)
intstack=cluster is pop=proc(r: cvt)
new, push, pop returns(int)
rep=array[int] signals (empty)
new=proc() returns(cvt) return(rep$remh(r))
return(rep$new()) except when
end new bounds:
push=proc(r: cvt, x: int) signal empty
rep$addh(r, x) end
end push end pop
end intstack
11. パラメータ機能の例
stack=cluster[t: type] is pop=proc(r: cvt)
new, push, pop returns(t)
rep=array[t] signals (empty)
new=proc() returns(cvt) return(rep$remh(r))
return(rep$new()) except when
end new bounds:
push=proc(r: cvt, x: t) signal empty
rep$addh(r, x) end
end push end pop
end stack
17. ncluを使ったプログラムの例
赤字は2バイト文字使用部分
start_up = proc()
標準出力:stream:= stream$primary_output()
for i:int in int$from_to(1, 5) do
標準出力!putl(”No. ” || i!unparse() ||
” Hello World!”)
end
end start_up