From fac76980fe6e863fcc15023eeeefaa0f1ed6cbd9 Mon Sep 17 00:00:00 2001 From: Justin Ethier Date: Mon, 23 Mar 2015 23:02:26 -0400 Subject: [PATCH] Added (append) --- trans.scm | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/trans.scm b/trans.scm index 8fea0407..692a390b 100644 --- a/trans.scm +++ b/trans.scm @@ -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))