UNSLISP/util.scm

36 lines
763 B
Scheme
Raw Normal View History

2024-09-04 02:16:10 -04:00
;;; 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))))))