shklaurant
shklaurant

Reputation: 69

How can i subtract arbitrary 2 lists in racket?

I want to define a function that consumes 2 lists and do subtraction resulting another list.

For example, when list1 is '(a a b b c) and list2 is '(a b), the subtraction result should be '(a b c).

I tried to implement it by lambda and remove, ended up making sevral lists.

I really have no idea how to do this.

Upvotes: 2

Views: 1566

Answers (1)

soegaard
soegaard

Reputation: 31145

Here is one way:

#lang racket
(define (subtract xs ys)
  (if (empty? ys)
      xs
      (subtract (remove (first ys) xs) (rest ys))))

(subtract '(a a b b c) '(a b))

Upvotes: 1

Related Questions