SICP 問題 4.40
こうゆうことでいいんだろうか?
def match( data, cond, &block) data.each {|x| block.call(x) if cond.call(x) } end data = [1,2,3,4,5] match( data, lambda {|x| x!=5 } ) {|baker| work = data - [baker] match( work, lambda {|x| x!=1 }) {|cooper| work -= [cooper] match( work, lambda {|x| x!=1 and x!=5 and (x - cooper).abs != 1}) {|fletcher| work -= [fletcher] match( work, lambda {|x| x > cooper }) {|miller| work -= [miller] match( work, lambda {|x| (x - fletcher).abs != 1 }) {|smith| p [baker, cooper, fletcher, miller, smith] } } } } }
1000回実行で0.594sec.総当りの1/100か.アルゴリズム万歳!