When I was teaching myself programming, I found teachyourselfcs.com, which recommended some books for teaching yourself programming. The one I read was How to Design Programs.
I think the main idea of this textbook is the structure of the data informs the structure of the program, and so designing or understanding the data type is fundamental to program design.
As an example to demonstrate this point, consider a function that counts how many times a symbol appears in an S-expression (exercise 320). The data type is recursive by definition:
In the Lisp used in this textbook, the ;
starts a comment and '()
is the empty list. cons
constructs a list where the first argument becomes the list’s first element and the second argument, a list, becomes the rest of the list’s elements. These are accessed by the first
and rest
functions.
Here is my solution (the book encourages writing function signatures and unit testing):
The recursive logic of the function is essentially identical to the recursive structure of the data type. I thought that was really interesting, but to some this may be obvious.
My review on this book: it’s a buy (actually it’s free and you can read it at htdp.org)!
Attribution:
How to Design Programs, Second Edition
Matthias Felleisen, Robert Bruce Findler, Matthew Flatt, Shriram Krishnamurthi
MIT Press
ISBN: 9780262534802