6

Now I'm leaning schemer by looking the book The Seasoned Schemer. I writed the code by racket, however when I using the try, the schemer didn't have this method or macro. And It reported expand: unbound identifier in module in: try. The code as the below: (in the page 89)

(define (remove-member-first* a lat)
   (try oh (rm a lat oh) lat))

I've search the racket documents, but didn't find smiliar function.

So who does know whether there are kinda function like the 'try'?

Matvey Aksenov
  • 3,802
  • 3
  • 23
  • 45
Colin Ji
  • 803
  • 7
  • 15

2 Answers2

14

I've just found someone who has already written all code snippets from the book The Seasoned Schemer in github.

And it is his answer: ( It is not non-hygienic and don't require other model)

(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))))

The link is https://github.com/viswanathgs/The-Seasoned-Schemer

Colin Ji
  • 803
  • 7
  • 15
  • 1
    Yay hygiene! Definitely the answer I prefer. – C. K. Young Aug 03 '12 at 12:14
  • 1
    Note, there is already a letcc in racket, but it is called `let/cc`, further, you should be able to get away with using `let/ec` instead, also using `define-syntax-rule`, you could define try as: `(define-syntax-rule (try var a b ...) (let/ec success (let/ec var (success a)) b ...))` – Marty Neal Aug 03 '12 at 15:33
4

You don't mention it, but I'm guessing that the book you're talking about is "The Seasoned Schemer". Use the following macro definitions for implementing try as defined in the book:

(require mzlib/defmacro)

(define-macro (letcc c . body)
  `(call/cc (lambda (,c) ,@body)))

(define-macro (try x a b)
  `(letcc *success*
     (letcc ,x
       (*success* ,a))
     ,b))
Óscar López
  • 232,561
  • 37
  • 312
  • 386