Teaching - when should `local` be introduced?

Does it matter when local variables are introduced? (For reference, HtDP refers to local variables in III.16.2 Local Definitions.)

One pro to waiting so long to introduce local definitions is that students are encouraged to think of "helper functions" that have a simple purpose. This is definitely a push toward good design habits. I am concerned that if I introduce local variables early, functions will balloon in size (ignoring the importance of having a "single responsibility").

One con is that they can have difficulty seeing the need for decomposing a problem with helper functions, and sometimes if the orgnization is incorrect it's difficult to complete.

The motivating example I have in mind is a function that places a dot at a random location on the screen, colored depending on the coordinates. Students that begin with (place-image ... (random WIDTH) (random HEIGHT) ...) are off track and have a hard time recovering.

There is a design process for this ("write and test a non-random version of the function first"), but like many things in teaching there needs to be a match between an approach and a student's level of receptivity to that approach.

I am curious about the thoughts of people experienced with either early local variables or avoiding local variables. What are the tradeoffs here?

At the college level, we work through BSL/BSL+ in about four weeks.

Your pro observation is correct. It should force students to recognize
that problems must be divided so that they can be conquered.

Part of the learning is to make mistakes and to see how to backtrack.
In your example, I might ask the student

β€” So the random numbers are chosen. 
β€” But, the `place-image` function is fixed, you can’t modify it. 
β€” What could you do instead? 

Assuming that students have been exposed to helper functions before,
I’d hope a student would say

β€” oh I could write `(my-place-image …)` 
β€” and define `my-place-image` as a helper function. 

For young or inexperienced students I might just formulate this last part
as a hint.

The ordering of them kind of varies(there's also a german version of htdp that I havent looked into yet)

HTDP book:

  1. Basics, bools, strings, numbers, design recipe
  2. Structs
  3. Lists
  4. ListOfStructs
  5. Map, filter, Fold, Abstract Sigs
  6. Local and then Lambda
  7. BSTs

University of British Colombia(UBC 110):

  1. Basics, bools, strings, numbers, design recipe
  2. Structs
  3. Lists
  4. ListOfStructs
  5. BSTs
  6. Mutual Referential Data
  7. Simultaneous List Processing
  8. Local
  9. Map, filter, Fold, Abstract Sigs

Uwaterloo CS115

  1. Basics, bools, strings, numbers, design recipe
  2. Lists (mapping, filtering, and folding)
  3. Structural Recursion with Lists(covers Natural Numbers as Recursive types, Sorting, Dictionaries)
  4. Simultaneous Recursion(2 recursive types as inputs)
  5. Local
  6. Higher order functions
  7. Structs(yes, this is for some reason dead last)

As far as I know, they use lists to represent structs and just write wrappers like:

; Person is (list Name Age)
(define (person-name ls) (first ls))

Indiana 211

Lecture 11: More points (Sunday, September 25, 11:59pm)
Lecture 12: Unlimited data (Tuesday, September 27, 11:59pm)
Lecture 13: More self-reference (Sunday, October 2, 11:59pm)
Lecture 14: Built-in structures (Tuesday, October 4, 11:59pm)
Lecture 15: Space invaders (Sunday, October 9, 11:59pm)
Lecture 16: Abstraction (Tuesday, October 11, 11:59pm)
Lecture 17: Local definitions (Sunday, October 16, 11:59pm)
Lecture 18: Built-in abstractions (Tuesday, October 18, 11:59pm)
Lecture 19: Follow the template (Sunday, October 23, 11:59pm)
Lecture 20: Mutual recursion (Tuesday, October 25, 11:59pm)
Lecture 21: Simultaneous processing (Sunday, October 30, 11:59pm)

Northeastern fundies 2500

https://www.ccs.neu.edu/home/nderbinsky/fundies1/m5.html
(Get the vids while you still can before they get removed/privated)

  1. Base types, structs, etc
  2. Structural recursion
  3. Abstraction with map, filter, fold
  4. Local
  5. Mutually Referential Data
  6. Graphs

Conclusion

Based on these 5 course table of contents(book included), we have:

  • Introduce map/filter/fold and then local (3)(the book, UWaterloo, Northeastern)
  • Introduce local and then map/filter/fold(2) (UBC, Indiana)

UBC 110 introduces it wayy later on, which is advantageous in that students can go backtrack to fix the classic performance problem of max-numbers having exponential blow up because of not using local. He demonstrated it multiple times since he did binarytrees, mutual recursive types(modeling filesystem) without local, he makes it an exercise to fix them with local

For my own course:

I recall I largely follow the books order in that I introduce map/filter/fold abstraction and then local because the two sections focused on reducing repetition(which was partially how I explained abstraction).
I was really tempted to follow UBC 110s route, but I didn't want to teach local wayyy after BSTs and Mutually Referential Data like they did because the earlier I introduce it, the quicker they can keep an eye on things to factor repetition out of and the more videos I can make showing the instinct of "ok, I typed this 2-4x now, I should probably store it"

As for the balance of delaying local so students get into a good habit of splitting up their tasks, I didn't think about it too much? local is kinda cumbersome to write and I try to rigorously enforce starting your work based off copying and pasting from the "function template" step of htdp

1 Like

I think your categorization is a little off because it doesn't consider enough things. The issue is how to order:
A. Abstraction and higher-order functions as a concept
B. Lexical scope (local)
C. The abstractions built-in to ISL (map etc)
D. Anonymous functions (lambda)

The book orders them A,C,B+D, which I think what Northeastern does. Indiana does A, B+D, C. UBC/Waterloo do B first, before A (I think).