SCHEME remove atomic value from a list -


i need function send value, , check in list if there equal value remove it. here examples:

(elimina 1 '(a b c))            => (a b c) (elimina 'b '(a (b) c))         => (a () c) (elimina 1  '(0 (1 (2) 1) 0))   => (0 ((2)) 0) 

i tried this:

(define (elimina v1 lista)   (cond ((null? lista)'())          ((list? (first lista))          (list (elimina v1 (first lista))))          (else          (if(equal? v1 (first lista))            (elimina v1 (cdr lista))            (append (cons (first lista) (elimina v1 (cdr lista))))))    ) ) 

and results this:

(elimina 1 '(a b c))         => (a b c) (elimina 'b '(a (b) c))      => (a ()) (elimina 1  '(0 (1 (2) 1) 0) => (0 ((2))) 

for reason last value on list isn't showing. hope can help.

thanks!

1) problem here:

((list? (first lista))          (list (elimina v1 (first lista)))) 

when recurse sublist, don't process rest of list anymore.

2) also,

(append (cons (first lista) (elimina v1 (cdr lista)))))) 

can simplified, because you're appending list nothing, drop append.

3) not error, recommend use either first, second, rest ... or car, cadr, cdr ...

try:

(define (elimina v1 lista)   (cond      ((null? lista) '())     ((list? (first lista))       (cons (elimina v1 (first lista)) (elimina v1 (rest lista)))) ; <= (1)     (else      (if (equal? v1 (first lista))          (elimina v1 (rest lista))          (cons (first lista) (elimina v1 (rest lista)))))))       ; <= (2) 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -