2
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)?

1
  • 1
    Is it 5 or 0? the problem description states that it's 0, but the code uses 5 Commented Mar 10, 2013 at 4:13

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.