我是新手,我试图找到5个数字集的最大值和最小值,并将它们作为列表返回。 我正在计划使用一个函数来查找最大值和一个函数来查找最小值并将结果放在一个列表中。(可以在下面的代码中看到)但我继续得到错误,我似乎无法了解什么是错的。我们将不胜感激。
#lang pl
( : min&max : Number Number Number Number Number -> (Listof Number))
(define (min&max x y z w v)
(define newlist '(x y z w v))
(list (maxOf newlist) (minOf newlist)))
( : maxOf : (Listof Number) -> Number)
(define (maxOf list)
(cond
((null? list) (error "empty list"))
((null? (rest list)) (first list))
(else (cond
((> (first list) (maxOf (rest list))) (first list))
(else (maxOf (rest list)))))))
( : minOf : (Listof Number) -> Number)
(define (minOf list)
(cond
((null? list) (error "empty list"))
((null? (rest list)) (first list))
(else (cond
((< (first list) (minOf (rest list))) (first list))
(else (minOf (rest list)))))))
我得到的错误是:
Type Checker: type mismatch
expected: (Listof Number)
given: (List 'x 'y 'z 'w 'v) in: newlist
Type Checker: type mismatch
expected: (Listof Number)
given: (List 'x 'y 'z 'w 'v) in: newlist
Type Checker: type mismatch
expected: (Listof Number)
given: (List Number Number) in: (list (maxOf newlist) (minOf newlist))
Type Checker: could not apply function;
wrong number of arguments provided
expected at least: 2
given: 1 in: (error "empty list")
Type Checker: type mismatch
expected: Symbol
given: String in: "empty list"
Type Checker: could not apply function;
wrong number of arguments provided
expected at least: 2
given: 1 in: (error "empty list")
Type Checker: type mismatch
expected: Symbol
given: String in: "empty list"
Type Checker: Summary: 7 errors encountered in:
newlist
newlist
(list (maxOf newlist) (minOf newlist))
(error "empty list")
"empty list"
(error "empty list")
"empty list"
我正在试图把我得到的所有5个数字放在一个列表中并称之为newlinst,这就是(定义新列表'(x y z w v)) 但我一直在犯错误。 加上我继续讨论(list(maxOf newlist)(minOf newlist))) 这意味着将max和min值放在一个列表中并返回它 并为(错误“空列表”))
答案 0 :(得分:0)
编写一个函数max2
,找到最多2个数字,然后最多5个数字可以计算为
(max2 a (max2 b (max2 c (max2 d e)))).