srfi-26.scm 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. ;;; srfi-26.scm --- specializing parameters without currying.
  2. ;; Copyright (C) 2002, 2006 Free Software Foundation, Inc.
  3. ;;
  4. ;; This library is free software; you can redistribute it and/or
  5. ;; modify it under the terms of the GNU Lesser General Public
  6. ;; License as published by the Free Software Foundation; either
  7. ;; version 2.1 of the License, or (at your option) any later version.
  8. ;;
  9. ;; This library is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; Lesser General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU Lesser General Public
  15. ;; License along with this library; if not, write to the Free Software
  16. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. (define-module (srfi srfi-26)
  18. :export (cut cute))
  19. (cond-expand-provide (current-module) '(srfi-26))
  20. (define-macro (cut slot . slots)
  21. (let loop ((slots (cons slot slots))
  22. (params '())
  23. (args '()))
  24. (if (null? slots)
  25. `(lambda ,(reverse! params) ,(reverse! args))
  26. (let ((s (car slots))
  27. (rest (cdr slots)))
  28. (case s
  29. ((<>)
  30. (let ((var (gensym)))
  31. (loop rest (cons var params) (cons var args))))
  32. ((<...>)
  33. (if (pair? rest)
  34. (error "<...> not on the end of cut expression"))
  35. (let ((var (gensym)))
  36. `(lambda ,(append! (reverse! params) var)
  37. (apply ,@(reverse! (cons var args))))))
  38. (else
  39. (loop rest params (cons s args))))))))
  40. (define-macro (cute . slots)
  41. (let ((temp (map (lambda (s) (and (not (memq s '(<> <...>))) (gensym)))
  42. slots)))
  43. `(let ,(delq! #f (map (lambda (t s) (and t (list t s))) temp slots))
  44. (cut ,@(map (lambda (t s) (or t s)) temp slots)))))