scheme - Create lists from lists -
i want write function n
arguments create n
lists , each contains n-th element every argument, example:
(aux '(1 2) '(3 4)) = `((1 3) (2 4))
i wrote such function:
(define (aux . args) (if (null? args) '() (cons (map car args) (aux (map cdr args)))))
but when try evalute (aux '(1 2) '(3 4))
repl not show output. question should change because don't see syntax errors.
chris correct. in event want use rest arguments , use in recursion should consider wrapping in named let
or make local helper procedure.
(define (zip . args) (let aux ((args args)) (if (ormap null? args) '() (cons (map car args) (aux (map cdr args))))))
i when there arguments don't change. eg. map
implementation 1 list don't pass procedure @ each iteration:
(define (map1 proc lst) (let aux ((lst lst)) (if (null? lst) '() (cons (proc (car lst)) (aux (cdr lst))))))
of course happen implementation don't think of of these optimizations. it's code clarity.
Comments
Post a Comment