Racket\Scheme compare and delete unwanted items in a list

142 views Asked by At

I have 2 lists:

(Define list1 '("xx1" "xx2" xx3" "xx4" "xx5"))
(Define list2 '("xx2" "xx4" "xx5"))

the items in the list above are just an example, but either way it will be a string item. What I need to do is compare both lists and remove the items in list1 that are found in list2.

is there a short map routine i can do?

this isn't some homework project, I wish there was a course here for programming classes though :/

2

There are 2 answers

0
soegaard On BEST ANSWER
#lang racket
(define list1 '("xx1" "xx2" "xx3" "xx4" "xx5"))
(define list2 '("xx2" "xx4" "xx5"))

(for/list ([x (in-list list1)]
           #:unless (member x list2))
  x)

Result:

 '("xx1" "xx3")
0
minikomi On

There's also remove*

An example:

#lang racket
(define list1 '("xx1" "xx2" "xx3" "xx4" "xx5"))
(define list2 '("xx2" "xx4" "xx5"))

(displayln (remove* list2 list1))

Prints:

(xx1 xx3)