lambda - Variadic append function (append any number of lists together) -
i'm trying implement scheme procedure append myself. simplest version append 2 lists quite easy , can done with:
(define (append lis1 lis2) (if (null? lis1) lis2 (cons (car lis1) (append (cdr lis1) lis2))))
the trouble starts when want append any number of lists. 0 lists , 1 lists the idea simple, im having real hard time thinking how apply procedure number of lists... appreciated, oren
Óscar's answer reverse
input , use foldl
instead foldl
implemented proper tail call.
(define (append* . xs) (foldl append null (reverse xs)))
output same
(append* '(1 2) '(3) '(4 5 6) '(7 8)) => '(1 2 3 4 5 6 7 8)
Comments
Post a Comment