Chinaunix首页 | 论坛 | 博客
  • 博客访问: 209246
  • 博文数量: 136
  • 博客积分: 2919
  • 博客等级: 少校
  • 技术积分: 1299
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-11 09:08
文章分类

全部博文(136)

文章存档

2013年(1)

2011年(135)

我的朋友

分类: Python/Ruby

2011-06-03 16:41:40

  1. ; Exercise 1.3. Define a procedure that takes three numbers as arguments
  2. ; and returns the sum of the squares of the two larger numbers.

  3. (define (square x) (* x x))
  4. (define (sum-of-squares x y) (+ (square x) (square y)))
  5. (define (sum-of-squares-largest-two x y z)
  6.   (cond ((and (>= x y) (>= y z)) (sum-of-squares x y))
  7.         ((and (>= x y) (>= z y)) (sum-of-squares x z))
  8.         ((and (>= y x) (>= x z)) (sum-of-squares y x))
  9.         ((and (>= y x) (>= z x)) (sum-of-squares y z))
  10.         ((and (>= z x) (>= x y)) (sum-of-squares z x))
  11.         ((and (>= z x) (>= y x)) (sum-of-squares z y))))

  12. ;; tests
  13. (sum-of-squares-largest-two 1 2 3)
  14. (sum-of-squares-largest-two 1 3 2)
  15. (sum-of-squares-largest-two 2 3 1)
  16. (sum-of-squares-largest-two 2 1 3)
  17. (sum-of-squares-largest-two 3 1 2)
  18. (sum-of-squares-largest-two 3 2 1)
阅读(561) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~