NoSense
NoSense

Reputation: 949

delete sublist by element occurrence (Scheme)

How I could remove a sublist by searching only one elem from it.

For example, let's have the list:

( (pacific (atlanta ohaio) (NY LI))
  (atlanta (pacific blue) (ohaio green)) )

And I want to remove "pacific" from the list and to get:

( (pacific (atlanta ohaio) (NY LI))
  (atlanta (ohaio green)) )

Any ideas would be greatly appreciated :).

Upvotes: 1

Views: 212

Answers (1)

Óscar López
Óscar López

Reputation: 235994

The criteria for deleting an element from the input list is not stated clearly in the question. This will work for the example shown:

(define lst
  '((pacific (atlanta ohaio) (NY LI))
    (atlanta (pacific blue) (ohaio green))))

(map (lambda (slst)
       (filter (lambda (e)
                 (not (and (list? e) (member 'pacific e))))
               slst))
     lst)

=> '((pacific (atlanta ohaio) (NY LI)) (atlanta (ohaio green)))

If necessary, for other inputs you can tweak the condition in the innermost lambda until the result is as desired. For instance, I interpreted the comment in the question:

I want to delete the sublist that contains the searched word

As: "find the searched word in any position inside a sublist"; if the searched word can be found in, say, only the first position then adjust the condition accordingly.

Upvotes: 1

Related Questions