algorithmlistracketracket-student-languages

Return smallest element in a list of numbers in Racket ISL?


I have to write a function in Racket ISL that takes a list of numbers and returns the smallest number in the list. Both min and max are not allowed. I think I have a start here; obviously recursion is needed.

Eventually I'll use this function to create an abstract one.

(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)]
    [(

Solution

  • It looks like your base case is good. You default case can be as follows: You use find-smallest to find the smallest of the rest of the list and compare this to the first element, eg. with <. The smallest should then be the result.