[Teaching programming] Lesson two - lazyblog
It wasn't clear, and still isn't to me as of now, how S-exps are counted in a list, for example
;; how many are here? Is it 2?
(((a b c) d))
;; and here? Should be 3...
(a (((b c ))) d)
So another lesson just finished. This time we went through the second chapter where the authors introduce functions for the first time. And recursion.
The first example the reader finds is this:
(define lat?
(lambda (l)
(cond
((null? l) #t)
((atom? (car l)) (lat? (rest l)))
(else #f))))
There was an initial moment of shock when we moved from the usual one liners like (eq? (car '(1 2 3)) (car (car '((1 2) 3))))
to something that complicated; but it was just a moment, as I've proceeded to explain that
- there's no syntax we're not aware of
- all new expressions have names that are easy to understand or guess
- the same rules that we've seen until now still apply, and that's all there is to know
The book instructs the reader to treat cond
as a series of questions that one should ask oneself, so that the function
can be applied onto the arguments. She also went through the recursive step really easily, I proceeded to explain how l
shrinks down at each recursive step until one of the exit conditions is met.
One thing I've noticed about giving exercises for her to solve is that I find their purpose more fitting our situation if I find a reason for them to exist, if by doing them she's learning something, getting some insights we've missed during the explanation.
For example, I want to understand if she's got the general idea of function, which in my opinion is something that expresses a concept via an initial state and a final state. I want this to be gradual, as of now she's following along perfectly so I don't want to lose that. The first exercise could be
;; write the function 2nd that given a list of atoms
;; returns the second atom
;; return values for edege cases are up to you
(define 2nd
(lambda (lat)
;; ...
))
;; then write the results for these applications
(2dn '()) ;; ???
(2nd 'a) ;; ???
(2nd '(a b c)) ;; ???
(2nd '((a) b c) ;; ???
;; don't limit yourself to these inputs though, try to think about possible inputs your function
;; might receive: it should work with lists of atoms but it might happen that the input
;; is not what you expect
This exercise gives the chance to apply what we've seen lots of times before, but under a new light. By having a look at
the scenarios where she has to use 2nd
I believe she will get more insights on how to write the function.
The next and last exercise I want her to complete is about recursion, which was the other new concept of this chapter, even though the authors didn't explicitly went through that I think she understood everything well enough so we can talk about this.