This is the mail archive of the kawa@sources.redhat.com mailing list for the Kawa project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Spec of syntax-case


I use this macro to match and bind simple structures; hope it helps.

(define-syntax match
  (syntax-rules (cons list vector else)
    ((match exp
	    (() form0 form1 ...)
	    match0 ...)
     (if (null? exp) (begin form0 form1 ...)
	 (match exp match0 ...)))
    ((match exp
	    ((cons head tail) form0 form1 ...)
	    match0 ...)
     (if (pair? exp) (let ((head (car exp))(tail (cdr exp))) form0 form1
...)
	 (match exp match0 ...)))
    ((match exp
	    ((list elt0 elt1 ...) form0 form1 ...)
	    match0 ...)
     (if (list? exp) (apply (lambda (elt0 elt1 ...) form0 form1 ...)
exp) 
	 (match exp match0 ...)))
    ((match exp
	    ((vector elt0 elt1 ...) form0 form1 ...)
	    match0 ...)
     (if (vector? exp) (apply (lambda (elt0 elt1 ...) form0 form1 ...)
(vector->list exp))
	 (match exp match0 ...)))
    ((match exp
	    (else form0 form1 ...))
     (begin form0 form1 ...))
    ((match exp)
     exp)
    ))

; (let ((a '(a . b)))
;   (match a
; 	 ((cons c d) (and (eq? c 'a)(eq? d 'b)))
; 	 (else #f)))
;=>#t

; (let ((a '(a . b)))
;   (match a
; 	 ((cons c d) (and (eq? c 'a)(eq? d 'b)))
; 	 ))
;=>#t

; (let ((a '(1 2 3)))
;   (match a
; 	 ((list c d e)(and (= c 1)(= d 2)(= e 3)))
; 	 ((vector c d e) #f)
; 	 (else #f)))
;=>#t

; (let ((a (vector 1 2 3)))
;   (match a
; 	 ((list c d e) #f)
; 	 ((vector c d e)(and (= c 1)(= d 2)(= e 3)))
; 	 (else #f)))
;=>#t

; (let loop ((a '(1 2 3 4 5 6 7 8 9)) 
; 	   (result '()))
;   (match a
; 	 (() (reverse! result))
; 	 ((cons b c) (if (odd? b) (loop c (cons b result))
; 			 (loop c result)))
; 	 (else #f)))
;=>(1 3 5 7 9)

-- 
	(--cafe babe--) 
Marco Vezzoli	marco.vezzoli@st.com
CR&D Intranet Developement   STMicroelectronics
tel. +39 039 603 6852 fax. +39 039 603 5055


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]