Peter McGoron
8bee2d39a4
After taking a look at the R7RS syntax and how Chicken parses directives, I realized that it's easier to have "#!", "#\\", etc. parse identifiers instead of baking in trie actions. This is slightly slower but completely removes the trie concept from the readtable, which simplifies the implementation and removes many corner cases involving combining readtables with different action types.
50 lines
1.2 KiB
Scheme
50 lines
1.2 KiB
Scheme
;;; Utilities.
|
|
|
|
;;; ;;;;;;;;;;;;;;;;
|
|
;;; Versions of FOLD
|
|
;;; ;;;;;;;;;;;;;;;;
|
|
|
|
(define fold
|
|
(lambda (f init lst)
|
|
(if (null? lst)
|
|
init
|
|
(fold f (f (car lst) init) (cdr lst)))))
|
|
|
|
(define fold-vector
|
|
(lambda (f init vec)
|
|
(if (list? vec) ; Support MiniScheme
|
|
(fold f init vec)
|
|
(letrec
|
|
((loop
|
|
(lambda (i val)
|
|
(if (= i (vector-length vec))
|
|
val
|
|
(loop (+ i 1) (f (vector-ref vec i) val))))))
|
|
(loop 0 init)))))
|
|
|
|
;;; ;;;;;;;;;;;;;;;;;;;;
|
|
;;; Misc. list functions
|
|
;;; ;;;;;;;;;;;;;;;;;;;;
|
|
|
|
(define all
|
|
(lambda (f lst)
|
|
(cond
|
|
((null? lst) #t)
|
|
((not (f (car lst))) #f)
|
|
(else (all f (cdr lst))))))
|
|
|
|
;;; (REVAPPEND L1 ... LN) returns L{N-1}, L{N-2}, ... reversed and
|
|
;;; appended to LN, in that order.
|
|
(define revappend
|
|
(letrec ((loop
|
|
(lambda (lst1 lst2)
|
|
(if (null? lst1)
|
|
lst2
|
|
(loop (cdr lst1) (cons (car lst1) lst2))))))
|
|
(lambda lists
|
|
(cond
|
|
((null? lists) '())
|
|
((null? (cdr lists)) (car lists))
|
|
(else
|
|
(apply revappend (loop (car lists) (cadr lists))
|
|
(cddr lists)))))))
|