From 96d35184bae81888352645ad28efa279c3d5e322 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Thu, 23 Feb 2023 01:08:16 +0100 Subject: Extend `for'-macro to allow improper list elements. --- module/hnh/util.scm | 3 +++ tests/test/util.scm | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/module/hnh/util.scm b/module/hnh/util.scm index ffac17bd..36b56c58 100644 --- a/module/hnh/util.scm +++ b/module/hnh/util.scm @@ -118,6 +118,9 @@ ((for ( ...) in b1 body ...) (map ((@ (ice-9 match) match-lambda) [( ...) b1 body ...]) )) + ((for ( ... . ) in b1 body ...) + (map ((@ (ice-9 match) match-lambda) [( ... . ) b1 body ...]) + )) ((for in b1 body ...) (map (lambda () b1 body ...) )))) diff --git a/tests/test/util.scm b/tests/test/util.scm index c91017fc..2a171d12 100644 --- a/tests/test/util.scm +++ b/tests/test/util.scm @@ -49,6 +49,16 @@ (for (x c) in (zip (iota 12) (string->list "Hello, World")) x)) +(test-equal "for with improper list elements" + `(3 7) + (for (a . b) in '((1 . 2) (3 . 4)) + (+ a b))) + +(test-equal "for with longer improper list elements" + '(1 2 4) + (for (a b . c) in '((1 -1 . 1) (2 -2 . 2) (4 -4 . 4)) + (* c (+ 1 a b)))) + (test-equal "procedure label" 120 ((label factorial (lambda (n) -- cgit v1.2.3