Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any function like 'try' in Racket

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'?

like image 936
Colin Ji Avatar asked Aug 03 '12 02:08

Colin Ji


2 Answers

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

like image 88
Colin Ji Avatar answered Oct 20 '22 02:10

Colin Ji


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))
like image 29
Óscar López Avatar answered Oct 20 '22 04:10

Óscar López