2
votes
Design a function that consumes a [Listof Number] and checks if 0 is in the list.

I want to figure this out using lambda. I've been reading the book, and this is what I've came up with so far. I hope im close.

(define (five? lon)
  (..... (lambda (x)
           (= x 5))
         lon))

how would you finish this (if right)?

2
Is it 5 or 0? the problem description states that it's 0, but the code uses 5Óscar López

2 Answers

1
votes

Yup! Checkout the ormap function.

procedure

(ormap proc lst ...+) → any
  proc : procedure?
  lst : list?

From the Racket documentation:

Similar to map in the sense that proc is applied to each element of lst, but

  • The result is #f if every application of proc produces #f; and

  • The result is that of the first application of proc producing a value other than #f, in which case proc is not applied to later elements of the lsts; the application of proc to the last elements of the lsts is in tail position with respect to the ormap call.

1
votes

Assuming that the number that's being searched is 5 (as inferred from the code and not the problem description) - there's a procedure that already does what's being asked, it's called member:

(member 5 '(1 3 5 7))
=> '(5 7)

Which, according to the documentation:

Locates the first element of lst that is equal? to v. If such an element exists, the tail of lst starting with that element is returned. Otherwise, the result is #f.

But if we were to implement a similar procedure from scratch, this is how the solution would look, fill-in the blanks:

(define five?
  (lambda (lon)
    (if <???>                 ; is the list empty?
        <???>                 ; then the number is not in the list
        (or <???>             ; otherwise the current element is 5 or
            (five? <???>))))) ; the number is in the rest of list

Don't forget to test your code:

(five? '())
=> #f
(five? '(1 2 3 6 7 9))
=> #f
(five? '(1 2 3 5 7 9))
=> #t