[racket] syntax, differently

From: Eduardo Cavazos (wayo.cavazos at gmail.com)
Date: Wed Aug 18 14:39:08 EDT 2010

On 08/18/2010 09:52 AM, Matthias Felleisen wrote:

> To make this available in Racket, you'd have to port his macros
> from R6RS Scheme to Racket. I suspect that this would be a minor
> task.

To anyone interested in porting the code to Racket: if the 
implementation looks kinda complex, it's partly because it does more 
than what is shown in the note. Going with the 'point' example, you also 
get an 'import-point' macro:

(define (distance-from-origin p)
   (import-point p)
   (sqrt (+ (* x x) (* y y))))

I.e. 'import-point' is like 'is-point' except that the fields are made 
available directly by their name.

There's also a way to define "methods" on the record. Let's say we want 
a 'neg' "method" which negates a point:

(define-record-type++ point
   (fields x y)
   (methods (neg point::neg)))

(define (point::neg p)
   (is-point p)
   (make-point (- p.x)
               (- p.y)))

  > (define p0 (make-point 10 20))
  > (is-point p0)
  > (p0.neg)
  #[point -10 -20]

To only implement what I show in the note (enough to get a 
'define-record-type' which can provide things like 'is-point') should be 
alot less code.

I also experimented with a 'class' macro which builds on the above:

     http://gist.github.com/358639

The Racket veterans here could code all this up blindfolded. I'm sending 
this note in case some folks who are new to Racket are interested in 
experimenting with these ideas using my code. I didn't want them to be 
put off by the apparent complexity of it.

Ed


Posted on the users mailing list.