My solutions to the exercises in Structure and Interpretation of Computer Programs.
Go to file
2021-04-18 20:42:04 -04:00
misc Start to work on 5.33 2021-04-15 12:41:52 -04:00
.gitignore Implement till 3.30 2020-12-23 11:04:55 -05:00
ex-1_01-10.scm Finish exercises for chapter 1 section 1 and 2 2020-10-15 20:38:28 -04:00
ex-1_11-20.scm Implement till 2.76 2020-11-17 13:33:55 -05:00
ex-1_21-28.scm Finish exercises for chapter 1 section 1 and 2 2020-10-15 20:38:28 -04:00
ex-1_29-34.scm Implement till 2.76 2020-11-17 13:33:55 -05:00
ex-1_35-39.scm Implement till 2.62 2020-11-15 11:30:56 -05:00
ex-1_40-46.scm Finish chapter 1 (rerun) 2020-10-15 21:14:22 -04:00
ex-2_01-06.scm Implement exercises till 2.6 2020-10-25 21:40:51 -04:00
ex-2_07-16.scm Finish 2.1 exercises 2020-10-28 10:57:13 -04:00
ex-2_17-23.scm Implement exercises till 2.23 2020-10-29 11:47:13 -04:00
ex-2_24-32.scm Implement till 2.76 2020-11-17 13:33:55 -05:00
ex-2_33-43.scm Implement till 4.44 2021-02-05 11:12:59 -05:00
ex-2_44-52.scm Implement till 3.30 2020-12-23 11:04:55 -05:00
ex-2_53-58.scm Add tests to 2.58 2020-11-13 11:38:23 -05:00
ex-2_59-66.scm Implement till 2.76 2020-11-17 13:33:55 -05:00
ex-2_67-72.scm Implement till 2.72 2020-11-16 12:36:19 -05:00
ex-2_73-76.scm Answer till 3.45 2020-12-29 14:00:47 -05:00
ex-2_77-97.scm Finish exercises for chapter 2 2020-12-08 21:39:02 -05:00
ex-3_01-08.scm Implement till 3.8 2020-12-14 08:45:16 -05:00
ex-3_09-11.scm Read till 3.11 2020-12-16 12:05:38 -05:00
ex-3_12-20.scm Implement till 3.22 2020-12-19 09:22:30 -05:00
ex-3_21-23.scm Fix delete rear and front pointer when there is only one element left 2020-12-21 15:16:32 -05:00
ex-3_24-27.scm Solve till 3.27 2020-12-22 10:42:40 -05:00
ex-3_28-32.scm Answer 3.32 2020-12-26 01:57:27 -05:00
ex-3_33-37.scm Implement till 3.37 2020-12-27 14:49:49 -05:00
ex-3_38-45.scm Answer till 3.45 2020-12-29 14:00:47 -05:00
ex-3_46-49.scm Finish 3.49 2020-12-31 16:38:21 -05:00
ex-3_50-62.scm Implement till 3.66 2021-01-05 09:44:02 -05:00
ex-3_63-72.scm Implement till 3.72 2021-01-07 14:48:38 -05:00
ex-3_73-82.scm Finish exercises for chapter 3 2021-01-14 07:35:46 -05:00
ex-4_01-10.scm Implement till 4.13 2021-01-22 08:20:46 -05:00
ex-4_11-21.scm Implement till 4.24 2021-01-25 08:42:19 -05:00
ex-4_22-24.scm Implement till 4.24 2021-01-25 08:42:19 -05:00
ex-4_25-30.scm Implement till 4.30 2021-01-27 10:08:51 -05:00
ex-4_31-34.scm Implement till 4.34 2021-01-30 11:54:26 -05:00
ex-4_35-44.scm Fix solution to liars-puzzle where my code yielded a wrong result previously 2021-02-17 20:08:48 -05:00
ex-4_45-54.scm Finish section 4.3 2021-02-27 19:17:07 -05:00
ex-4_55-60.scm Answer till 4.73 2021-03-05 12:17:25 -05:00
ex-4_61-69.scm Answer till 4.73 2021-03-05 12:17:25 -05:00
ex-4_70-77.scm Working on 4.78 2021-03-11 12:11:20 -05:00
ex-4_78-79.scm Finish 4.78 and skip 4.79 2021-03-16 20:25:41 -04:00
ex-5_01-06.scm Working on 5.11 2021-03-24 13:04:52 -04:00
ex-5_07-13.scm Implement till 5.14 2021-03-29 08:28:45 -04:00
ex-5_14-19.scm Implement 5.19 2021-03-31 21:16:22 -04:00
ex-5_20-22.scm Implement till 5.22 2021-04-02 12:11:38 -04:00
ex-5_23-30.scm Implement 5.30; on to last section; let's go! 2021-04-11 13:51:18 -04:00
ex-5_31-xx.scm Answer 5.37 2021-04-18 20:42:04 -04:00
LICENSE Initial commit 2020-10-12 02:45:43 +02:00
README.md Write summary chapter 2 2021-01-26 13:58:02 -05:00
run Implement up to 2.39 2020-10-30 20:15:03 -04:00
util.scm Implement till 4.38 2021-02-01 13:02:33 -05:00

SICP

These are my solutions to the CS classic Structure and Interpretation of Computer Programs. I have looked up the answer for some exercises on the Scheme Community Wiki. Such exercise have a mark in their respective script.

You can use the Scheme implementation by the MIT to run these scripts. In Arch, execute pacman -S mit-scheme to install it. Then run the scripts via mit-scheme --quiet < script.scm.

This is currently (2021/01/25) work in progress.

Chapter 1

The first chapter of SICP starts by explaining the Scheme syntax. The first couple of exercises are simple enough. However, already at 1.5, the book foreshadows some of the difficulty that is about to come.

(define (p) (p))
(define (test x y)
  (if (= x 0) 0 y))

The goal is to decide whether Scheme uses applicative-order-evaluation or normal-order-evaluation based on the above code. I have initially found the exercise confusing, but the code triggering an infinite loop is a clear indication of Scheme (or at least my version of Scheme, MIT Scheme) using applicative-order-evaluation.

After this exercise, things get more comfortable again. The book proceeds to introduce if-else clauses, conditionals, as well as recursion. The book uses these primitives to compare iterative and recursive procedures based on a couple of typical CS example functions such as computing Fibonacci numbers, greatest common divisor, and fast exponentiation.

Two new insights I had how using modulo instead of subtracting the divisor speeds up the GCD algorithm I learned in middle school and how exponentiation can run in O(log n) by halving even exponents.

I wasn't able to prove the Golden Ration exercise at the time of working through this chapter. My knowledge of induction and proofs was too limited. I found that depressing at the time, and I wish they hadn't included that exercise.

Nevertheless, the book moves on to further essential CS concepts such as Prime numbers and the Fermat primality test. Funnily enough, I used that probabilistic Prime test for a Project Euler exercise, wondering why I wasn't able to get the correct results. It turns out that this test detects probable primes (the book mentions that a little later and introduces the Miller-Rabin test that pseudoprimes cannot fool). On the one hand, it was cool to use an algorithm from a book directly. On the other hand, I was undoubtedly a bit annoyed by that story.

The book moves on to discuss the runtime of some of the algorithms discussed to this point. It introduces some other mathematical concepts, such as calculating roots via the fixed-point method, Euler expansions, and the Newton method for finding minima/maxima. It was cool to see how the fixed-point method can be used to implement the Newton method if you plug the derivate of a function into it. I did my project presentation for math in high school about the Newton method. So this brought up cool memories. I wish I still had that presentation.

Finally, SICP introduces the evaluation model for stateless functions and concludes with some exercises that require second-order procedures: procedures that take other procedures as arguments.

Chapter 2

Chapter 2 starts by introducing compound data structures to represent pairs and rational numbers. Abstraction barriers allow implementing procedures on data types independent of the underlying representation. For example, we could reduce a rational-number to its lowest denominator at creation or display time. The book introduces interval arithmetic to deepen the understanding of data abstractions.

Next, the book shows how to create more complex data structures such as lists and trees from cons. Higher-order procedures such as map and fold operate on these structures, for example, to update each element or to aggregate data.

The book then expands on the idea of higher-order procedures by introducing a picture language as shown in the following image. We can manipulate a painter with different transformations to create more complex images. The book does not present a way to paint to the screen, so I have implemented the painter to create a Python script that can then draw the images via the PIL library.

Corner Split

The next section introduces symbolic data that we utilize to implement a system for symbolic differentiation. One of my favorite things about the book is that it references concepts from other disciplines, such as calculus. I am happy that my high school knowledge of these topics is still present enough for me to work through the exercises.

Next, we explore sets and different ways to present them. The section finishes with the implementation of Huffman Encoding trees.

The rest of this chapter shows how to implement an algebra system utilizing a data-directed programming style. We create packages for different types of numbers, such as rational, complex, and imaginary numbers. We install methods for all basic algebraic operations, and the functions dispatch the correct procedure depending on the data type.

Over the next sections and many exercises, we expand the system to automatically simplify the numbers by creating a hierarchy of data types. Eventually, we extend the system to support polynomials and even rational polynomials by extending our previous rational numbers implementation.

I found these exercises challenging but incredibly rewarding. The algebra system was the point where I gave up when I worked through the book initially, so I felt a sense of accomplishment when I finished it on my second attempt.

Chapter 3