guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

srfi-26 (cut, cute)


From: Daniel Skarda
Subject: srfi-26 (cut, cute)
Date: 03 Oct 2002 22:45:55 +0200
User-agent: Gnus/5.0808 (Gnus v5.8.8) Emacs/20.7

Hello,

  during summer I browsed through srfi specification and as an excercice I
implemented srfi-26 (curry-which-is-not-curry :-). I hope somebody with
write access to CVS will like it and commit it.

  I found cut and cute macros very handy (they usually safe you a lot of
boring typing and screen space :-) so maybe they should be also part of
ice-9/boot-9.scm.

Have a nice day,
0.

ps: The reference implementation uses define-syntax instead of define-macro.
I chose define-macro since I prefere this way of writing macros and
(use-modules (ice-9 syncase)) doubles guile startup time (at least on my comp).

;--- srfi/srfi-26.scm: 
----------------------------------------------------------

(define-macro (cut slot . slots)
  (let loop ((slots     (cons slot slots))
             (params    '())
             (args      '()))
    (if (null? slots)
        `(lambda ,(reverse! params) ,(reverse! args))
      (let ((s    (car slots))
            (rest (cdr slots)))
        (case s
          ((<>)
           (let ((var (gensym)))
             (loop rest (cons var params) (cons var args))))
          ((<...>)
           (if (pair? rest)
               (error "<...> not on the end of cut expression"))
           (let ((var (gensym)))
             `(lambda ,(append! (reverse! params) var)
                (apply ,@(reverse! (cons var args))))))
          (else
           (loop rest params (cons s args))))))))

(define-macro (cute . slots)
  (let ((temp (map (lambda (s) (and (not (memq s '(<> <...>))) (gensym))) 
slots)))
    `(let ,(delq! #f (map (lambda (t s) (and t (list t s))) temp slots))
       (cut ,@(map (lambda (t s) (or t s)) temp slots)))))




reply via email to

[Prev in Thread] Current Thread [Next in Thread]