From: Alexander Schofield Date: 2002-02-07T10:23:16+09:00 Subject: Re: Call/CC and Ruby iterators. Johann Hibschman wrote: > > timsuth@ihug.co.nz (Tim Sutherland) writes: > > > No. Ruby's ``iterators'' just call a closure repeatedly. The 'callcc' method > > in Ruby is equivalent to Scheme's call/cc. > > > > Ruby > > [1, 2, 3].map { |x| x+1 } > > Scheme > > (map (lambda (x) (+ x 1)) (list 1 2 3)) > > No, this isn't quite true. Ruby iterators define "break" (to stop the > iteration) and "next" to skip to the next iteration. This is harder > to do with continuations. > > i.e. > > > 10.times {|i| puts i; next if i == 2; break if i == 4; puts 'foo'} > 0 > foo > 1 > foo > 2 > 3 > foo > 4 > nil > > How would you do this in Scheme? > > --Johann I can't say with scheme, but in CL one way would be: CL-USER 11 > (dotimes (i 10 i) (print i) (if (= i 2) (go next)) (if (= i 4) (return)) (print "foo") next) 0 "foo" 1 "foo" 2 3 "foo" 4 NIL dotimes is just one of many iteration macros. If you wanted you could easily roll your own, since all the CL do macros are in an implicit tagbody. -- Alexander Schofield