Algorithm 返回Racket ISL中数字列表中的最小元素?

Algorithm 返回Racket ISL中数字列表中的最小元素?,algorithm,list,racket,racket-student-languages,Algorithm,List,Racket,Racket Student Languages,我必须在racketisl中编写一个函数,它获取一个数字列表并返回列表中最小的数字。最小值和最大值都不允许。我想我从这里开始;显然,需要递归 最后,我将使用这个函数创建一个抽象函数 (check-expect (find-smallest (list 3 8 4 9 2 0 1)) 0) (check-expect (find-smallest (list 2 3 4 5 6)) 2) (check-expect (find-smallest (list 58 37 28 37 58 92 24

我必须在racketisl中编写一个函数,它获取一个数字列表并返回列表中最小的数字。最小值和最大值都不允许。我想我从这里开始;显然,需要递归

最后,我将使用这个函数创建一个抽象函数

(check-expect (find-smallest (list 3 8 4 9 2 0 1)) 0)
(check-expect (find-smallest (list 2 3 4 5 6)) 2)
(check-expect (find-smallest (list 58 37 28 37 58 92 24)) 24)
(check-expect (find-smallest (list 19 38 46 85 19 38 19)) 19)

;; find-smallest: list of numbers -> number
;; consumes a list of numbers and returns the
;; smallest number in the list
(define (find-smallest lon)
  (cond
    [(empty? (rest lon)) (first lon)]
    [(

看来你的基本情况不错。默认情况如下:您可以使用
查找最小值
查找列表其余部分中的最小值,并将其与第一个元素进行比较,例如,使用
还可以使用内部命名let循环和临时变量来存储最小值,以查找列表中的最小值:

(define (find-smallest l)
  (let loop ((l l)
             (sm (first l))) ; start with first of list as smallest
    (cond
      [(empty? l) sm]
      [(< sm (first l))
       (loop (rest l) sm)]
      [else
       (loop (rest l) (first l))]))) 
(定义(查找最小的l)
(let循环((l)
(sm(第一个l));以列表的第一个作为最小值开始
(续)
[(空?l)sm]
[(
哇,我没想到这么简单!非常感谢你!