simply scheme 第六章 練習

6.1 nowhere man,3,goes

6.2 #t #f #f #t #t #t

6.3 (define (sign number)

  (cond ((< number 0) 'negative)

        ((= number 0) 'zero)

        (else 'positive)))

6.4 (define (utensil meal)

  (if (equal? meal 'chinese) 'chopsticks 'forks))

6.5 (define (european-time st)

  (cond ((equal? st '(12 pm)) 12)

        ((equal? st '(12 am)) 24)

        ((equal? (last st) 'am) (first st))

        ((equal? (last st) 'pm) (+(first st) 12))))

(define (american-time wd)

  (cond ((equal? wd '12) '(12 PM))

        ((equal? wd '24) '(12 AM))

        ((< wd 12) (se wd 'AM))

        ((> wd 13) (se (- wd 12) 'PM))))

6.6 (define (teen arg)

  (if (and (>= arg 13)(<= arg 19)) #t #f))

6.7 (define (type-of st)

  (cond ((number? st) 'number)

        ((boolean? st) 'boolean)

        ((word? st) 'word)

        (else 'sentence)))

6.8 (define (indef-article wd)

  (if (vowel? (first wd)) (se 'an wd) (se 'a wd))) 

(define (vowel? letter)

  (member? letter 'aeiou))

6.9 (define (thismany arg1 arg2)

  (if(< arg1 2) (se arg1 arg2) (se arg1 (word arg2 's))))

6.10 (define (sort2 st)

  (if(< (last st) (first st)) (se (last st)(first st)) st))

6.11 

(define (valid-date? a1 a2 a3)

  (if (and (< a1 13)(> a1 0)(> a3 0)) (check a1 a2 a3) #f))

(define (check a1 a2 a3)

  (cond((member? a1 '(1 3 5 7 8 10 12)) (if (and(> a2 0) (< a2 32)) #t #f))

       ((= a1 2) (if (leapyear? a3)

                     (if(and(> a2 0)(< a2 30)) #t #f)

                     (if(and(> a2 0)(< a2 29)) #t #f)))

       ((member? a1 '(4 6 9 11)) (if(and(> a2 0)(< a2 31)) #t #f))))

(define (leapyear? a)

  (if(div a 100) 

      (if(div a 400) #t #f)

      (if(div a 4) #t #f)))

(define (div x y)

  (if(=(remainder x y) 0) #t #f))

6.14

(define (describe-time wd)

  (cond ((< wd 60)

         (convert wd 'second))

        ((< wd (* 60 60))

         (convert (/ wd 60) 'minute))

        ((< wd (* 60 60 24))

         (convert(/ wd (* 60 60))'hour))

        ((< wd (* 60 60 24 30))

         (convert(/ wd(* 60 60 24))'day))

        ((< wd (* 60 60 24 30 12))

         (convert(/ wd(* 60 60 24 30))'month))

        ((< wd (* 60 60 24 30 12 100))

         (convert(/ wd(* 60 60 24 30 12))'year))

        (else (convert (/ wd(* 60 60 24 30 12 100))'century))))

(define (convert a b)

  (se a (if(= a 1) b (word b 's))))

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章