0

Fig. 65 in "How to Design Programs" is as follows:

; Nelon -> Number
; determines the smallest number on l
(define (inf l)
  (cond
    [(empty? (rest l)) (first l)]
    [else
     (local ((define smallest-in-rest (inf (rest l))))
       (cond
         [(< (first l) smallest-in-rest) (first l)]
         [else smallest-in-rest]))]))

Can somebody explain how variable smallest-in-rest works. I get recursion in a function but a variable has me confused

1 Answer 1

1

It's just a shorthand (longhand ;-)) for the following:

(let ((smallest-in-rest (inf (rest l))))
  (cond
    [(< (first l) smallest-in-rest) (first l)]
    [else smallest-in-rest]))

The let should make it clear that we're just storing the result of the (inf (rest l)) so that it only has to be written once in the code, rather than once for each branch of the cond.

Sign up to request clarification or add additional context in comments.

3 Comments

Wonder why racket goes to such lengths to teach students something completely different than their main language. It's like teaching JavaScript by first teaching them Python..
The HtDP curriculum is not about learning Racket (or any language). As the first line of the book says, any idiot can pick up any language in a few days. HtDP teaches how to program, a significantly harder undertaking. It uses a series of streamlined beginner languages to do so, which allows students to focus on what's important, and not on idiosyncrasies of syntax (think public static void main).
As someone who learned local first, it made more sense to me over let, because we could reuse the same syntax for definitions that we had already learned. We spent less time learning about syntax that way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.