add (ice-9 curried definitions)
authorAndy Wingo <wingo@pobox.com>
Wed, 7 Apr 2010 22:36:53 +0000 (00:36 +0200)
committerAndy Wingo <wingo@pobox.com>
Wed, 7 Apr 2010 22:36:53 +0000 (00:36 +0200)
* module/Makefile.am:
* module/ice-9/curried-definitions.scm: New module, implementing
  SICM-style currying.

* test-suite/Makefile.am:
* test-suite/tests/curried-definitions.test: A test.

module/Makefile.am
module/ice-9/curried-definitions.scm [new file with mode: 0644]
test-suite/Makefile.am
test-suite/tests/curried-definitions.test [new file with mode: 0644]

index ca38524..16013b0 100644 (file)
@@ -179,6 +179,7 @@ ICE_9_SOURCES = \
   ice-9/calling.scm \
   ice-9/common-list.scm \
   ice-9/control.scm \
+  ice-9/curried-definitions.scm \
   ice-9/debug.scm \
   ice-9/debugger.scm \
   ice-9/documentation.scm \
diff --git a/module/ice-9/curried-definitions.scm b/module/ice-9/curried-definitions.scm
new file mode 100644 (file)
index 0000000..d7db1f0
--- /dev/null
@@ -0,0 +1,37 @@
+;;; Copyright (C) 2010  Free Software Foundation, Inc.
+;;;
+;;;; This library is free software; you can redistribute it and/or
+;;;; modify it under the terms of the GNU Lesser General Public
+;;;; License as published by the Free Software Foundation; either
+;;;; version 3 of the License, or (at your option) any later version.
+;;;; 
+;;;; This library is distributed in the hope that it will be useful,
+;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+;;;; Lesser General Public License for more details.
+;;;; 
+;;;; You should have received a copy of the GNU Lesser General Public
+;;;; License along with this library; if not, write to the Free Software
+;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+(define-module (ice-9 curried-definitions)
+  #:replace ((cdefine . define)
+             (cdefine* . define*)))
+
+(define-syntax cdefine
+  (syntax-rules ()
+    ((_ ((head . tail) . rest) body body* ...)
+     (cdefine (head . tail)
+       (lambda rest body body* ...)))
+    ((_ (head . rest) body body* ...)
+     (define head
+       (lambda rest body body* ...)))))
+
+(define-syntax cdefine*
+  (syntax-rules ()
+    ((_ ((head . tail) . rest) body body* ...)
+     (cdefine* (head . tail)
+       (lambda* rest body body* ...)))
+    ((_ (head . rest) body body* ...)
+     (define* head
+       (lambda* rest body body* ...)))))
index bc166c4..006b131 100644 (file)
@@ -35,6 +35,7 @@ SCM_TESTS = tests/00-initial-env.test         \
            tests/common-list.test              \
            tests/control.test                  \
            tests/continuations.test            \
+           tests/curried-definitions.test      \
            tests/ecmascript.test               \
            tests/elisp.test                    \
            tests/elisp-compiler.test           \
diff --git a/test-suite/tests/curried-definitions.test b/test-suite/tests/curried-definitions.test
new file mode 100644 (file)
index 0000000..650288f
--- /dev/null
@@ -0,0 +1,46 @@
+;;;; curried-definitions.test          -*- scheme -*-
+;;;; Copyright (C) 2010  Free Software Foundation, Inc.
+;;;;
+;;;; This library is free software; you can redistribute it and/or
+;;;; modify it under the terms of the GNU Lesser General Public
+;;;; License as published by the Free Software Foundation; either
+;;;; version 3 of the License, or (at your option) any later version.
+;;;; 
+;;;; This library is distributed in the hope that it will be useful,
+;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+;;;; Lesser General Public License for more details.
+;;;; 
+;;;; You should have received a copy of the GNU Lesser General Public
+;;;; License along with this library; if not, write to the Free Software
+;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+(define-module (test-suite test-curried-definitions)
+  #:use-module (test-suite lib)
+  #:use-module (ice-9 curried-definitions))
+
+(with-test-prefix "define"
+  (pass-if "define works as usual"
+    (equal? 34
+            (primitive-eval '(let ()
+                               (define (foo)
+                                 34)
+                               (foo)))))
+  (pass-if "define works as usual (2)"
+    (equal? 134
+            (primitive-eval '(let ()
+                               (define (foo x)
+                                 (+ x 34))
+                               (foo 100)))))
+  (pass-if "currying once"
+    (equal? 234
+            (primitive-eval '(let ()
+                               (define ((foo) x)
+                                 (+ x 34))
+                               ((foo) 200)))))
+  (pass-if "currying twice"
+    (equal? 334
+            (primitive-eval '(let ()
+                               (define (((foo)) x)
+                                 (+ x 34))
+                               (((foo)) 300))))))