SCIPゼミ第6回

練習問題2.12
(define (make-center-percent c p) 
	(define w (* c (/ p 100)))
	(make-interval (- c w) (+ c w)))

by どりきゃす

練習問題2.17
(define (last-pair l)
  (if (= (length l) 1)
    l
    (last-pair (cdr l))))

by バリィさん

練習問題2.18
(define (reverse l)
  (if (= (length l) 1)
    (list (car l))
    (append (reverse (cdr l)) (list (car l))))

by バリィさん

練習問題2.19
(define (cc amount coin-values)
  (cond ((= amount 0) 1)
        ((or (< amount 0) (no-more? coin-values )) 0)
        (else (+ (cc amount (except-first-denomination coin-values ))
                 (cc (- amount (first-denomination coin-values )) coin-values )))))

(define no-more? null?)
(define except-first-denomination cdr)
(define first-denomination car)

(define us-coins (list 50 25 10 5 1))
(define uk-coins (list 100 50 20 10 5 2 1 0.5))

実行結果

gosh> (cc 100 us-coins)
292

by pine

練習問題2.20
(define nil '())
(define (same-parity x . z)
  (define rem (remainder x 2))
  (define (par-lis z)
    (if (null? z)
        nil
        (if (= (remainder (car z) 2) rem)
            (append (list (car z)) (par-lis (cdr z)))
            (par-lis (cdr z)))))
  (append (list x) (par-lis z)))

by pine

練習問題2.21
(define (square x) (* x x))
(define (square-list items)
(if (null? items)
'()
(cons (square (car items)) (square-list (cdr items)))))

(define (square-list items)
	(map square items))
練習問題2.22

>逆順になる
逆だもん。
>なおしたけどうまくいかないよお><
(cons [list] [value])にすると((...('() value) value) value)...)になるからconsじゃなくてappendしろ。

練習問題2.23
(define (foreach lamb li)
  (map lamb li)
  (newline))

これで動くけどちょっとズルいかも

(define (for-each func l)
	(cond 
		((null? l) (undefined))
		(else (func (car l)) (for-each func (cdr l)))
	)
)

by どりきゃす