Home > Software design >  What is the correct way to use for/fold check if an element exists in nested list ;Racket?
What is the correct way to use for/fold check if an element exists in nested list ;Racket?

Time:04-21

So, I am trying to write a function that takes two arguments, a nested list of numbers L and a number n. The function returns true if the number n belongs to any of the lists and false otherwise.
E.g., (sob35 ‘((1 2 3) (5 4) (6 7) (8 9 10 11)) 8) returns true and (sob35 ‘((1 2 3) (5 4) (6 7) (8 9 10 11)) 22) returns false.

i tested this with the member function using rest to see if it will check if 6 was a member of the nested list,

 (member 6 ((rest  '((1 2 3) (5 4) (6 7) (8 9 10 11))))) it returned



application: not a procedure;
 expected a procedure that can be applied to arguments
  given: '((5 4) (6 7) (8 9 10 11))  what i had given it was indeed a list



 (define sob35
(lambda ( l1 l2 )
(for/list ([ L l1 ] [ N l2 ])
   (if ( member N (rest (L))) "True" "False"))))

i dont think i have the correct idea because i thinking if N is a member of L it should return true else it should return false ,though when i ran it

(sob35 '((1 2 3) (5 4) (6 7) (8 9 10 11)) 8)
. . application: not a procedure;
 expected a procedure that can be applied to arguments
  given: '(1 2 3)

i don't understand the application error is trying to imply;its supposed to take a list

CodePudding user response:

You need a member function which works in a nested manner. In lisp tradition, it will be called member* - because it is the recursively working form of member (which is for flat lists).

(define (member*? nested-list element (test equal?))
  (cond ((null? nested-list) #f)
        ((list? (car nested-list)) (or (member*? (car nested-list) element test)
                                       (member*? (cdr nested-list) element test)))
        ((test (car nested-list) element) #t)
        (else (member*? (cdr nested-list) element test))))

Because nesting can be arbitrarily deep but recursion isn't, one can use tail-recursive functions.

(define (member*? nested-list element (test equal?) (acc #f))
  (cond ((null? nested-list) acc)
        ((list? (car nested-list)) (member*? (cdr nested-list) element test
                                             (or acc (member*? (car nested-list) element test))))
        ((test (car nested-list) element) #t)
        (else (member*? (cdr nested-list) element test acc))))

CodePudding user response:

Using a for/or over a List of Lists input, to achieve what is needed...

(define two-sequences 
  (lambda (l1 l2) 
    (for/or ([N l1]) 
      (and (member l2 N) #t))))

Test

racket@> (define two-sequences (lambda (l1 l2) (for/or ([N l1]) (and (member l2 N) #t))))
racket@> (two-sequences '((1 2 3) (5 4) (6 7) (8 9 10 11)) 6)
#t
racket@> (two-sequences '((1 2 3) (5 4) (6 7) (8 9 10 11)) 16)
#f
racket@> (two-sequences '((1 2 3) (5 4) (6 7) (8 9 10 11)) 8)
#t
racket@> (two-sequences '((1 2 3) (5 4) (6 7) (8 9 10 11)) 10)
#t
racket@> (two-sequences '((1 2 3) (5 4) (6 7) (8 9 10 11)) 3)
#t
racket@> (two-sequences '((1 2 3) (5 4) (6 7) (8 9 10 11)) 12)
#f
racket@> (two-sequences '((1 2 3) (5 4) (6 7) (8 9 10 11)) 1)
#t
racket@> (two-sequences '((1 2 3) (5 4) (6 7) (8 9 10 11)) 2)
#t
racket@> (two-sequences '((1 2 3) (5 4) (6 7) (8 9 10 11)) 3)
#t
racket@> (two-sequences '((1 2 3) (5 4) (6 7) (8 9 10 11)) 4)
#t
racket@> (two-sequences '((1 2 3) (5 4) (6 7) (8 9 10 11)) 5)
#t
racket@> (two-sequences '((1 2 3) (5 4) (6 7) (8 9 10 11)) 6)
#t
racket@> (two-sequences '((1 2 3) (5 4) (6 7) (8 9 10 11)) 7)
#t
racket@> (two-sequences '((1 2 3) (5 4) (6 7) (8 9 10 11)) 8)
#t
racket@> (two-sequences '((1 2 3) (5 4) (6 7) (8 9 10 11)) 9)
#t
racket@> (two-sequences '((1 2 3) (5 4) (6 7) (8 9 10 11)) 10)
#t
racket@> (two-sequences '((1 2 3) (5 4) (6 7) (8 9 10 11)) 11)
#t
racket@> (two-sequences '((1 2 3) (5 4) (6 7) (8 9 10 11)) 12)
#f
racket@> (two-sequences '((1 2 3) (5 4) (6 7) (8 9 10 11)) 13)
#f
racket@> (two-sequences '((1 2 3) (5 4) (6 7) (8 9 10 11)) 14)
#f
racket@> (two-sequences '((1 2 3) (5 4) (6 7) (8 9 10 11)) 15)
#f
racket@> 

How about this...

(member 6 (rest  '((1 2 3) (5 4) (6 7) (8 9 10 11))))

In the original call, it's like...

(member 6 ( (rest  '((1 2 3) (5 4) (6 7) (8 9 10 11))) ))
        ;;^                                         ;; ^ 

which gets a treatment of a function application after (rest ...) is evaluated.

(let ((result-of-evaluation-of-rest (rest  '((1 2 3) (5 4) (6 7) (8 9 10 11))))) 
  (member 6 (result-of-evaluation-of-rest)))

where result-of-evaluation-of-rest isn't a function, but a list. Whereas what we actually want is ...

(let ((result-of-evaluation-of-rest (rest  '((1 2 3) (5 4) (6 7) (8 9 10 11))))) 
  (member 6 result-of-evaluation-of-rest))
  • Related