Any way to add let and let* to Beginning Student language

I like to teach DRY (Don't Repeat Yourself) fairly early, and some of my students have already asked for a way to name values inside functions.

Is there a fairly easy way to allow students to use let and let* inside the Beginning Student language that will work both in my lab and is easy for students to make available on their home machines?

For a problem like writing a function for Heron's Formula, this would allow:

;; heron: number number number -> number
;; consumes: side lengths a, b, and c of a triangle
;; produces: the triangle's area
(define (heron a b c)
  (let 
        ([s (/ (+ a b c) 2)])
    (sqrt (* s (- s a) (- s b) (- s c))))

(check-expect (heron 3 4 5) 6)

which is much nicer than if you have to either write a helper function for the semi-perimeter or write it out each time.

Hi @ToddOBryan sorry this post took so long to appear. It somehow triggered the spam filter. I’ll make sure it doesn’t to you again!
Stephen

1 Like
#lang htdp/bsl

; heron: number number number -> number
;; consumes: side lengths a, b, and c of a triangle
;; produces: the triangle's area
#;
(define (heron a b c)
  (let ([s (/ (+ a b c) 2)])
    (sqrt (* s (- s a) (- s b) (- s c)))))

(define (heron-1 a b c)
  (heron/s a b c (/ (+ a b c) 2)))

(define (heron/s a b c s)
  (sqrt (* s (- s a) (- s b) (- s c))))
  
(check-expect (heron-1 3 4 5) 6)

BSL was intentionally designed to avoid locally named values.

Teach very introductory program design should also convey
a model of computation and pre-algebra is all that HtDP assumes.
Problem is many students never really understood what function
composition (and other concepts) is and what it is useful for:
breaking problems down.

So for a problem like this I push function composition. See above.

My intention is to help such students actually understand composition
by having a notation that forces it. Most won’t be sw devs in the end,
but understanding composition/breaking problems down will hopefully
come across.

ISL comes relatively soon and I then use some of these examples
to introduce local (or let) to say PL/Program Design feed each other
so PL designers make people’s life simpler with some features, e.g.,
local names.

I can live with this. The idea of having to write a function, even if it's a helper function, where one of the parameters is computable from the others does strike me as a code smell, but we can talk about how to make it better when we get to ISL.

The unfortunate thing is that when you're teaching at the high school level with a wide range of abilities, ISL takes an awful long time to get to.

And I totally agree about function composition. That theater owner example from HtDP really drives it home to students. I make them figure out several examples and then we write the big, messy function. Then we talk about how to break it down into smaller, easier to comprehend functions. (So the opposite order of how the text presents it.) I put the two presentations side-by-side on the board and ask students, "The first one is shorter and is only one function, but... If I were to ask you to enter one of these into the definitions window and click Run and if it works, you get $100; if there's an error, you pay me $100. Which one would you try?" Universally (except for the smart alecs), they pick the decomposed functions because they're so short and easy to understand.

We keep hitting that theme over and over again. One of my students who didn't take HtDP with me, but did take AP Computer Science was nice enough to say about 15 years later when he joined my team at Workday that in his first class he'd learned Java, and then in my class, he'd learned how to program. I credit HtDP with most of the insights that I was able to impart.

On Nov 8, 2024, at 7:20 PM, Todd O'Bryan writes "one of the parameters is computable from the others does strike me as a code smell”.

Yes, but keep in mind that some of the early part of teaching reinforces basic math skills, which are as important as coding skills.

On Nov 8, 2024, at 7:20 PM, Todd O'Bryan writes "That theater owner example from HtDP really drives it home to students” and continues with "hey pick the decomposed functions because they're so short and easy to understand.”

The key is that they are not only short but we can test each of them individually, increasing our confidence that when we compose them, we have good chance of getting the overall function right. Conversely, if something goes wrong, we can break down the failed test case into pieces that test these small functions, and this will identify the failing building block. The coolest thing is that this idea scales to 10Kloc programs that compose many different components statically, dynamically, or even remotely. (That’s the sequences of courses we teach here .. still).

On Nov 8, 2024, at 7:20 PM, Todd O'Bryan writes "he'd learned Java, and then in my class, he'd learned how to program.”

As Mike S. wrote years ago (see my web page), "Wir sind froh, dass die Absolventen schon Java können. Programmieren müssen wir denen halt noch beibringen.” Now think of what companies pay to do this .. when colleges already pretend to take tuition in exchange for teaching programming.