J. Doe
J. Doe

Reputation: 43

Memv procedure in Scheme

(define memv2
   (lambda (x l)
    (cond
     ((null? l) #f)
     ((eqv? (car l) x) 
        cdr l)
      (else
        (memv2 x (cdr l))))

Studying for an exam - this code has been provided in my notes as a replication of the built in memv function in Scheme. I was wondering if someone could explain what the #f is doing in this situation. Is it exiting the loop?

(memv takes in an element and a list, and returns the list from the point of the element onward, for example: (memv 2 '(1 2 3 4 5)) would return (2 3 4 5))

Upvotes: 3

Views: 1330

Answers (1)

Óscar López
Óscar López

Reputation: 236004

The #f value is returned when the procedure finishes traversing the list, which means that the searched element was not found, thus ending the recursion.

Upvotes: 4

Related Questions