scheme - 下面的方案代码有什么问题吗?

标签 scheme

这个函数有什么问题吗?

(define (get-two-largest a b c)
  (cond ((and (>= a b) (>= a c)) (if (> b c) (list a b) (list a c))))
  (cond ((and (>= b a) (>= b c)) (if (> a c) (list b a) (list b c))))
  (cond ((and (>= c a) (>= c b)) (if (> a b) (list c a) (list c b))))

当我按顺序传递参数 3 5 4 时,它不会返回任何内容。

最佳答案

如果只在其中放入一个分支,为什么要使用 cond

(define (get-two-largest a b c)
  (cond ((and (>= a b) (>= a c)) (if (> b c) (list a b) (list a c)))
        ((and (>= b a) (>= b c)) (if (> a c) (list b a) (list b c)))
        ((and (>= c a) (>= c b)) (if (> a b) (list c a) (list c b)))))

关于scheme - 下面的方案代码有什么问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7766289/

相关文章:

scheme - 为什么方案会找到一个用 "define"而不是 "let"定义的变量

lisp - Scheme 中是否有等同于 Lisp 的 "runtime"原语?

Scheme let bound 语句

scheme - 小Schemer eqlist?功能 - 替代版本?

functional-programming - Scheme - 遍历两个列表

scheme - 截断(受限) Racket 中的列表

scheme - 是否有一种速记方法来更新 Racket 中的特定结构字段?

scheme - 在 Scheme 中创建一个非常大的列表

scheme - 你会如何读取Scheme中的输入文件?

lambda - 使用 lambda Racket