Making a new list with recursion from scheme -
this question has answer here:
i picking scheme way learn recursive , found me:d now, have question. how make function called thirds picks 1 element , skips 2 , repeats process over. returns new list first element from, every triple of elements example (thirds '(a b c d e f g h))
should return (a d g)
(define (thirds lst) (cond ((not(list? lst)) (newline) "usage: (thirds [list])") ((null? lst) lst) ((null? (cdr lst)) (car lst)) (else (cons (car lst) (thirds (cdr(cdr(cdr lst)))) )) ))
thats code have tried not real luck.. help?
there's way this, create helper function , pass along index.
(define (thirds l) (thirds-helper l 0)) (define (thirds-helper l i) (cond ((null? l) '()) ((= 0 (modulo 3)) (cons (car l) (thirds-helper (cdr l) (+ 1)))) (else (thirds-helper (cdr l) (+ 1)))))
exercise reader. can modify pick out given nth values of list?
Comments
Post a Comment