Commit high-level version of existing primitive functions.

This commit is contained in:
Jesse D. McDonald 2010-05-04 22:06:47 -05:00
parent b1add2caf1
commit 50d9e0e0fc
9 changed files with 97 additions and 0 deletions

View File

@ -0,0 +1,4 @@
(define (acons a b lst)
(cons (cons a b) lst))
; vim:set syntax= sw=2 expandtab:

16
src/lib/primitive/and.rls Normal file
View File

@ -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:

View File

@ -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:

View File

@ -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:

View File

@ -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:

View File

@ -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:

10
src/lib/primitive/map.rls Normal file
View File

@ -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:

16
src/lib/primitive/or.rls Normal file
View File

@ -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:

View File

@ -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: