srfi-26.scm 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. ;;; srfi-26.scm --- specializing parameters without currying.
  2. ;; Copyright (C) 2002, 2006, 2010 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 3 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-syntax cut
  21. (lambda (stx)
  22. (syntax-case stx ()
  23. ((cut slot0 slot1+ ...)
  24. (let loop ((slots #'(slot0 slot1+ ...))
  25. (params '())
  26. (args '()))
  27. (if (null? slots)
  28. #`(lambda #,(reverse params) #,(reverse args))
  29. (let ((s (car slots))
  30. (rest (cdr slots)))
  31. (with-syntax (((var) (generate-temporaries '(var))))
  32. (syntax-case s (<> <...>)
  33. (<>
  34. (loop rest (cons #'var params) (cons #'var args)))
  35. (<...>
  36. (if (pair? rest)
  37. (error "<...> not on the end of cut expression"))
  38. #`(lambda #,(append (reverse params) #'var)
  39. (apply #,@(reverse (cons #'var args)))))
  40. (else
  41. (loop rest params (cons s args))))))))))))
  42. (define-syntax cute
  43. (lambda (stx)
  44. (syntax-case stx ()
  45. ((cute slots ...)
  46. (let loop ((slots #'(slots ...))
  47. (bindings '())
  48. (arguments '()))
  49. (define (process-hole)
  50. (loop (cdr slots) bindings (cons (car slots) arguments)))
  51. (if (null? slots)
  52. #`(let #,bindings
  53. (cut #,@(reverse arguments)))
  54. (syntax-case (car slots) (<> <...>)
  55. (<> (process-hole))
  56. (<...> (process-hole))
  57. (expr
  58. (with-syntax (((t) (generate-temporaries '(t))))
  59. (loop (cdr slots)
  60. (cons #'(t expr) bindings)
  61. (cons #'t arguments)))))))))))