From a89d6c82e981fec7d6e4c975e083d2b9e04467ad Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Mon, 1 May 2023 07:56:42 -0700 Subject: Rewrite most of the compiler. This represents a major step back in terms of functionality, and amount of code. The latter I think constitutes a major win. Next steps are to reimplement syntax-rules, call/cc, and call-with-values. --- lib/csc/list.scheme | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 lib/csc/list.scheme (limited to 'lib/csc/list.scheme') diff --git a/lib/csc/list.scheme b/lib/csc/list.scheme new file mode 100644 index 0000000..ea25454 --- /dev/null +++ b/lib/csc/list.scheme @@ -0,0 +1,73 @@ +(define-library (csc list) + (export + all + any + filter + foldl + foldr + intersperse + map-maybe) + (import (scheme base)) + (begin + + + (define (any1 f l) + (and (not (null? l)) + (or (f (car l)) + (any1 f (cdr l))))) + + + (define (any f . ls) + (and (not (any1 null? ls)) + (or (apply f (map car ls)) + (apply any f (map cdr ls))))) + + + (define (all f . ls) + (or (any null? ls) + (and (apply f (map car ls)) + (apply all f (map cdr ls))))) + + + (define (foldl f acc . ls) + (if (any null? ls) + acc + (apply foldl f (apply f acc (map car ls)) (map cdr ls)))) + + + (define (foldr f acc . ls) + (if (any null? ls) + acc + (apply f (append (map car ls) (list (apply foldr f acc (map cdr ls))))))) + + + (define (map-maybe f l) + (foldr + (lambda (x acc) + (define fx (f x)) + (if fx + (cons fx acc) + acc)) + '() + l)) + + + (define (filter f l) + (foldr + (lambda (x acc) + (if (f x) + (cons x acc) + acc)) + '() + l)) + + + (define (intersperse sep l) + (if (null? l) + '() + (cons (car l) + (foldr + (lambda (x acc) + (cons sep (cons x acc))) + '() + (cdr l))))))) -- cgit v1.3.1