問題4.35 – SICP(計算機プログラムの構造と解釈)その209
2009年06月29日
問題4.35
最初に思いついた解答がこれ。
2つの整数間の数からなるリストを作り、 an-element-of
でその内の1つの整数を返す。
(define (require p) (if (not p) (amb))) (define (an-element-of items) (require (not (null? items))) (amb (car items) (an-element-of (cdr items)))) (define (an-integer-between low high) (define (make-list start end) (if (> start end) (error "start is bigger than end -- MAKE-LIST") (if (= start end) (cons end '()) (cons start (make-list (+ start 1) end))))) (an-element-of (make-list low high)))
実行結果
;;; Amb-Eval input: (define (require p) (if (not p) (amb))) ;;; Starting a new problem ;;; Amb-Eval value: ok ;;; Amb-Eval input: (define (an-element-of items) (require (not (null? items))) (amb (car items) (an-element-of (cdr items)))) ;;; Starting a new problem ;;; Amb-Eval value: ok ;;; Amb-Eval input: (define (an-integer-between low high) (define (make-list start end) (if (> start end) (error "start is bigger than end -- MAKE-LIST") (if (= start end) (cons end '()) (cons start (make-list (+ start 1) end))))) (an-element-of (make-list low high))) ;;; Starting a new problem ;;; Amb-Eval value: ok ;;; Amb-Eval input: (define (make-list start end) (if (> start end) (error "start is bigger than end -- MAKE-LIST") (if (= start end) (cons end '()) (cons start (make-list (+ start 1) end))))) ;;; Starting a new problem ;;; Amb-Eval value: ok ;;; Amb-Eval input: (define (a-pythagorean-triple-between low high) (let ((i (an-integer-between low high))) (let ((j (an-integer-between i high))) (let ((k (an-integer-between j high))) (require (= (+ (* i i) (* j j)) (* k k))) (list i j k))))) ;;; Starting a new problem ;;; Amb-Eval value: ok ;;; Amb-Eval input: (a-pythagorean-triple-between 5 30) ;;; Starting a new problem ;;; Amb-Eval value: (5 12 13) ;;; Amb-Eval input: try-again ;;; Amb-Eval value: (6 8 10)
amb
を使った解答。
(< low high)
を満たす場合を探索する。
(define (an-integer-between low high) (require (< low high)) (amb low (an-integer-between (+ low 1) high)))
実行結果
;;; Amb-Eval input: (define (require p) (if (not p) (amb))) ;;; Starting a new problem ;;; Amb-Eval value: ok ;;; Amb-Eval input: (define (an-element-of items) (require (not (null? items))) (amb (car items) (an-element-of (cdr items)))) ;;; Starting a new problem ;;; Amb-Eval value: ok ;;; Amb-Eval input: (define (an-integer-between low high) (require (< low high)) (amb low (an-integer-between (+ low 1) high))) ;;; Starting a new problem ;;; Amb-Eval value: ok ;;; Amb-Eval input: (define (a-pythagorean-triple-between low high) (let ((i (an-integer-between low high))) (let ((j (an-integer-between i high))) (let ((k (an-integer-between j high))) (require (= (+ (* i i) (* j j)) (* k k))) (list i j k))))) ;;; Starting a new problem ;;; Amb-Eval value: ok ;;; Amb-Eval input: (a-pythagorean-triple-between 5 30) ;;; Starting a new problem ;;; Amb-Eval value: (5 12 13) ;;; Amb-Eval input: try-again ;;; Amb-Eval value: (6 8 10)
計算機プログラムの構造と解釈
posted with amazlet at 08.11.07
ジェラルド・ジェイ サスマン ジュリー サスマン ハロルド エイブルソン
ピアソンエデュケーション
売り上げランキング: 6542
ピアソンエデュケーション
売り上げランキング: 6542