[Teaching programming] Lesson six
(define insert-middle (lambda (new lat) (cons (car lat) (cons new (cdr lat)))))
We've been experimenting with functions, so we're starting to move away from the list manipulation tasks for their own sake.
"Giving names to pieces of code and reading them in plain english helps identifying functions that could be extracted and ultimately lead to more readable code.", this is how I a) tried to explain why we use functions, and b) started to introduce the concept of readability.
These are some of the functions we wrote, the fact that we passed a function as argument felt natural to her, I didn't introduce it as a big deal, in fact it isn't!
(define insert-flexible (lambda (new old lat funx) (cond ((null? lat) '()) ((eq? (car lat) old) (funx new old lat)) (else (cons (car lat) (insert-flexible new old (cdr lat) funx)))))) (define insert-after (lambda (new old lat) (cons (car lat) (cons new (cdr lat))))) (define insert-before (lambda (new old lat) (cons new lat))) (insert-flexible 'and 'brioche '(alaska brioche matisse) insert-after) (insert-flexible 'and 'brioche '(alaska brioche matisse) insert-before)
What comes next?
Luckily the book we're using will greatly help us along the way, also because the next chapters from 5 onwards are going to switch gears, but slowly so. As I said to her I think the biggest leaps have already been done, from now it's just walking.