dimanche 21 septembre 2014

SCHEME remove atomic value from a list


Vote count:

0




I need to do a function where I send a value, and check in the list if there is an equal value to remove it. Here are some 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 my results where like 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 some reason the last value on the list isn't showing. Hope someone can help.


Thanks!



asked 34 secs ago







SCHEME remove atomic value from a list

Aucun commentaire:

Enregistrer un commentaire