In their book "The Seasoned Schemer", Felleisen and Friedman introduce the try
function. According to http://community.schemewiki.org/?seasoned-schemer, this function can be defined as
(define-syntax try
(syntax-rules ()
((try var a . b)
(letcc success
(letcc var (success a)) . b))))
where letcc
is defined as
(define-syntax letcc
(syntax-rules ()
((letcc var body ...)
(call-with-current-continuation
(lambda (var) body ... )))))
Now, while I understand what try
does and how it can be used, I am having trouble to follow the formal definition of it. What exactly is the meaning of the dot in the application of letcc
to success
and (letcc var (success a)) . b
in the lines
(letcc success
(letcc var (success a)) . b)
of try
? Or maybe asked differently: Which part of the definition of try
establishes that try
is evaluated to b
if var
is called in a
?
Edit 1: Sorry, the definition of letcc
was incomplete. Added the missing first line.
Edit 2: The following code can be run in Racket.
(define-syntax letcc
(syntax-rules ()
((letcc var body ...)
(call-with-current-continuation
(lambda (var) body ... )))))
(define-syntax try
(syntax-rules ()
((try var a . b)
(letcc success
(letcc var (success a)) . b))))
(try var (+ 1 1) 4)
; output: 2
(try var (var '(2)) 4)
; output: 4