As an attempt to learn more about call/cc
in general I saw How does the yin-yang puzzle work? question and this explaination about the yin-yang puzzle:
(let*
(
(yin (
(lambda (cc) (display #\@) cc)
(call/cc (lambda (c) c))
))
(yang (
(lambda (cc) (display #\*) cc)
(call/cc (lambda (c) c))
))
)
(yin yang)
)
I probably (or probably not) understood the yin-yang puzzle concept as of by now. But I found that scheme syntax is not particularly easy to understand, and upon searching I found that ruby have Continuation module. Since ruby syntax follows procedural style, I found that it's much easier to read ruby code than scheme code. Therefore I decided to translate the puzzle to ruby version (I'm both novice at scheme and ruby):
require "continuation"
yin = (lambda do |cc|
print "@"
return cc
end).call(callcc {|c| c})
yang = (lambda do |cc|
print "*"
return cc
end).call(callcc {|c| c})
yin.call(yang)
However this version prints out @*@***********...
(demo here) instead of @*@**@***@****@*****@**...
, which is not what I expected.
Is my ruby version correct? If it's not correct then I don't really know what's to do from here...