Subject: Re: syntax-case and data transformation
From: rpw3@rigden.engr.sgi.com (Rob Warnock)
Date: 5 Feb 2001 04:30:22 GMT
Newsgroups: comp.lang.scheme
Message-ID: <95la8u$1q04l$1@fido.engr.sgi.com>
Keisuke Nishida  <kxn30@po.cwru.edu> wrote:
+---------------
| I would like to transform a list, not program, using syntax-case.
| For example, I need a function like this:
|   (transform '(+ 1 2 3))  =>  (add (add 1 2) 3)
| This rule can be written in terms of syntax-case as follows:
...[omitted...]
| However, since I am not going to define a syntax of program,
| I don't want to do this.  Instead, I want to define a set of
| transformation rules and apply it to given data.
+---------------

Look at the pattern matching system by Andrew Wright and Bruce Duba
<URL:http://www.cs.rice.edu/CS/PLT/packages/doc/match/index.htm>.
[Note: If you have a DrScheme/MrEd/MzScheme distribution, it should
already be there in ".../plt/collects/mzlib/match.ss". Load it with
(require-library "match.ss" "mzlib").]

Using the "match" macro instead of "match-lambda" should do what
you want, that is, produce values instead of code:

	> (define (transform form)
	    (match form
	      (('+ e1 e2) `(add ,e1 ,e2))
	      (('+ e1 e2 e3 ...) (transform `(+ (add ,e1 ,e2) ,@e3)))
	      ;; ...other patterns go here...
	      (else else))) ; catch-all identity [if you want that]
	> (transform '(+ 1 2))
	(add 1 2)
	> (transform '(+ 1 2 3))
	(add (add 1 2) 3)
	> (transform '(+ 1 2 3 4))
	(add (add (add 1 2) 3) 4)
	> (transform '(- 1 2 3 4))
	(- 1 2 3 4)
	> 

Other pattern matchers (and an earlier version of this one) can be found
at <URL:http://www.cs.indiana.edu/scheme-repository/code.match.html>.


-Rob

-----
Rob Warnock, 31-2-510		rpw3@sgi.com
SGI Network Engineering		http://reality.sgi.com/rpw3/
1600 Amphitheatre Pkwy.		Phone: 650-933-1673
Mountain View, CA  94043	PP-ASEL-IA

Newsgroups: comp.lang.scheme
Subject: Re: syntax-case and data transformation
Summary: 
Expires: 
References: <m3d7cy85ul.wl@kei.cwru.edu>
Sender: 
Followup-To: 
Distribution: 
Organization: Silicon Graphics Inc., Mountain View, CA
Keywords: 
Cc: 

In article <m3d7cy85ul.wl@kei.cwru.edu>,
Keisuke Nishida  <kxn30@po.cwru.edu> wrote:
>I would like to transform a list, not program, using syntax-case.
>For example, I need a function like this:
>
>  (transform '(+ 1 2 3))  =>  (add (add 1 2) 3)
>
>This rule can be written in terms of syntax-case as follows:
>
>  (define-syntax +
>    (lambda (x)
>      (syntax-case x ()
>        ((_ e1 e2) (syntax (add e1 e2)))
>        ((_ e1 e2 e3 ...) (syntax (+ (+ e1 e2) e3 ...))))))
>
>However, since I am not going to define a syntax of program,
>I don't want to do this.  Instead, I want to define a set of
>transformation rules and apply it to given data.
>
>Is there an easy way of doing this?
>
>Thanks,
>Keisuke Nishida
>