質問
SICPを介してスキームを学習しようとしています。演習1.3では、次のように読みます。引数として3つの数値を取り、2つの大きな数値の平方和を返す手続きを定義します。ソリューションを改善する方法についてコメントしてください。
(define (big x y)
(if (> x y) x y))
(define (p a b c)
(cond ((> a b) (+ (square a) (square (big b c))))
(else (+ (square b) (square (big a c))))))
解決
よろしいですか、改善したい具体的なものはありますか?
次のようなことができます:
(define (max2 . l)
(lambda ()
(let ((a (apply max l)))
(values a (apply max (remv a l))))))
(define (q a b c)
(call-with-values (max2 a b c)
(lambda (a b)
(+ (* a a) (* b b)))))
(define (skip-min . l)
(lambda ()
(apply values (remv (apply min l) l))))
(define (p a b c)
(call-with-values (skip-min a b c)
(lambda (a b)
(+ (* a a) (* b b)))))
そして、これ(proc p)は、任意の数の引数を処理するために簡単に変換できます。
他のヒント
本のその時点で提示された概念のみを使用して、私はそれを行います:
(define (square x) (* x x))
(define (sum-of-squares x y) (+ (square x) (square y)))
(define (min x y) (if (< x y) x y))
(define (max x y) (if (> x y) x y))
(define (sum-squares-2-biggest x y z)
(sum-of-squares (max x y) (max z (min x y))))
big
は max
と呼ばれます。標準ライブラリ機能がある場合はそれを使用します。
私のアプローチは異なります。多くのテストではなく、3つすべての2乗を加算し、次に最小の2乗を減算します。
(define (exercise1.3 a b c)
(let ((smallest (min a b c))
(square (lambda (x) (* x x))))
(+ (square a) (square b) (square c) (- (square smallest)))))
このアプローチを好むか、多数の if
テストを行うかは、もちろんあなた次第です。
SRFI 95 を使用した代替実装:
(define (exercise1.3 . args)
(let ((sorted (sort! args >))
(square (lambda (x) (* x x))))
(+ (square (car sorted)) (square (cadr sorted)))))
上記と同じですが、ワンライナーとして(synx @ freenode #schemeに感謝);また、 SRFI 1 および SRFI 26 :
(define (exercise1.3 . args)
(apply + (map! (cut expt <> 2) (take! (sort! args >) 2))))
組み込みの min
、 max
、および square
プロシージャを使用する次のコードで実行しました。それらは、その時点までにテキストで紹介されたものだけを使用して実装するのに十分簡単です。
(define (sum-of-highest-squares x y z)
(+ (square (max x y))
(square (max (min x y) z))))
このようなものはどうですか?
(define (p a b c)
(if (> a b)
(if (> b c)
(+ (square a) (square b))
(+ (square a) (square c)))
(if (> a c)
(+ (square a) (square b))
(+ (square b) (square c)))))
テキストのその時点までに導入された概念のみを使用して、かなり重要だと思う、ここに別の解決策があります:
(define (smallest-of-three a b c)
(if (< a b)
(if (< a c) a c)
(if (< b c) b c)))
(define (square a)
(* a a))
(define (sum-of-squares-largest a b c)
(+ (square a)
(square b)
(square c)
(- (square (smallest-of-three a b c)))))
(define (sum-sqr x y)
(+ (square x) (square y)))
(define (sum-squares-2-of-3 x y z)
(cond ((and (<= x y) (<= x z)) (sum-sqr y z))
((and (<= y x) (<= y z)) (sum-sqr x z))
((and (<= z x) (<= z y)) (sum-sqr x y))))
(define (f a b c)
(if (= a (min a b c))
(+ (* b b) (* c c))
(f b c a)))
Scott Hoffmanといくつかのircの助けを借りて、誤ったコードを修正しました。ここにあります
(define (p a b c)
(cond ((> a b)
(cond ((> b c)
(+ (square a) (square b)))
(else (+ (square a) (square c)))))
(else
(cond ((> a c)
(+ (square b) (square a))))
(+ (square b) (square c)))))
リストをソートして、ソートされたリストの最初と2番目の要素の正方形を追加することもできます。
(require (lib "list.ss")) ;; I use PLT Scheme
(define (exercise-1-3 a b c)
(let* [(sorted-list (sort (list a b c) >))
(x (first sorted-list))
(y (second sorted-list))]
(+ (* x x) (* y y))))
さらに別の方法があります:
#!/usr/bin/env mzscheme #lang scheme/load (module ex-1.3 scheme/base (define (ex-1.3 a b c) (let* ((square (lambda (x) (* x x))) (p (lambda (a b c) (+ (square a) (square (if (> b c) b c)))))) (if (> a b) (p a b c) (p b a c)))) (require scheme/contract) (provide/contract [ex-1.3 (-> number? number? number? number?)])) ;; tests (module ex-1.3/test scheme/base (require (planet "test.ss" ("schematics" "schemeunit.plt" 2)) (planet "text-ui.ss" ("schematics" "schemeunit.plt" 2))) (require 'ex-1.3) (test/text-ui (test-suite "ex-1.3" (test-equal? "1 2 3" (ex-1.3 1 2 3) 13) (test-equal? "2 1 3" (ex-1.3 2 1 3) 13) (test-equal? "2 1. 3.5" (ex-1.3 2 1. 3.5) 16.25) (test-equal? "-2 -10. 3.5" (ex-1.3 -2 -10. 3.5) 16.25) (test-exn "2+1i 0 0" exn:fail:contract? (lambda () (ex-1.3 2+1i 0 0))) (test-equal? "all equal" (ex-1.3 3 3 3) 18)))) (require 'ex-1.3/test)
例:
$ mzscheme ex-1.3.ss 6 success(es) 0 failure(s) 0 error(s) 6 test(s) run 0
他の人がこの問題をどのように解決したかを見るのは素晴らしいことです。これが私の解決策でした:
(define (isGreater? x y z)
(if (and (> x z) (> y z))
(+ (square x) (square y))
0))
(define (sumLarger x y z)
(if (= (isGreater? x y z) 0)
(sumLarger y z x)
(isGreater? x y z)))
反復で解決しましたが、私のバージョンでは、zが最小の数値であるため、ashitakaと(+(square(max xy))(square(max(min xy)z)))解がより良いです、より大きいですか?を2回呼び出して、不必要に遅くて遠回りの手順を作成します。
(define (sum a b) (+ a b))
(define (square a) (* a a))
(define (greater a b )
( if (< a b) b a))
(define (smaller a b )
( if (< a b) a b))
(define (sumOfSquare a b)
(sum (square a) (square b)))
(define (sumOfSquareOfGreaterNumbers a b c)
(sumOfSquare (greater a b) (greater (smaller a b) c)))
やってみました:
(define (procedure a b c)
(let ((y (sort (list a b c) >)) (square (lambda (x) (* x x))))
(+ (square (first y)) (square(second y)))))
;exercise 1.3
(define (sum-square-of-max a b c)
(+ (if (> a b) (* a a) (* b b))
(if (> b c) (* b b) (* c c))))
これが最小かつ最も効率的な方法だと思います:
(define (square-sum-larger a b c)
(+
(square (max a b))
(square (max (min a b) c))))
以下は、私が思いついた解決策です。コードが小さな関数に分解されると、ソリューションについて簡単に推論できます。
; Exercise 1.3
(define (sum-square-largest a b c)
(+ (square (greatest a b))
(square (greatest (least a b) c))))
(define (greatest a b)
(cond (( > a b) a)
(( < a b) b)))
(define (least a b)
(cond ((> a b) b)
((< a b) a)))
(define (square a)
(* a a))