2009/03/31

[The Little Schemer]member?

The Little Schemer」P.22 ~

 

  • The First Commandment
    • (preliminary)
    • Always ask null? as the first question in expressing any function.
    • 関数を評価する際は、null?手続きが常に初めの問いとなるでしょう。

 

(member? 'tea '(coffee tea or milk))
;; => #t
  • because, one of the atoms of the '(coffee tea or milk), is the same as the atom tea.
  • なぜなら、'(coffee tea or milk)のatomの一つと、atomであるteaは同じであるから。

 

(member? 'poached '(fried eggs and scrambled eggs))
;; => #f

 

自前

(define member?
  (lambda (a lat)
    (cond ((null? lat) #f)
               ((eq? (car lat) a) #t)
               (else (member? a (cdr lat))))))

 

member?
;; =>  #<closure member?>

 

(member? 'tea '(coffee tea or milk))
;; => #t

 

(member? 't '(coffee tea or milk))
;; => #f

 

(member? 'meat '(mashed potatoses and meat gravy))
;; => #t

 

回答

(define member?
  (lambda (a lat)
    (cond
     ((null? lat) #f)
     (else (or (eq? (car lat) a)
               (member? a (cdr lat)))))))

 

(member? 'tea '(coffee tea or milk))
;; => #t

 

(member? 'coke '(cofee tea or milk))
;; => #f

 

(member? 'meat '(mashed potatoses and meat gravy))
;; => #t

 

 

  • cond
    • else
      • Is else really a question?
      • Yes, else is a question whoose value is always true.

 

 

  • Do It, Do It Again, and Again, and Again ...

0 件のコメント:

コメントを投稿