aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Peter McGoron 2025-09-01 13:10:23 -0400
committerGravatar Peter McGoron 2025-09-01 13:10:23 -0400
commit88d83a868ff85c7386278397a3a38caf3e755844 (patch)
treed72420c07f63d220e7c72e189867211c24232154
parentthe rest of r7rs (diff)
fix list-tail, add some tests
-rw-r--r--lib/hascheme/base.scm57
-rw-r--r--tests/run.scm44
2 files changed, 75 insertions, 26 deletions
diff --git a/lib/hascheme/base.scm b/lib/hascheme/base.scm
index c00ff49..d96547d 100644
--- a/lib/hascheme/base.scm
+++ b/lib/hascheme/base.scm
@@ -24,35 +24,38 @@
(define-syntax if
(syntax-rules ()
- ((if x y ...) (r7rs:if (! x) y ...))))
+ ((if x y ...) (delay-force (r7rs:if (! x) y ...)))))
(define-syntax cond
(syntax-rules (else =>)
- ((cond (else result1 result2 ...))
+ ((cond "iter" (else result1 result2 ...))
(let () result1 result2 ...))
- ((cond (test => result))
+ ((cond "iter" (test => result))
(let ((temp test))
(if temp (result temp))))
- ((cond (test => result) clause1 clause2 ...)
+ ((cond "iter" (test => result) clause1 clause2 ...)
(let ((temp test))
(if temp
(result temp)
- (cond clause1 clause2 ...))))
- ((cond (test)) test)
- ((cond (test) clause1 clause2 ...)
+ (cond "iter" clause1 clause2 ...))))
+ ((cond "iter" (test)) test)
+ ((cond "iter" (test) clause1 clause2 ...)
(let ((temp test))
(if temp
temp
- (cond clause1 clause2 ...))))
- ((cond (test result1 result2 ...))
+ (cond "iter" clause1 clause2 ...))))
+ ((cond "iter" (test result1 result2 ...))
(if test (let () result1 result2 ...)))
- ((cond (test result1 result2 ...)
+ ((cond "iter" (test result1 result2 ...)
clause1 clause2 ...)
(if test
(let () result1 result2 ...)
- (cond clause1 clause2 ...)))))
+ (cond "iter" clause1 clause2 ...)))
+ ((cond clauses ...)
+ (delay-force (cond "iter" clauses ...)))))
(define-syntax case
+ ;; TODO: fix with delay-force
(syntax-rules (else =>)
((case (key ...) clauses ...)
(let ((atom-key (key ...)))
@@ -83,19 +86,19 @@
(define-syntax and
(syntax-rules ()
- ((and x ...) (r7rs:and (! x) ...))))
+ ((and x ...) (delay-force (r7rs:and (! x) ...)))))
(define-syntax or
(syntax-rules ()
- ((or x ...) (r7rs:or (! x) ...))))
+ ((or x ...) (delay-force (r7rs:or (! x) ...)))))
(define-syntax when
(syntax-rules ()
- ((when pred x ...) (r7rs:when (! pred) x ...))))
+ ((when pred x ...) (delay-force (r7rs:when (! pred) x ...)))))
(define-syntax unless
(syntax-rules ()
- ((unless pred x ...) (r7rs:unless (! pred) x ...))))
+ ((unless pred x ...) (delay-force (r7rs:unless (! pred) x ...)))))
(define-syntax define-record-type
(syntax-rules ()
@@ -122,8 +125,8 @@
(define (apply proc . arguments) (r7rs:apply r7rs:apply (! proc) arguments))
-(define error
- (r7rs:lambda formals (delay (r7rs:apply error formals))))
+(define (error message . irritants)
+ (r7rs:apply r7rs:error message irritants))
(r7rs:define (!list list)
(let loop ((list (! list))
@@ -289,10 +292,10 @@
((pair? x) (list? (cdr x)))
(else #f)))
-(define (ensure-exact-positive-integer n k)
- (if (not (and (exact-integer? k) (positive? k)))
- (error "not an exact integer" k)
- #t))
+(define (ensure-exact-positive-integer k)
+ (unless (or (and (number? k) (= k +inf.0))
+ (and (exact-integer? k) (positive? k)))
+ (error "not a positive integer" k)))
(define make-list
(case-lambda
@@ -338,11 +341,13 @@
(else (error "not a pair" x)))))
(define (list-tail list n)
- (seq (ensure-exact-positive-integer n)
- (let loop ((list list) (n n))
- (if (zero? n)
- list
- (list-tail (! (cdr list)) (- n 1))))))
+ (unless (and (exact-integer? n) (not (negative? n)))
+ (error "n must be a non-negative exact finite integer" n))
+ (let loop ((list list) (n n))
+ (if (zero? n)
+ list
+ (seq list
+ (loop (cdr list) (- n 1))))))
(define (list-ref list n)
(car (list-tail list n)))
diff --git a/tests/run.scm b/tests/run.scm
new file mode 100644
index 0000000..f9c5c26
--- /dev/null
+++ b/tests/run.scm
@@ -0,0 +1,44 @@
+(import r7rs
+ (prefix (hascheme base) ha:)
+ (hascheme eager)
+ (srfi 64))
+
+(cond-expand
+ (chicken-5 (test-runner-current (test-runner-create)))
+ (else))
+
+(ha:define
+ (hascheme-natural-numbers)
+ (ha:let loop ((i 0)) (ha:seq i (ha:cons i (loop (ha:+ i 1))))))
+
+(ha:define (square-of-list list) (ha:map ha:square list))
+
+(test-group "lists"
+ (test-assert
+ "cons is lazy, car"
+ (force (ha:car (ha:cons #t (ha:error "boom")))))
+ (test-assert
+ "cons is lazy, cdr"
+ (guard (x ((equal? (force (error-object-message x)) "boom") #t)
+ (else #f))
+ (force (ha:cdr (ha:cons #f (ha:error "boom"))))
+ #f))
+ (test-equal
+ 1000
+ (force (ha:list-ref (hascheme-natural-numbers) 1000)))
+ (let ((flag '()))
+ (test-equal
+ "map is lazy"
+ '(1000)
+ (begin
+ (force (ha:list-ref (ha:map (ha:lambda (x) (set! flag
+ (cons (force x) flag)))
+ (hascheme-natural-numbers))
+ 1000))
+ flag)))
+ (test-equal "runs in bounded space"
+ (square 1000)
+ (force (ha:list-ref (square-of-list (hascheme-natural-numbers))
+ 1000))))
+
+
'logsubject'>Removed acconfig.h, which wasn't needed for some time.Gravatar aeb 1-13/+0 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@41 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-11-22Added ieee1394.h header.Gravatar aeb 3-1/+38 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@40 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-09-13Fix raw1394_start_iso_write() which uses wrong variable.Gravatar aeb 1-1/+1 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@39 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-09-10Work around compiler warnings for int/ptr casts.Gravatar aeb 6-10/+20 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@38 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-09-10Added control files for Debian packages.Gravatar aeb 6-8/+106 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@37 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-09-01Added missing prototypes for iso send functions.Gravatar aeb 1-0/+7 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@36 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-08-08Added raw1394_get_irm_id().Gravatar aeb 7-7/+39 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@35 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-08-06Added support for isochronous sending.Gravatar aeb 3-0/+35 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@34 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-07-05Added raw1394_reset_bus() call.Gravatar aeb 4-0/+23 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@33 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-06-22- Set library version info in configure.in, use in src/Makefile.am.Gravatar aeb 4-2/+16 - Enable compiler warnings. git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@32 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-06-15Update libtool version number.Gravatar aeb 2-2/+2 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@31 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-06-14Added copyright headers.Gravatar aeb 6-0/+54 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@30 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-06-11Added explicit AC_PROG_INSTALL call.Gravatar aeb 1-0/+1 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@29 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-06-09Fix size of error field.Gravatar aeb 1-2/+2 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@28 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-06-02Modified support for 32/64 bit environments, control struct fields have ↵Gravatar aeb 7-43/+28 fixed size now. git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@27 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-05-28Added support for environments with 64 bit kernel and 32 bit userland.Gravatar aeb 8-7/+45 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@26 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-04-27Fixed missing setting of ext code in raw1394_start_lock()Gravatar aeb 1-0/+1 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@25 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-04-15Fixed lock transaction to actually return response value.Gravatar aeb 3-5/+11 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@24 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-04-12Add userdata functions as news.Gravatar aeb 1-0/+4 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@23 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-04-05Add userdata functions.Gravatar aeb 3-0/+18 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@22 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-03-18Bump version number to 0.6.Gravatar aeb 3-5/+6 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@21 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-03-18Mention byte order change.Gravatar aeb 1-0/+2 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@20 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-03-18Mention SourceForge home.Gravatar aeb 1-1/+5 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@19 53a565d1-3bb7-0310-b661-cf11e63c67ab