15 November 2024 |
I've just finished my first read through of Simply Scheme by Brian Harvey and Matthew Wright. It was a phenomenal book and my intro to programming proper. We'll go through some of the stuff I learned and hopefully it'll excite you enough to pick it up.
I picked up this book because I want to go through Structure and Interpretation of Computer Programs (SICP) and I heard it's a good introduction to programming in Scheme. I was not disappointed. By the end of the book you're implementing a spreadsheet and a database program! I must confess that I didn't go through those two exercises because it was a bit much for me. But it's all there.
I went through all the previous exercises however. And they did not disappoint. Especially the section on lists, that's where it got a bit complicated for me and that's what I was looking for. A proper introduction to programming in Scheme. You go through it all, even tree recursion. Here is an example exercise from the book
Write depth, a procedure that takes a tree as argument and returns the largest number of nodes connected through parent-child links. That ism a leaf node has depth 1; a tree in which all the children of the root node are leaves has depth 2. Our world tree has depth 4 (because the longest path from the root to a leaf is, for example, world, country, state, city).
(define (make-node datum children) │ (cons datum children)) │ (define (datum node) │ (car node)) │ (define (children node) │ (cdr node)) │ (define world-tree │ (make-node │ 'world │ (list (make-node │ │ 'italy │ │ (list (make-node 'venezia '()) │ │ │ │ (make-node 'riomaggiore '()) │ │ │ │ (make-node 'firenze '()) │ │ │ │ (make-node 'roma '()))) │ │ (make-node │ │ │ '(united states) │ │ │ (list (make-node 'california │ │ │ │ (list (make-node 'berkeley '()) │ │ │ │ │ │ │ │ (make-node '(san francisco) '()) │ │ │ │ │ │ │ │ (make-node 'gilroy '()))) │ │ │ │ (make-node 'massachusetts │ │ │ │ │ (list (make-node 'cambridge '()) │ │ │ │ │ │ │ │ (make-node 'amherst '()) │ │ │ │ │ │ │ │ (make-node 'sudbury '())))))))) │ │ │ │ │ │ │ │ (define (depth node) │ (cond ((null? (children node)) 1) ; base case: if node is a leaf │ │ (else ; otherwise node has children │ │ │(+ 1 ; add one │ │ │ (apply max ; to the maximum of │ │ │ │ (map depth (children node))))))) ; the recursive call │ │ │ │ (depth world-tree)
4
The best way to go through this book is with Emacs hands down. With org-mode you can launch an org-noter session and take notes as you go and have an interactive learning experience. You can also fire up a repl for an even more interactive session. Don't forget org-babel with geiser for evaluation in your org files. You can even blog about it from those same org files with org-export and use either haunt or hugo as your static sit generator!
Nonetheless, I can't recommend this book enough if you're interested in Scheme. It'll be the best intro you've ever had and quite the challenge. It has taken me almost a year to go through the book with differing levels of intensity over time. I've learned so much and my programming has improved by leaps and bounds. I feel more than ready for SICP.
Read the book and enjoy!