Dracket:在没有内置功能的情况下在列表中找到最高值
Drracket: Finding highest value in a list without in built function
我所在的 class 这个作业让我很困惑。我能够使大部分功能正常工作,但这一部分使我无法完成它。我有两个函数,这是我需要的,一个是在列表中找到最高数值 (largest-num),另一个是查找列表并引用它 (max-lon)。我收到以下错误 largest-num: expects only 1 argument, but found 2 我不太确定如何修复它。列出代码:
(define (max-lon a-lon)
(local [
(define ACUMM (void))
(define lst (void))
;; lon number → number
;; Purpose: Return max(max of given list, given number)
;; Accumulator Invariant: accum = maximum in L - lst
(define (max-helper)
(if(empty? lst)
ACUMM
(begin
(set! ACUMM (largest-num (first lst) ACUMM))
(set! lst (rest lst))
(max-helper))))]
(if (empty? a-lon)
(error 'max-lon "An empty lon does not have a maximum.")
(begin
(set! ACUMM(first a-lon))
(set! lst (rest a-lon))
(max-helper)))))
(define (largest-num lst)
(cond
[(empty? (rest lst)) (first lst)]
[(< (first lst) (first (rest lst))) (largest-num (rest lst))]
[else (largest-num (cons (first lst) (rest (rest lst))))]))
任何建议都很好,谢谢
largest-num 可以简化很多,因为它所要做的就是比较两个数字,即 ACUMM,即列表中的 accumulator/place,第一个 lst 是拥有最高数字的数字,而不是而不是处理我最初假设的整个列表,这里是固定代码:
(define (max-lon a-lon)
(local [
(define ACUMM (void))
(define lst (void))
(define (max-helper)
(if(empty? lst)
ACUMM
(begin
(set! ACUMM (largest? ACUMM (first lst)))
(set! lst (rest lst))
(max-helper))))]
(if (empty? a-lon)
(error 'max-lon "An empty lon does not have a maximum.")
(begin
(set! ACUMM(first a-lon))
(set! lst (rest a-lon))
(max-helper)))))
(define (largest? a b)
(if (> a b) a b))
(define (largest-num lst (acc -inf.0))
(cond ((empty? lst) acc)
((and acc (> (car lst) acc)) (largest-num (cdr lst) (car lst)))
(else (largest-num (cdr lst) acc))))
我所在的 class 这个作业让我很困惑。我能够使大部分功能正常工作,但这一部分使我无法完成它。我有两个函数,这是我需要的,一个是在列表中找到最高数值 (largest-num),另一个是查找列表并引用它 (max-lon)。我收到以下错误 largest-num: expects only 1 argument, but found 2 我不太确定如何修复它。列出代码:
(define (max-lon a-lon)
(local [
(define ACUMM (void))
(define lst (void))
;; lon number → number
;; Purpose: Return max(max of given list, given number)
;; Accumulator Invariant: accum = maximum in L - lst
(define (max-helper)
(if(empty? lst)
ACUMM
(begin
(set! ACUMM (largest-num (first lst) ACUMM))
(set! lst (rest lst))
(max-helper))))]
(if (empty? a-lon)
(error 'max-lon "An empty lon does not have a maximum.")
(begin
(set! ACUMM(first a-lon))
(set! lst (rest a-lon))
(max-helper)))))
(define (largest-num lst)
(cond
[(empty? (rest lst)) (first lst)]
[(< (first lst) (first (rest lst))) (largest-num (rest lst))]
[else (largest-num (cons (first lst) (rest (rest lst))))]))
任何建议都很好,谢谢
largest-num 可以简化很多,因为它所要做的就是比较两个数字,即 ACUMM,即列表中的 accumulator/place,第一个 lst 是拥有最高数字的数字,而不是而不是处理我最初假设的整个列表,这里是固定代码:
(define (max-lon a-lon)
(local [
(define ACUMM (void))
(define lst (void))
(define (max-helper)
(if(empty? lst)
ACUMM
(begin
(set! ACUMM (largest? ACUMM (first lst)))
(set! lst (rest lst))
(max-helper))))]
(if (empty? a-lon)
(error 'max-lon "An empty lon does not have a maximum.")
(begin
(set! ACUMM(first a-lon))
(set! lst (rest a-lon))
(max-helper)))))
(define (largest? a b)
(if (> a b) a b))
(define (largest-num lst (acc -inf.0))
(cond ((empty? lst) acc)
((and acc (> (car lst) acc)) (largest-num (cdr lst) (car lst)))
(else (largest-num (cdr lst) acc))))