Continuing Work on SICP and Setting up Emacs
I'm continuing to work on Structure and Interpretation of Computer Programs, but I want to be able to apply the stuff I'm learning to real projects as soon as possible. I've decided to start learning Common Lisp after finishing the first chapter of SICP.
I thought I might as well post a quick guide to getting set up for the exercises in SICP (if you use Emacs).
Now, in any Racket file (use the .rkt file extension), you can use the SICP-compatable Scheme implementation by including
#lang sicp at the top of the file.
This works fine in DrRacket, but after a few exercises I began to miss Emacs features. Luckily, there's racket-mode for Emacs! I also recommend installing smartparens, and hideshow-org. Here are the settings I used in
(add-to-list 'load-path "~/.lisp/hideshow-org") (require 'hideshow-org) ;; make tab also do completion (setq tab-always-indent 'complete) ;; make code fold from the end of the line, as normally expected, ;; then return cursor to original position (defun toggle-fold () (interactive) (save-excursion (end-of-line) (hs-toggle-hiding))) (smartparens-global-mode t) (add-hook 'prog-mode-hook #'hs-org/minor-mode) (add-to-list 'auto-mode-alist '("\.rkt\'" . racket-mode))
Now I can use tab for completion, and for easy folding of the current code bracket (or, in this case, parentheses) in any code. Smartparens saves time by inserting a closing paren, bracket, brace or quote automatically, and the final line makes sure that the Racket REPL will be available whenever I open a
The only thing left is to learn the keymappings for racket-mode, which conveniently match the keyboard shortcuts for evaluating Elisp. To 'run' the racket file in the REPL, type C-c C-K, and to evaluate the symbol at point, type C-x C-o.
Note: I'm not posting all of my solutions; many are strongly analogous to solutions found elsewhere on the web.
(define (* a b) (if (= b 0) 0 (*-iter a b))) (define (*-iter a b) (cond ((= b 1) a) ((not (even? b)) (+ a (* a (- b 1)))) (else (*-iter (double a) (half b))))) (define (even? a) (= (/ a 2) 0)) (define (double a) (+ a a)) (define (half a) (/ a 2))
The answer to exercise 1.19 is technically only two lines of missing code from a program the book provides; I thought that the language of the question was obtuse enough to be almost infuriating. Refer to Tekkie's updated post on the problem for the best explanation.
(define (sum term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) next b)))) (define (inc a) (+ a 1)) (define (cube a) (* a a a) ) (define (integral f a b n) (define h (/ (- b a) n)) (define (y-of-k k) (f (+ a (* h k)))) (define (simpson-term k) (* (cond ((or (= 0 k) (= k n)) 1) ((even? k) 2) (else 4)) (y-of-k k))) (* (/ h 3) (sum simpson-term 0 inc n) )) (integral cube 0 1 1000)
(define (sum term a next b) (define (iter a result) (if (= a b) (+ result (term a)) (iter (next a) (+ result (term a))))) (iter a 0) ) (define (square a) (* a a)) (define (inc a) (+ a 1)) (sum square 0 inc 100)