'What is the correct way to use for/fold check if an element exists in nested list ;Racket?
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
Solution 1:[1]
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))
Solution 2:[2]
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))))
Solution 3:[3]
A simple "for loop" answer is likely to be specific to "list of lists" arguments.
Racket's for/fold, referenced in the question, could be used:
(define (member-of-sub-list? lols x)
(for/fold ([result #f])
([ls lols])
(or result (not (not (member x ls))))))
;(member-of-sub-list? '((1) (2 3 4)) 3) => #t
;(member-of-sub-list? '((1) (2 3 4)) 99) => #f
However, when learning Scheme or Racket, developing functions like this systematically is helpful.
First, write a stub function with signature and purpose, and simple tests:
(define (member-of-sub-list? lolox x) ;; (ListOf ListOfX) X -> Boolean
;; produce whether x occurs in sub-lists of lolox
#f)
(check-expect (member-of-sub-list? '(()) 0) #f)
(check-expect (member-of-sub-list? '((0)) 0) #t)
Then use the examples and a standard template to fill out the function, and test:
(define (member-of-sub-list? lolox x) ;; (ListOf ListOfX) X -> Boolean
;; produce whether x occurs in sub-lists of lolox
(cond
[(empty? lolox) #f ]
[(member x (first lolox)) #t ]
[else (member-of-sub-list? (rest lolox) x) ]))
(check-expect (member-of-sub-list? '((1) (2 3 4) ()) 0) #f)
(check-expect (member-of-sub-list? '((1) (2 0 4) ()) 0) #t)
(This solution can take less time to write than reading and understanding for/fold documentation)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Gwang-Jin Kim |
| Solution 3 | mnemenaut |
