我正在尝试通过SICP学习计划。练习1.3内容如下:定义一个过程,该过程将三个数字作为参数,并返回两个较大数字的平方和。请评论我如何改进我的解决方案。
(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))))))
答案 0 :(得分:28)
只使用本书那一点提出的概念,我会这样做:
(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))))
答案 1 :(得分:13)
big
被称为max
。当它存在时使用标准库功能。
我的方法不同。我只是添加所有三个的正方形,然后减去最小的正方形,而不是大量的测试。
(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))))
答案 2 :(得分:10)
我使用以下代码完成了该操作,该代码使用内置的min
,max
和square
过程。它们很简单,只能使用到目前为止在文本中引入的内容来实现。
(define (sum-of-highest-squares x y z)
(+ (square (max x y))
(square (max (min x y) z))))
答案 3 :(得分:9)
这样的事情怎么样?
(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)))))
答案 4 :(得分:4)
(define (f a b c)
(if (= a (min a b c))
(+ (* b b) (* c c))
(f b c a)))
答案 5 :(得分:4)
(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))))
答案 6 :(得分:4)
仅使用在文本的那一点介绍的概念,我认为这是非常重要的,这是一个不同的解决方案:
(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)))))
答案 7 :(得分:4)
对我来说没问题,你想改进什么具体的事情吗?
您可以执行以下操作:
(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)可以很容易地转换为处理任意数量的参数。
答案 8 :(得分:3)
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)))))
答案 9 :(得分:2)
这是另一种方法:
#!/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
答案 10 :(得分: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))))
答案 11 :(得分:2)
很高兴看到其他人如何解决了这个问题。这是我的解决方案:
(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)))
我通过迭代解决了问题,但我更喜欢ashitaka的解决方案和[+(square(max xy)z)(square(max(min xy)z)))解决方案更好,因为在我的版本中,如果z是最小数, 更伟大?被两次调用,创建了不必要的缓慢而and回的过程。
答案 12 :(得分:1)
(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)))
答案 13 :(得分:0)
我已经去了:
(define (procedure a b c)
(let ((y (sort (list a b c) >)) (square (lambda (x) (* x x))))
(+ (square (first y)) (square(second y)))))
答案 14 :(得分:0)
;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))))
答案 15 :(得分:0)
我认为这是最小巧,最有效的方式:
(define (square-sum-larger a b c)
(+
(square (max a b))
(square (max (min a b) c))))
答案 16 :(得分:0)
以下是我提出的解决方案。当代码被分解为小函数时,我发现更容易推理出解决方案。
; 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))