[plt-scheme] htdp exercise 12.2.2

From: aditya shukla (adityashukla1983 at gmail.com)
Date: Wed Jun 3 16:00:48 EDT 2009

Ok .I think I got the difference now. For the search-sorted we can check for
the first and then if the number is greater than the first then the number
is not in the list only otherwise we can check for the first and do the
recursion on the rest.

This is how I have done it.

;; search : number list-of-numbers  ->  boolean
(define (search n alon)
    [(empty? alon) false]
    [else (cond
            [(> n (first alon)) false]
            [else (or (= (first alon) n) (search n (rest alon)))])]))

(define num (cons 3 (cons 2 (cons 1 empty))))

(search -1 num)

(search 1 empty)

(search 10 num)

(search 3 num)

(search 2 num)


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20090603/c60a0082/attachment.html>

Posted on the users mailing list.