Question

I come up again with my strange Scheme questions.

I have a definition that remove subelement(based on search occurrence) from a list and generate new list without it (based on this answer here).

(func '1 mylist) ; will return mylist without all sublists containing 1

All was working fine until I realized that I need to repeat my definition for each element in another list.

I call it recursively, but with every call I use the original list not the previous filtered.

Or with another words I want to achieve this:

(define filterList '(1 2 3))

(func '3 (func '2 (func '1 mylist) ); list without all sublists containig 1 2 3

I'm tottaly stuck. Thanks everyone for the help.

Was it helpful?

Solution

This is a basic loop over the lists of elements (elts) to remove from the initial list (lst):

(define (func2 elts lst)
  (if (null? elts)
      lst
      (func2 (cdr elts) (func (car elts) lst))))

then

(func2 '(1 3) '(1 2 3))
=> '(2)

or, in Racket:

(define (func2 elts lst)
  (for/fold ((res lst)) ((e (in-list elts)))
    (func e res)))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top