【发布时间】:2019-12-08 08:36:38
【问题描述】:
我正在尝试缩放列表中的所有值,其中最大值为 1,最小值为 0。这是我尝试完成的示例
(check-expect (squash (list 100 90 70 20)) (list 1 0.875 0.625 0)).
如您所见,最大值为 100,因此缩放值为 1,最小值为 20 被缩小为 0。
为了缩小所有这些值,我正在执行计算 Z=(Li - (smallest-val)) / ((largest-val) - (smallest-val))
其中 Li 是列表中的所有值 (L1, L2, L3 ...)
这是我目前的代码
(define (squash L)
(local
[
;;(largest-val M) returns the largest value M in L
;;(largest-val: (listof Num) -> Num
(define (largest-val M) (foldr max (first M) (rest M)))
;;(smallest-val x) returns the smallest value x in L
;;(smallest-val: (listof Num) -> Num
(define (smallest-val x) (foldr min (first x) (rest x)))
]
(cond
[(empty? L)'()]
[else (cons (/ (- (first L) smallest-val) (- largest-val smallest-val))
(squash (rest L)))])))
这是我遇到的错误
:: -: 需要一个数字作为第二个参数,给定 (lambda (a1) ...)
我不确定如何修复此代码以使我的程序正常运行
我想使用命令式编程范例来保留我的解决方案,因此我希望我的答案保持与现在相同的格式。
【问题讨论】:
-
抱歉,我似乎忘记在(第一个 L)之前添加“-”符号,我会查看我的问题并编辑我的问题。
标签: scheme racket imperative