Added (append)

This commit is contained in:
Justin Ethier 2015-03-23 23:02:26 -04:00
parent ef53201de5
commit fac76980fe

View file

@ -70,6 +70,20 @@
((pair? obj)
(list? (cdr obj)))
(else #f)))
; append accepts a variable number of arguments, per R5RS. So a wrapper
; has been provided for the standard 2-argument version of (append).
;
; We return the given value if less than 2 arguments are given, and
; otherwise fold over each arg, appending it to its predecessor.
(define (append . lst)
(define append-2
(lambda (inlist alist)
(foldr (lambda (ap in) (cons ap in)) alist inlist)))
(if (null? lst)
lst
(if (null? (cdr lst))
(car lst)
(foldl (lambda (a b) (append-2 b a)) (car lst) (cdr lst)))))
(define (list . objs) objs)
(define (map func lst)
(foldr (lambda (x y) (cons (func x) y)) '() lst))