Scheme: Searching element in a list and sublist

user2953423

I want to write a function that receives list and returns a list of each element. For example: get - (x 3 4 5 (x 4) 3 x (6))) and receive: (x (x) x ())

(define (lookForX lst)
  (cond
    ((null? lst) '())
    ((eq? (car lst) 'x) (cons (car lst) (lookForX (cdr lst))) )
    (else (lookForX (cdr lst)))))

my code result for:

(lookForX '(x 3 4 5 (x 4) 3 x (6))) 
-> (x x) 

What am I doing wrong?

Sylwester

In you function you are only looking for x as element in the list and you are not doing sub lists:

(define (filter-x lst)
  (cond
    ((null? lst) '())
    ((eq? (car lst) 'x)
     (cons (car lst)
           (filter-x (cdr lst))))
    ((pair? (car lst))
     (cons (filter-x (car lst))
           (filter-x (cdr lst))))
    (else (filter-x (cdr lst)))))

(filter-x '(x 3 4 5 (x 4) 3 x (6)))
; ==> (x (x) x ())

Notice I renamed this to be more lisp like. Lisp code usually don't use camelCase but lisp-case. You can do it more general:

(define (filter-tree predicate? lst)
  (cond
    ((null? lst) '())
    ((predicate? (car lst))
     (cons (car lst)
           (filter-tree predicate? (cdr lst))))
    ((pair? (car lst))
     (cons (filter-tree predicate? (car lst))
           (filter-tree predicate? (cdr lst))))
    (else (filter-tree predicate? (cdr lst)))))

(define (filter-tree-x lst)
  (filter-tree (lambda (v) (eq? v 'x)) lst))

(filter-tree-x '(x 3 4 5 (x 4) 3 x (6)))
; ==> (x (x) x ())

(define (filter-tree-numbers lst)
  (filter-tree number? lst))

(filter-tree-numbers '(x 3 4 5 (x 4) 3 x (6)))
; ==> (3 4 5 (4) 3 (6))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

delete sublist by element occurrence (Scheme)

From Dev

searching an element in list of list

From Dev

Searching for element in Skip List

From Dev

Searching for element in a list

From Dev

Searching for an isolated list element

From Dev

Two Element List Scheme

From Dev

prolog sorting a list based on the sublist of each element

From Dev

Deleting repeated element if it occurs twice in the sublist of a list

From Dev

Cassandra CQL searching for element in list

From Dev

Delete element from List in Scheme

From Dev

Find an element in a nested list? (scheme)

From Dev

Change an element in a list using scheme

From Dev

Get the last element of a list in Scheme

From Dev

Replicate A Given Element in a List in Scheme

From Java

Order list of lists by length of second element in sublist python

From Dev

Split a list into multiple sublist based on element properties in Java

From Dev

Merging first element in sublist with elements in the same list using python

From Dev

scheme - how to retrieve each element of list as an integer

From Dev

Insert element to circular list using scheme

From Dev

Duplicate every found element in a list in Scheme

From Dev

Scheme - Check if element of list is a single letter

From Dev

Cons element to list vs cons list to element in Scheme

From Dev

Adding an element to a sublist

From Dev

Removing Item from List in Scheme leaves empty null list element

From Dev

Abstract List Functions in Racket/Scheme - Num of element occurrences in list

From Dev

What is the most efficient way to find the index of an element in a list, given only an element of a sublist (Python)

From Dev

Searching an element in a list of double-nested dictionaries with generator

From Dev

Defining a macro in Scheme to create fancy sublist

From Dev

Checking if list is a sublist

Related Related

  1. 1

    delete sublist by element occurrence (Scheme)

  2. 2

    searching an element in list of list

  3. 3

    Searching for element in Skip List

  4. 4

    Searching for element in a list

  5. 5

    Searching for an isolated list element

  6. 6

    Two Element List Scheme

  7. 7

    prolog sorting a list based on the sublist of each element

  8. 8

    Deleting repeated element if it occurs twice in the sublist of a list

  9. 9

    Cassandra CQL searching for element in list

  10. 10

    Delete element from List in Scheme

  11. 11

    Find an element in a nested list? (scheme)

  12. 12

    Change an element in a list using scheme

  13. 13

    Get the last element of a list in Scheme

  14. 14

    Replicate A Given Element in a List in Scheme

  15. 15

    Order list of lists by length of second element in sublist python

  16. 16

    Split a list into multiple sublist based on element properties in Java

  17. 17

    Merging first element in sublist with elements in the same list using python

  18. 18

    scheme - how to retrieve each element of list as an integer

  19. 19

    Insert element to circular list using scheme

  20. 20

    Duplicate every found element in a list in Scheme

  21. 21

    Scheme - Check if element of list is a single letter

  22. 22

    Cons element to list vs cons list to element in Scheme

  23. 23

    Adding an element to a sublist

  24. 24

    Removing Item from List in Scheme leaves empty null list element

  25. 25

    Abstract List Functions in Racket/Scheme - Num of element occurrences in list

  26. 26

    What is the most efficient way to find the index of an element in a list, given only an element of a sublist (Python)

  27. 27

    Searching an element in a list of double-nested dictionaries with generator

  28. 28

    Defining a macro in Scheme to create fancy sublist

  29. 29

    Checking if list is a sublist

HotTag

Archive