Commit high-level version of existing primitive functions.
This commit is contained in:
parent
b1add2caf1
commit
50d9e0e0fc
|
|
@ -0,0 +1,4 @@
|
|||
(define (acons a b lst)
|
||||
(cons (cons a b) lst))
|
||||
|
||||
; vim:set syntax= sw=2 expandtab:
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
;; Evaluates each function in turn, returning #f immediately if any
|
||||
;; function returns #f, or else the first result of the last function.
|
||||
|
||||
(load "foldl.rls")
|
||||
|
||||
(define (and . fns)
|
||||
(let/cc k
|
||||
(foldl (lambda (fn _)
|
||||
(let/cc k2
|
||||
((lambda (x)
|
||||
((if x k2 k) x))
|
||||
(fn))))
|
||||
#t
|
||||
fns)))
|
||||
|
||||
; vim:set syntax= sw=2 expandtab:
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
;; Concatenates the list argument(s) into a single new list.
|
||||
|
||||
(load "foldr.rls")
|
||||
|
||||
(define (append . lsts)
|
||||
(foldr (lambda (lst base)
|
||||
(foldr cons base lst))
|
||||
'()
|
||||
lsts))
|
||||
|
||||
; vim:set syntax= sw=2 expandtab:
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
;; Fold a function over a list; left-associative
|
||||
;; (foldl fn init lst)
|
||||
;; ==> (foldl fn (fn (car lst) init) (cdr lst))
|
||||
|
||||
(define (foldl fn init lst)
|
||||
(if (pair? lst)
|
||||
(foldl fn (fn (car lst) init) (cdr lst))
|
||||
init))
|
||||
|
||||
; vim:set syntax= sw=2 expandtab:
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
;; Fold a function over a list; right-associative
|
||||
;; (foldr fn init lst)
|
||||
;; ==> (fn (car lst) (foldr fn init (cdr lst)))
|
||||
|
||||
(define (foldr fn init lst)
|
||||
(if (pair? lst)
|
||||
(fn (car lst)
|
||||
(foldr fn init (cdr lst)))
|
||||
init))
|
||||
|
||||
; vim:set syntax= sw=2 expandtab:
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
;; Returns a copy of the argument list
|
||||
|
||||
(load "foldr.rls")
|
||||
|
||||
; Function to match builtin cons; builtin will still be preferred.
|
||||
(define (cons x y) (cons x y))
|
||||
|
||||
(define (list . lst)
|
||||
(foldr cons '() lst))
|
||||
|
||||
; vim:set syntax= sw=2 expandtab:
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
(load "reverse.rls")
|
||||
(load "foldl.rls")
|
||||
|
||||
(define (map fn lst)
|
||||
(reverse (foldl (lambda (x lst)
|
||||
(cons (fn x) lst))
|
||||
'()
|
||||
lst)))
|
||||
|
||||
; vim:set syntax= sw=2 expandtab:
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
;; Evaluates each function in turn, immediately returning the first
|
||||
;; non-#f result, or #f if no function returns a non-#f result.
|
||||
|
||||
(load "foldl.rls")
|
||||
|
||||
(define (or . fns)
|
||||
(let/cc k
|
||||
(foldl (lambda (fn _)
|
||||
(let/cc k2
|
||||
((lambda (x)
|
||||
((if x k k2) x))
|
||||
(fn))))
|
||||
#f
|
||||
fns)))
|
||||
|
||||
; vim:set syntax= sw=2 expandtab:
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
;; Returns a reversed copy of the given list
|
||||
|
||||
(load "foldl.rls")
|
||||
|
||||
(define (reverse lst)
|
||||
(foldl cons nil lst))
|
||||
|
||||
; vim:set syntax= sw=2 expandtab:
|
||||
Loading…
Reference in New Issue