Lua/MoonScriptで遅延リスト
MoonScriptで遅延リストを実装した。
list = require 'list' import Function, Delay, List, Array, Sequence from list require 'std' local fib fib = Sequence\Table({1, 1})\appendDelay -> fib\zipWith op['+'], fib\tail! nat = Sequence\iterate (=> @ + 1), 0 rnd = Sequence\iterate (-> math.random 9) seven = Sequence\pure 7 print '# Fibonacci number' print fib print '# Natural number' print nat print '# Random number' print rnd print rnd print '# Sevens' print seven print '# Multiples of 7' print seven\zipWith op['*'], nat
出力
# Fibonacci number [1,1,2,3,5,8,13,21,34,55,...] # Natural number [1,2,3,4,5,6,7,8,9,10,...] # Random number [8,4,8,8,9,2,4,7,3,5,...] [8,4,8,8,9,2,4,7,3,5,...] # Sevens [7,7,7,7,7,7,7,7,7,7,...] # Multiples of 7 [7,14,21,28,35,42,49,56,63,70,...]
(0は自然数? ゴメンナサイ……)