简体   繁体   中英

Recursively creating list in scheme

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.

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)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM