(mark x) returns a pair of values. The car is x, the cdr is a (return) function. When you call (return y), the (mark x) call returns again, this time returning y instead of x. Here's an infinite loop example:
(let ((pair (mark 1)))
(let ((value (car pair)) (return (cdr pair))) ; unpack the pair
(print value)
(return (+ value 1)))) ; jump back to the (mark 1) call, this time returning value+1
Here's a Ruby implementation of mark:
def mark(initial)
callcc do |c|
ret = lambda{|value| c.call([value,ret])}
[initial, ret]
end
end
(I used ret instead of return because return is a Ruby keyword)
Example in Ruby:
value, ret = mark 1
puts value
ret.call(value + 1)
Bonus:
(mark x) returns a pair of values. The car is x, the cdr is a (return) function. When you call (return y), the (mark x) call returns again, this time returning y instead of x. Here's an infinite loop example: Here's a Ruby implementation of mark: (I used ret instead of return because return is a Ruby keyword)Example in Ruby: