├── .gitignore ├── LICENSE.md ├── README.md ├── project.clj ├── resources ├── code │ ├── 1.1.1.1.clj │ ├── 1.1.1.2.clj │ ├── 1.1.1.3.clj │ ├── 1.1.1.4.clj │ ├── 1.1.1.5.clj │ ├── 1.1.1.6.clj │ ├── 1.1.1.7.clj │ ├── 1.1.2.1.clj │ ├── 1.1.2.2.clj │ ├── 1.1.2.3.clj │ ├── 1.1.3.1.clj │ ├── 1.1.4.1.clj │ ├── 1.1.4.2.clj │ ├── 1.1.4.3.clj │ ├── 1.1.4.4.clj │ ├── 1.1.4.5.clj │ ├── 1.1.4.6.clj │ ├── 1.1.4.7.clj │ ├── 1.1.5.1.clj │ ├── 1.1.5.10.clj │ ├── 1.1.5.11.clj │ ├── 1.1.5.2.clj │ ├── 1.1.5.3.clj │ ├── 1.1.5.4.clj │ ├── 1.1.5.5.clj │ ├── 1.1.5.6.clj │ ├── 1.1.5.7.clj │ ├── 1.1.5.8.clj │ ├── 1.1.5.9.clj │ ├── 1.1.6.1.clj │ ├── 1.1.6.10.clj │ ├── 1.1.6.11.clj │ ├── 1.1.6.12.clj │ ├── 1.1.6.13.clj │ ├── 1.1.6.14.clj │ ├── 1.1.6.15.clj │ ├── 1.1.6.2.clj │ ├── 1.1.6.3.clj │ ├── 1.1.6.4.clj │ ├── 1.1.6.5.clj │ ├── 1.1.6.6.clj │ ├── 1.1.6.7.clj │ ├── 1.1.6.8.clj │ ├── 1.1.6.9.clj │ ├── 1.1.7.1.clj │ ├── 1.1.7.10.clj │ ├── 1.1.7.11.clj │ ├── 1.1.7.2.clj │ ├── 1.1.7.3.clj │ ├── 1.1.7.4.clj │ ├── 1.1.7.5.clj │ ├── 1.1.7.6.clj │ ├── 1.1.7.7.clj │ ├── 1.1.7.8.clj │ ├── 1.1.7.9.clj │ ├── 1.1.8.1.clj │ ├── 1.1.8.2.clj │ ├── 1.1.8.3.clj │ ├── 1.1.8.4.clj │ ├── 1.1.8.5.clj │ ├── 1.1.8.6.clj │ ├── 1.2.1.1.clj │ ├── 1.2.1.2.clj │ ├── 1.2.1.3.clj │ ├── 1.2.1.4.clj │ ├── 1.2.1.5.clj │ ├── 1.2.1.6.clj │ ├── 1.2.1.7.clj │ ├── 1.2.1.8.clj │ ├── 1.2.2.1.clj │ ├── 1.2.2.2.clj │ ├── 1.2.2.3.clj │ ├── 1.2.2.4.clj │ ├── 1.2.3.1.clj │ ├── 1.2.4.1.clj │ ├── 1.2.4.2.clj │ ├── 1.2.4.3.clj │ ├── 1.2.4.4.clj │ ├── 1.2.4.5.clj │ ├── 1.2.5.1.clj │ ├── 1.2.6.1.clj │ ├── 1.2.6.2.clj │ ├── 1.2.6.3.clj │ ├── 1.2.6.4.clj │ ├── 1.2.6.5.clj │ ├── 1.2.6.6.clj │ ├── 1.2.6.7.clj │ ├── 1.2.6.8.clj │ ├── 1.2.6.9.clj │ ├── 1.3.1.1.clj │ ├── 1.3.1.10.clj │ ├── 1.3.1.11.clj │ ├── 1.3.1.12.clj │ ├── 1.3.1.13.clj │ ├── 1.3.1.14.clj │ ├── 1.3.1.2.clj │ ├── 1.3.1.3.clj │ ├── 1.3.1.4.clj │ ├── 1.3.1.5.clj │ ├── 1.3.1.6.clj │ ├── 1.3.1.7.clj │ ├── 1.3.1.8.clj │ ├── 1.3.1.9.clj │ ├── 1.3.1.clj │ ├── 1.3.2.1.clj │ ├── 1.3.2.10.clj │ ├── 1.3.2.11.clj │ ├── 1.3.2.12.clj │ ├── 1.3.2.13.clj │ ├── 1.3.2.14.clj │ ├── 1.3.2.15.clj │ ├── 1.3.2.16.clj │ ├── 1.3.2.17.clj │ ├── 1.3.2.18.clj │ ├── 1.3.2.19.clj │ ├── 1.3.2.2.clj │ ├── 1.3.2.3.clj │ ├── 1.3.2.4.clj │ ├── 1.3.2.5.clj │ ├── 1.3.2.6.clj │ ├── 1.3.2.7.clj │ ├── 1.3.2.8.clj │ ├── 1.3.2.9.clj │ ├── 1.3.2.clj │ ├── 1.3.3.1.clj │ ├── 1.3.3.10.clj │ ├── 1.3.3.11.clj │ ├── 1.3.3.2.clj │ ├── 1.3.3.3.clj │ ├── 1.3.3.4.clj │ ├── 1.3.3.5.clj │ ├── 1.3.3.6.clj │ ├── 1.3.3.7.clj │ ├── 1.3.3.8.clj │ ├── 1.3.3.9.clj │ ├── 1.3.4.1.clj │ ├── 1.3.4.10.clj │ ├── 1.3.4.11.clj │ ├── 1.3.4.12.clj │ ├── 1.3.4.13.clj │ ├── 1.3.4.14.clj │ ├── 1.3.4.15.clj │ ├── 1.3.4.2.clj │ ├── 1.3.4.3.clj │ ├── 1.3.4.4.clj │ ├── 1.3.4.5.clj │ ├── 1.3.4.6.clj │ ├── 1.3.4.7.clj │ ├── 1.3.4.8.clj │ ├── 1.3.4.9.clj │ ├── 2.1.1.1.clj │ ├── 2.1.1.2.clj │ ├── 2.1.1.3.clj │ ├── 2.1.1.4.clj │ ├── 2.1.1.5.clj │ ├── 2.1.1.6.clj │ ├── 2.1.1.7.clj │ ├── 2.1.1.8.clj │ ├── 2.1.1.9.clj │ ├── 2.1.2.1.clj │ ├── 2.1.2.2.clj │ ├── 2.1.3.1.clj │ ├── 2.1.3.2.clj │ ├── 2.1.3.3.clj │ ├── 2.1.3.4.clj │ ├── 2.1.4.1.clj │ ├── 2.1.4.2.clj │ ├── 2.1.4.3.clj │ ├── 2.1.4.4.clj │ ├── 2.1.4.5.clj │ ├── 2.1.4.6.clj │ ├── 2.1.4.7.clj │ ├── 2.1.clj │ ├── 2.2.1.1.clj │ ├── 2.2.1.10.clj │ ├── 2.2.1.11.clj │ ├── 2.2.1.12.clj │ ├── 2.2.1.13.clj │ ├── 2.2.1.14.clj │ ├── 2.2.1.15.clj │ ├── 2.2.1.16.clj │ ├── 2.2.1.17.clj │ ├── 2.2.1.18.clj │ ├── 2.2.1.19.clj │ ├── 2.2.1.2.clj │ ├── 2.2.1.20.clj │ ├── 2.2.1.21.clj │ ├── 2.2.1.22.clj │ ├── 2.2.1.23.clj │ ├── 2.2.1.24.clj │ ├── 2.2.1.25.clj │ ├── 2.2.1.26.clj │ ├── 2.2.1.27.clj │ ├── 2.2.1.28.clj │ ├── 2.2.1.3.clj │ ├── 2.2.1.4.clj │ ├── 2.2.1.5.clj │ ├── 2.2.1.6.clj │ ├── 2.2.1.7.clj │ ├── 2.2.1.8.clj │ ├── 2.2.1.9.clj │ ├── 2.2.2.1.clj │ ├── 2.2.2.10.clj │ ├── 2.2.2.11.clj │ ├── 2.2.2.12.clj │ ├── 2.2.2.13.clj │ ├── 2.2.2.14.clj │ ├── 2.2.2.15.clj │ ├── 2.2.2.16.clj │ ├── 2.2.2.2.clj │ ├── 2.2.2.3.clj │ ├── 2.2.2.4.clj │ ├── 2.2.2.5.clj │ ├── 2.2.2.6.clj │ ├── 2.2.2.7.clj │ ├── 2.2.2.8.clj │ ├── 2.2.2.9.clj │ ├── 2.2.3.1.clj │ ├── 2.2.3.10.clj │ ├── 2.2.3.11.clj │ ├── 2.2.3.12.clj │ ├── 2.2.3.13.clj │ ├── 2.2.3.14.clj │ ├── 2.2.3.15.clj │ ├── 2.2.3.16.clj │ ├── 2.2.3.17.clj │ ├── 2.2.3.18.clj │ ├── 2.2.3.19.clj │ ├── 2.2.3.2.clj │ ├── 2.2.3.20.clj │ ├── 2.2.3.21.clj │ ├── 2.2.3.22.clj │ ├── 2.2.3.23.clj │ ├── 2.2.3.24.clj │ ├── 2.2.3.25.clj │ ├── 2.2.3.26.clj │ ├── 2.2.3.27.clj │ ├── 2.2.3.28.clj │ ├── 2.2.3.29.clj │ ├── 2.2.3.3.clj │ ├── 2.2.3.30.clj │ ├── 2.2.3.31.clj │ ├── 2.2.3.32.clj │ ├── 2.2.3.4.clj │ ├── 2.2.3.5.clj │ ├── 2.2.3.6.clj │ ├── 2.2.3.7.clj │ ├── 2.2.3.8.clj │ ├── 2.2.3.9.clj │ ├── 2.2.4.1.clj │ ├── 2.2.4.10.clj │ ├── 2.2.4.11.clj │ ├── 2.2.4.12.clj │ ├── 2.2.4.13.clj │ ├── 2.2.4.14.clj │ ├── 2.2.4.15.clj │ ├── 2.2.4.16.clj │ ├── 2.2.4.17.clj │ ├── 2.2.4.18.clj │ ├── 2.2.4.19.clj │ ├── 2.2.4.2.clj │ ├── 2.2.4.20.clj │ ├── 2.2.4.21.clj │ ├── 2.2.4.22.clj │ ├── 2.2.4.3.clj │ ├── 2.2.4.4.clj │ ├── 2.2.4.5.clj │ ├── 2.2.4.6.clj │ ├── 2.2.4.7.clj │ ├── 2.2.4.8.clj │ ├── 2.2.4.9.clj │ ├── 2.2.clj │ ├── 2.3.1.1.clj │ ├── 2.3.1.10.clj │ ├── 2.3.1.11.clj │ ├── 2.3.1.2.clj │ ├── 2.3.1.3.clj │ ├── 2.3.1.4.clj │ ├── 2.3.1.5.clj │ ├── 2.3.1.6.clj │ ├── 2.3.1.7.clj │ ├── 2.3.1.8.clj │ ├── 2.3.1.9.clj │ ├── 2.3.2.1.clj │ ├── 2.3.2.10.clj │ ├── 2.3.2.11.clj │ ├── 2.3.2.2.clj │ ├── 2.3.2.3.clj │ ├── 2.3.2.4.clj │ ├── 2.3.2.5.clj │ ├── 2.3.2.6.clj │ ├── 2.3.2.7.clj │ ├── 2.3.2.8.clj │ └── 2.3.2.9.clj ├── static │ ├── css │ │ ├── _grid.scss │ │ ├── _solarized.scss │ │ ├── custom.scss │ │ └── style.css │ ├── img │ │ ├── favicon.ico │ │ ├── fig1.1.png │ │ ├── fig1.2.png │ │ ├── fig1.5.png │ │ ├── fig2.1.png │ │ ├── fig2.2.png │ │ ├── fig2.3.png │ │ ├── fig2.4.png │ │ ├── fig2.5.png │ │ ├── fig2.6.png │ │ ├── licensebadge-dark.png │ │ ├── licensebadge-light.png │ │ └── licensebadge.png │ └── js │ │ ├── analytics.js │ │ ├── colorscheme.js │ │ ├── highlight.pack.js │ │ ├── jquery.js │ │ ├── main.js │ │ ├── mathjax.js │ │ ├── modernizr.js │ │ └── require.js └── text │ ├── 1.md │ ├── 10.md │ ├── 11.md │ ├── 12.md │ ├── 13.md │ ├── 14.md │ ├── 15.md │ ├── 16.md │ ├── 17.md │ ├── 18.md │ ├── 19.md │ ├── 2.md │ ├── 20.md │ ├── 21.md │ ├── 22.md │ ├── 23.md │ ├── 24.md │ ├── 25.md │ ├── 26.md │ ├── 27.md │ ├── 28.md │ ├── 29.md │ ├── 3.md │ ├── 30.md │ ├── 31.md │ ├── 32.md │ ├── 33.md │ ├── 34.md │ ├── 35.md │ ├── 36.md │ ├── 37.md │ ├── 38.md │ ├── 4.md │ ├── 5.md │ ├── 6.md │ ├── 7.md │ ├── 8.md │ ├── 9.md │ └── about.md ├── src └── sicpclojure │ ├── colorscheme.cljs │ ├── config.clj │ ├── core.clj │ ├── link-regex.clj │ └── templates │ ├── 404.clj │ ├── about.clj │ ├── base.clj │ ├── contents.clj │ ├── cover.clj │ └── page.clj └── test └── sicpclojure └── core_test.clj /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /lib 3 | /classes 4 | /checkouts 5 | /static/css/.sass-cache 6 | pom.xml 7 | *.jar 8 | *.class 9 | *.swp 10 | *.swo 11 | .lein-deps-sum 12 | .lein-failures 13 | .lein-plugins 14 | .lein-repl-history 15 | .DS_Store 16 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (C) 2013 E.C. Mendenhall 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software 4 | and associated documentation files (the "Software"), to deal in the Software without restriction, 5 | including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 6 | and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 7 | subject to the following conditions: 8 | 9 | The above copyright notice and this permission notice shall be included in all copies or substantial 10 | portions of the Software. 11 | 12 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 13 | LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 14 | NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 15 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 16 | OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #SICP in Clojure 2 | 3 | ##About 4 | This is a version of the classic [_Structure and Interpretation of Computer Programs_](http://mitpress.mit.edu/sicp/) 5 | by Hal Abelson and Jerry & Julie Sussman, revised with code examples in [Clojure](http://clojure.org) instead 6 | of the original [Scheme](http://en.wikipedia.org/wiki/Scheme_\(programming_language\)). 7 | 8 | ##Read 9 | If you'd just like to read the book, [visit the site here](http://ecmendenhall.github.com/sicpclojure). 10 | 11 | ##Contribute 12 | The [code samples](https://github.com/ecmendenhall/sicpclojure/tree/master/resources/code) and 13 | [chapter text](https://github.com/ecmendenhall/sicpclojure/tree/master/resources/text) are available 14 | in this repository, so you can contribute to the project yourself. If you see an example that could be improved, 15 | want to add a footnote or aside about Clojure, or want to jump in and edit one of the uncompleted chapters, 16 | please make a fix and submit a pull request. 17 | 18 | ###Editing guidelines 19 | The source text is in the `resources/text` directory, with filenames corresponding to the pages of the original book's 20 | [website](http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-1.html). These files are all in standard 21 | [markdown](http://daringfireball.net/projects/markdown/), with three extra elements: 22 | 23 | **Inline footnotes** are indicated by `[^fn-#]`, where `#` is the footnote number in the original text. The 24 | corresponding anchor at the bottom of the page is indicated by `[fn-#]`: 25 | 26 | ``` 27 | We will use block structure extensively to help us break up large programs 28 | into tractable pieces.[^fn-28] 29 | 30 | [fn-28] Embedded definitions must come first in a procedure body. The 31 | management is not responsible for the consequences of running programs that 32 | intertwine definition and use. 33 | ``` 34 | 35 | **Code blocks** are represented by sequential filenames inside double curly braces. For example: 36 | 37 | ``` 38 | A second advantage of prefix notation is that it extends in a straightforward 39 | way to allow combinations to be _nested_, that is, to have combinations whose 40 | elements are themselves combinations: 41 | 42 | {{ 1.1.1.5.clj }} 43 | 44 | There is no limit (in principle) to the depth of such nesting and to the 45 | overall complexity of the expressions that the Lisp interpreter can evaluate. 46 | It is we humans who get confused by still relatively simple expressions such 47 | as 48 | 49 | {{ 1.1.1.6.clj }} 50 | 51 | which the interpreter would readily evaluate to be 57. 52 | ``` 53 | 54 | These correspond to `.clj` files in `resources/code`. Code blocks are labeled sequentially according to their 55 | location in the text, e.g., the third figure in section 1.1.7 is `1.1.7.3.clj`. 56 | 57 | **Math** uses [MathJax](http://www.mathjax.org/) syntax, which includes most of your favorite 58 | [LaTex symbols](http://docs.mathjax.org/en/latest/tex.html#supported-latex-commands). Blocks of math are wrapped 59 | in `\\[` and `\\]`: 60 | 61 | ``` 62 | In general, the Fibonacci numbers can be defined by the rule 63 | 64 | \\[ \text{Fib}(n) = 65 | \begin{cases} 66 | 0 & \text {if } n = 0 \\\\ 67 | 1 & \text{if } n = 1 \\\\ 68 | \text{Fib}(n-1) + \text{Fib}(n-2) & \text{otherwise} 69 | \end{cases} \\] 70 | ``` 71 | 72 | Inline expressions are wrapped in `\\(` and `\\)`: 73 | 74 | ``` 75 | **Exercise 1.13.** Prove that _Fib_(_n_) is the closest integer to \\( \phi^{n} / \sqrt{5} \\), 76 | where \\( \phi = (1 + \sqrt{5})/2 \\) Hint: Let \\( \psi= (1 - \sqrt{5}) / {2} \\). 77 | ``` 78 | 79 | Be careful! You'll need to escape any characters that are also markdown symbols with a backslash. 80 | The most common are the underscore ("k sub one" is thus `\\( k\_{1} \\)`), and the double-backslash, 81 | used to separate rows in a matrix or system of equations (so `\\` becomes `\\\\`). 82 | I usually use this MathJax [test site](http://advisors-online.com/tex_field.html) for reference, then 83 | add the extra escape characters. 84 | 85 | Finally, figures are stored in `resources/static/img/`, with their original labels from the source text. I've been 86 | using `.png` images drawn up with Google Docs, but make your own however you'd like. Text is in 87 | [Ubuntu Mono](http://font.ubuntu.com/) and colors are from the [Solarized](http://ethanschoonover.com/solarized) 88 | color scheme. Transparent backgrounds work best. 89 | 90 | 91 | ###Previewing your edits 92 | I haven't packaged this as a library, and probably won't, but if you want to check out your extra-escaped backslashes 93 | or simply admire your handiwork, it's pretty easy to run it through the site generator from the REPL: 94 | 95 | ``` 96 | git clone git://github.com/ecmendenhall/sicpclojure.git 97 | cd sicpclojure 98 | lein deps 99 | lein repl 100 | ``` 101 | 102 | Open `src/sicpclojure/config.clj` and add the pages you'd like to preview to the `:complete` vector: 103 | ``` 104 | (def build {:path-to-code "resources/code/" ; Path to code excerpts. 105 | :path-to-text "resources/text/" ; Path to chapter text. 106 | :deploy-directory "deploy/" 107 | :ignore #{".scss"} ; Ignore these filetypes when deploying. 108 | :complete [9 10 11 12]}) ; Vector of completed pages. <--- This one! 109 | ``` 110 | 111 | Slurp `src/sicpclojure/core.clj` into your REPL, then: 112 | ``` 113 | (dev-server) 114 | ``` 115 | This will generate the page and start a simple Jetty server. Your edits should now be available as beautiful 116 | full-color HTML via localhost:3000. 117 | 118 | ##License 119 | The SICP text is licensed under a Creative Commons [BY-SA 3.0](http://creativecommons.org/licenses/by-sa/3.0/) license 120 | (so the source text in this repository is too). My code is released under an MIT license. If you would like to read 121 | one, see the file [LICENSE.md](https://github.com/ecmendenhall/sicpclojure/blob/master/LICENSE.md)! 122 | 123 | This site also uses [Requirejs](http://requirejs.org/), [MathJax](http://www.mathjax.org/), 124 | [Highlight.js](http://softwaremaniacs.org/soft/highlight/en/), [Google Web Fonts](http://www.google.com/webfonts), 125 | and the [Solarized](http://ethanschoonover.com/solarized) [sass](http://sass-lang.com/) template, plus 126 | the [pegdown](https://github.com/sirthias/pegdown) markdown processor, [Hiccup](https://github.com/weavejester/hiccup) 127 | templates, and the [Hickory](https://github.com/davidsantiago/hickory) HTML to Hiccup parser. 128 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject sicpclojure "0.1.0-SNAPSHOT" 2 | :description "A static site generator for SICP in Clojure." 3 | :url "http://github.com/ecmendenhall" 4 | :dependencies [[org.clojure/clojure "1.4.0"] 5 | [org.pegdown/pegdown "1.2.0"] 6 | [hickory "0.2.3"] 7 | [hiccup "1.0.2"] 8 | [fs "1.3.2"] 9 | [watchtower "0.1.1"] 10 | [compojure "1.1.5"] 11 | [ring/ring-core "1.1.8"] 12 | [ring/ring-jetty-adapter "1.1.8"] 13 | [jayq "2.3.0"]] 14 | :plugins [[lein-cljsbuild "0.3.0"]] 15 | :cljsbuild { 16 | :builds [{ 17 | :source-paths ["src/sicpclojure"] 18 | :compiler { 19 | :output-to "resources/static/js/colorscheme.js" 20 | :optimizations :whitespace 21 | :externs ["resources/static/js/jquery.js"] 22 | :pretty-print true}}]}) 23 | -------------------------------------------------------------------------------- /resources/code/1.1.1.1.clj: -------------------------------------------------------------------------------- 1 | user=> 486 2 | -------------------------------------------------------------------------------- /resources/code/1.1.1.2.clj: -------------------------------------------------------------------------------- 1 | 486 2 | -------------------------------------------------------------------------------- /resources/code/1.1.1.3.clj: -------------------------------------------------------------------------------- 1 | (+ 137 349) 2 | 486 3 | (- 1000 334) 4 | 666 5 | (* 5 99) 6 | 495 7 | (/ 10 5) 8 | 2 9 | (+ 2.7 10) 10 | 12.7 11 | -------------------------------------------------------------------------------- /resources/code/1.1.1.4.clj: -------------------------------------------------------------------------------- 1 | (+ 21 35 12 7) 2 | 75 3 | 4 | (* 25 4 12) 5 | 1200 6 | -------------------------------------------------------------------------------- /resources/code/1.1.1.5.clj: -------------------------------------------------------------------------------- 1 | (+ (* 3 5) (- 10 6)) 2 | 19 3 | -------------------------------------------------------------------------------- /resources/code/1.1.1.6.clj: -------------------------------------------------------------------------------- 1 | (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6)) 2 | -------------------------------------------------------------------------------- /resources/code/1.1.1.7.clj: -------------------------------------------------------------------------------- 1 | (+ (* 3 2 | (+ (* 2 4) 3 | (+ 3 5))) 4 | (+ (- 10 7) 5 | 6)) 6 | -------------------------------------------------------------------------------- /resources/code/1.1.2.1.clj: -------------------------------------------------------------------------------- 1 | (def size 2) 2 | -------------------------------------------------------------------------------- /resources/code/1.1.2.2.clj: -------------------------------------------------------------------------------- 1 | size 2 | 2 3 | (* 5 size) 4 | 10 5 | -------------------------------------------------------------------------------- /resources/code/1.1.2.3.clj: -------------------------------------------------------------------------------- 1 | (def pi 3.14159) 2 | (def radius 10) 3 | (* pi (* radius radius)) 4 | 314.159 5 | (def circumference (* 2 pi radius)) 6 | circumference 7 | 62.8318 8 | -------------------------------------------------------------------------------- /resources/code/1.1.3.1.clj: -------------------------------------------------------------------------------- 1 | (* (+ 2 (* 4 6)) 2 | (+ 3 5 7)) 3 | -------------------------------------------------------------------------------- /resources/code/1.1.4.1.clj: -------------------------------------------------------------------------------- 1 | (defn square [x] (* x x)) 2 | -------------------------------------------------------------------------------- /resources/code/1.1.4.2.clj: -------------------------------------------------------------------------------- 1 | (defn square [x] (* x x)) 2 | ; | | | | | | 3 | ; To square something, multiply it by itself. 4 | -------------------------------------------------------------------------------- /resources/code/1.1.4.3.clj: -------------------------------------------------------------------------------- 1 | (defn <-name-> [<-formal parameters->] <-body->) 2 | -------------------------------------------------------------------------------- /resources/code/1.1.4.4.clj: -------------------------------------------------------------------------------- 1 | (square 21) 2 | 441 3 | 4 | (square (+ 2 5)) 5 | 49 6 | 7 | (square (square 3)) 8 | 81 9 | -------------------------------------------------------------------------------- /resources/code/1.1.4.5.clj: -------------------------------------------------------------------------------- 1 | (+ (square x) (square y)) 2 | -------------------------------------------------------------------------------- /resources/code/1.1.4.6.clj: -------------------------------------------------------------------------------- 1 | (defn sum-of-squares [x y] 2 | (+ (square x) (square y))) 3 | 4 | (sum-of-squares 3 4) 5 | 25 6 | -------------------------------------------------------------------------------- /resources/code/1.1.4.7.clj: -------------------------------------------------------------------------------- 1 | (defn f [a] 2 | (sum-of-squares (+ a 1) (* a 2))) 3 | 4 | (f 5) 5 | 136 6 | -------------------------------------------------------------------------------- /resources/code/1.1.5.1.clj: -------------------------------------------------------------------------------- 1 | (f 5) 2 | -------------------------------------------------------------------------------- /resources/code/1.1.5.10.clj: -------------------------------------------------------------------------------- 1 | (+ (* 6 6) (* 10 10)) 2 | 3 | (+ 36 100) 4 | 5 | 136 6 | -------------------------------------------------------------------------------- /resources/code/1.1.5.11.clj: -------------------------------------------------------------------------------- 1 | (* x x) 2 | -------------------------------------------------------------------------------- /resources/code/1.1.5.2.clj: -------------------------------------------------------------------------------- 1 | (sum-of-squares (+ a 1) (* a 2)) 2 | -------------------------------------------------------------------------------- /resources/code/1.1.5.3.clj: -------------------------------------------------------------------------------- 1 | (sum-of-squares (+ 5 1) (* 5 2)) 2 | -------------------------------------------------------------------------------- /resources/code/1.1.5.4.clj: -------------------------------------------------------------------------------- 1 | (+ (square 6) (square 10)) 2 | -------------------------------------------------------------------------------- /resources/code/1.1.5.5.clj: -------------------------------------------------------------------------------- 1 | (+ (* 6 6) (* 10 10)) 2 | -------------------------------------------------------------------------------- /resources/code/1.1.5.6.clj: -------------------------------------------------------------------------------- 1 | (+ 36 100) 2 | -------------------------------------------------------------------------------- /resources/code/1.1.5.7.clj: -------------------------------------------------------------------------------- 1 | 136 2 | -------------------------------------------------------------------------------- /resources/code/1.1.5.8.clj: -------------------------------------------------------------------------------- 1 | (f 5) 2 | -------------------------------------------------------------------------------- /resources/code/1.1.5.9.clj: -------------------------------------------------------------------------------- 1 | (sum-of-squares (+ 5 1) (* 5 2)) 2 | 3 | (+ (square (+ 5 1)) (square (* 5 2))) 4 | 5 | (+ (* (+ 5 1) (+ 5 1)) (* (* 5 2) (* 5 2))) 6 | -------------------------------------------------------------------------------- /resources/code/1.1.6.1.clj: -------------------------------------------------------------------------------- 1 | (defn abs [x] 2 | (cond (> x 0) x 3 | (= x 0) 0 4 | (< x 0) (-x))) 5 | -------------------------------------------------------------------------------- /resources/code/1.1.6.10.clj: -------------------------------------------------------------------------------- 1 | (defn >= [x y] 2 | (or (> x y) (= x y))) 3 | -------------------------------------------------------------------------------- /resources/code/1.1.6.11.clj: -------------------------------------------------------------------------------- 1 | (defn >= [x y] 2 | (not (< x y))) 3 | -------------------------------------------------------------------------------- /resources/code/1.1.6.12.clj: -------------------------------------------------------------------------------- 1 | 10 2 | (+ 5 3 4) 3 | (- 9 1) 4 | (/ 6 2) 5 | (+ (* 2 4) (- 4 6)) 6 | (def a 3) 7 | (def b (+ a 1)) 8 | (+ a b (* a b)) 9 | (= a b) 10 | (if (and (> b a) (< b (* a b))) 11 | b 12 | a) 13 | (cond (= a 4) 6 14 | (= b 4) (+ 6 7 a) 15 | :else 25) 16 | (+ 2 (if (> b a) b a)) 17 | (* (cond (> a b) a 18 | (< a b) b 19 | :else -1) 20 | (+ a 1)) 21 | -------------------------------------------------------------------------------- /resources/code/1.1.6.13.clj: -------------------------------------------------------------------------------- 1 | (defn a-plus-abs-b [a b] 2 | ((if (> b 0) + -) a b)) 3 | -------------------------------------------------------------------------------- /resources/code/1.1.6.14.clj: -------------------------------------------------------------------------------- 1 | (defn p [] (p)) 2 | 3 | (defn test [x y] 4 | (if (= x 0) 5 | 0 6 | y)) 7 | -------------------------------------------------------------------------------- /resources/code/1.1.6.15.clj: -------------------------------------------------------------------------------- 1 | (test 0 (p)) 2 | -------------------------------------------------------------------------------- /resources/code/1.1.6.2.clj: -------------------------------------------------------------------------------- 1 | (cond (<-p1->) <-e1-> 2 | (<-p2->) <-e2-> 3 | ... 4 | :else <-en->) 5 | -------------------------------------------------------------------------------- /resources/code/1.1.6.3.clj: -------------------------------------------------------------------------------- 1 | (defn abs [x] 2 | (cond (< x 0) (- x) 3 | :else x)) 4 | -------------------------------------------------------------------------------- /resources/code/1.1.6.4.clj: -------------------------------------------------------------------------------- 1 | (defn abs [x] 2 | (if (< x 0) 3 | (- x) 4 | x)) 5 | -------------------------------------------------------------------------------- /resources/code/1.1.6.5.clj: -------------------------------------------------------------------------------- 1 | (if <-predicate-> <-consequent-> <-alternative->) 2 | -------------------------------------------------------------------------------- /resources/code/1.1.6.6.clj: -------------------------------------------------------------------------------- 1 | (and <-e1-> ... <-en->) 2 | -------------------------------------------------------------------------------- /resources/code/1.1.6.7.clj: -------------------------------------------------------------------------------- 1 | (or <-e1-> ... <-en->) 2 | -------------------------------------------------------------------------------- /resources/code/1.1.6.8.clj: -------------------------------------------------------------------------------- 1 | (not <-e->) 2 | -------------------------------------------------------------------------------- /resources/code/1.1.6.9.clj: -------------------------------------------------------------------------------- 1 | (and (> x 5) (< x 10)) 2 | -------------------------------------------------------------------------------- /resources/code/1.1.7.1.clj: -------------------------------------------------------------------------------- 1 | (defn sqrt [x] 2 | (the y (and (>= y 0) 3 | (= (square y) x)))) 4 | -------------------------------------------------------------------------------- /resources/code/1.1.7.10.clj: -------------------------------------------------------------------------------- 1 | (new-if (= 2 3) 0 5) 2 | 5 3 | 4 | (new-if (= 1 1) 0 5) 5 | 0 6 | -------------------------------------------------------------------------------- /resources/code/1.1.7.11.clj: -------------------------------------------------------------------------------- 1 | (defn sqrt-iter [guess x] 2 | (new-if (good-enough? guess x) 3 | guess 4 | (sqrt-iter (improve guess x) 5 | x))) 6 | -------------------------------------------------------------------------------- /resources/code/1.1.7.2.clj: -------------------------------------------------------------------------------- 1 | " 2 | Guess Quotient Average 3 | 1 (2/1) = 2 ((2 + 1)/2) = 1.5 4 | 1.5 (2/1.5) = 1.3333 ((1.3333 + 1.5)/2) = 1.4167 5 | 1.4167 (2/1.4167) = 1.4118 ((1.4167 + 1.4118)/2) = 1.4142 6 | 1.4142 [...] [...] 7 | " 8 | 9 | -------------------------------------------------------------------------------- /resources/code/1.1.7.3.clj: -------------------------------------------------------------------------------- 1 | (defn sqrt-iter [guess x] 2 | (if (good-enough? guess x) 3 | guess 4 | (sqrt-iter (improve guess x) 5 | x))) 6 | -------------------------------------------------------------------------------- /resources/code/1.1.7.4.clj: -------------------------------------------------------------------------------- 1 | (defn improve [guess x] 2 | (average guess (/ x guess))) 3 | -------------------------------------------------------------------------------- /resources/code/1.1.7.5.clj: -------------------------------------------------------------------------------- 1 | (defn average [x y] 2 | (/ (+ x y) 2)) 3 | -------------------------------------------------------------------------------- /resources/code/1.1.7.6.clj: -------------------------------------------------------------------------------- 1 | (defn good-enough? [guess x] 2 | (< (abs (- (square guess) x)) 0.001)) 3 | -------------------------------------------------------------------------------- /resources/code/1.1.7.7.clj: -------------------------------------------------------------------------------- 1 | (defn sqrt [x] 2 | (sqrt-iter 1.0 x)) 3 | -------------------------------------------------------------------------------- /resources/code/1.1.7.8.clj: -------------------------------------------------------------------------------- 1 | (sqrt 9) 2 | ;; 3.00009155413138 3 | (sqrt (+ 100 37)) 4 | ;; 11.704699917758145 5 | (sqrt (+ (sqrt 2) (sqrt 3))) 6 | ;; 1.7739279023207892 7 | (square (sqrt 1000)) 8 | ;; 1000.000369924366 9 | -------------------------------------------------------------------------------- /resources/code/1.1.7.9.clj: -------------------------------------------------------------------------------- 1 | (defn new-if [predicate then-clause else-clause] 2 | (cond (predicate) then-clause 3 | :else else-clause)) 4 | -------------------------------------------------------------------------------- /resources/code/1.1.8.1.clj: -------------------------------------------------------------------------------- 1 | (defn square [x] (* x x)) 2 | 3 | (defn square [x] 4 | (Math/exp (double-it (Math/log x)))) 5 | 6 | (defn double-it [x] (+ x x)) 7 | -------------------------------------------------------------------------------- /resources/code/1.1.8.2.clj: -------------------------------------------------------------------------------- 1 | (defn square [x] (* x x)) 2 | 3 | (defn square [y] (* y y)) 4 | -------------------------------------------------------------------------------- /resources/code/1.1.8.3.clj: -------------------------------------------------------------------------------- 1 | (defn good-enough? [guess x] 2 | (< (abs (- (square guess) x)) 0.001)) 3 | -------------------------------------------------------------------------------- /resources/code/1.1.8.4.clj: -------------------------------------------------------------------------------- 1 | (defn sqrt [x] 2 | (sqrt-iter 1.0 x)) 3 | (defn sqrt-iter [guess x] 4 | (if (good-enough? guess x) 5 | guess 6 | (sqrt-iter (improve guess x) x))) 7 | (defn good-enough? [guess x] 8 | (< (abs (- (square guess) x)) 0.001)) 9 | (defn improve [guess x] 10 | (average guess (/ x guess))) 11 | -------------------------------------------------------------------------------- /resources/code/1.1.8.5.clj: -------------------------------------------------------------------------------- 1 | (defn sqrt [x] 2 | (defn good-enough? [guess x] 3 | (< (abs (- (square guess) x)) 0.001)) 4 | (defn improve [guess x] 5 | (average guess (/ x guess))) 6 | (defn sqrt-iter [guess x] 7 | (if (good-enough? guess x) 8 | guess 9 | (sqrt-iter (improve guess x) x))) 10 | (sqrt-iter 1.0 x)) 11 | -------------------------------------------------------------------------------- /resources/code/1.1.8.6.clj: -------------------------------------------------------------------------------- 1 | (defn sqrt [x] 2 | (defn good-enough? [guess] 3 | (< (abs (- (square guess) x)) 0.001)) 4 | (defn improve [guess] 5 | (average guess (/ x guess))) 6 | (defn sqrt-iter [guess] 7 | (if (good-enough? guess) 8 | guess 9 | (sqrt-iter (improve guess)))) 10 | (sqrt-iter 1.0)) 11 | -------------------------------------------------------------------------------- /resources/code/1.2.1.1.clj: -------------------------------------------------------------------------------- 1 | (factorial 6) 2 | (* 6 (factorial 5)) 3 | (* 6 (* 5 (factorial 4))) 4 | (* 6 (* 5 (* 4 (factorial 3)))) 5 | (* 6 (* 5 (* 4 (* 3 (factorial 2))))) 6 | (* 6 (* 5 (* 4 (* 3 (* 2 (factorial 1)))))) 7 | (* 6 (* 5 (* 4 (* 3 (* 2 1))))) 8 | (* 6 (* 5 (* 4 (* 3 2)))) 9 | (* 6 (* 5 (* 4 6))) 10 | (* 6 (* 5 24)) 11 | (* 6 120) 12 | 720 13 | -------------------------------------------------------------------------------- /resources/code/1.2.1.2.clj: -------------------------------------------------------------------------------- 1 | (defn factorial [n] 2 | (if (= n 1) 3 | 1 4 | (* n (factorial (- n 1))))) 5 | -------------------------------------------------------------------------------- /resources/code/1.2.1.3.clj: -------------------------------------------------------------------------------- 1 | (factorial 6) 2 | (fact-iter 1 1 6) 3 | (fact-iter 1 2 6) 4 | (fact-iter 2 3 6) 5 | (fact-iter 6 4 6) 6 | (fact-iter 24 5 6) 7 | (fact-iter 120 6 6) 8 | (fact-iter 720 7 6) 9 | 720 10 | -------------------------------------------------------------------------------- /resources/code/1.2.1.4.clj: -------------------------------------------------------------------------------- 1 | (defn factorial [n] 2 | (fact-iter 1 1 n)) 3 | 4 | (defn fact-iter [product counter max-count] 5 | (if (> counter max-count) 6 | product 7 | (fact-iter (* counter product) 8 | (+ counter 1) 9 | max-count))) 10 | 11 | -------------------------------------------------------------------------------- /resources/code/1.2.1.5.clj: -------------------------------------------------------------------------------- 1 | (defn + [a b] 2 | (if (= a 0) 3 | b 4 | (inc (+ (dec a) b)))) 5 | 6 | (defn + [a b] 7 | (if (= a 0) 8 | b 9 | (+ (dec a) (inc b)))) 10 | -------------------------------------------------------------------------------- /resources/code/1.2.1.6.clj: -------------------------------------------------------------------------------- 1 | (defn A [x y] 2 | (cond (= y 0) 0 3 | (= x 0) (* 2 y) 4 | (= y 1) 2 5 | :else (A (- x 1) 6 | (A x (- y 1))))) 7 | -------------------------------------------------------------------------------- /resources/code/1.2.1.7.clj: -------------------------------------------------------------------------------- 1 | (A 1 10) 2 | 3 | (A 2 4) 4 | 5 | (A 3 3) 6 | -------------------------------------------------------------------------------- /resources/code/1.2.1.8.clj: -------------------------------------------------------------------------------- 1 | (defn f [n] (A 0 n)) 2 | 3 | (defn g [n] (A 1 n)) 4 | 5 | (defn h [n] (A 2 n)) 6 | 7 | (defn k [n] (* 5 n n)) 8 | -------------------------------------------------------------------------------- /resources/code/1.2.2.1.clj: -------------------------------------------------------------------------------- 1 | (defn fib [n] 2 | (cond (= n 0) 0 3 | (= n 1) 1 4 | :else (+ (fib (- n 1)) 5 | (fib (- n 2))))) 6 | -------------------------------------------------------------------------------- /resources/code/1.2.2.2.clj: -------------------------------------------------------------------------------- 1 | (defn fib [n] 2 | (fib-iter 1 0 n)) 3 | 4 | (defn fib-iter [a b counter] 5 | (if (= counter 0) 6 | b 7 | (fib-iter (+ a b) a (- counter 1)))) 8 | -------------------------------------------------------------------------------- /resources/code/1.2.2.3.clj: -------------------------------------------------------------------------------- 1 | (defn count-change 2 | [amount] 3 | 4 | (defn first-denom [kinds-of-coins] 5 | (cond (= kinds-of-coins 1) 1 6 | (= kinds-of-coins 2) 5 7 | (= kinds-of-coins 3) 10 8 | (= kinds-of-coins 4) 25 9 | (= kinds-of-coins 5) 50)) 10 | 11 | (defn cc 12 | [amount kinds-of-coins] 13 | (cond (= amount 0) 1 14 | (or (< amount 0) 15 | (= kinds-of-coins 0)) 0 16 | :else (+ (cc amount (- kinds-of-coins 1)) 17 | (cc (- amount 18 | (first-denom kinds-of-coins)) 19 | kinds-of-coins)))) 20 | 21 | (cc amount 5)) 22 | -------------------------------------------------------------------------------- /resources/code/1.2.2.4.clj: -------------------------------------------------------------------------------- 1 | (count-change 100) 2 | 292 3 | -------------------------------------------------------------------------------- /resources/code/1.2.3.1.clj: -------------------------------------------------------------------------------- 1 | (defn cube [x] (* x x x)) 2 | (defn p [x] (- (* 3 x) (* 4 (cube x)))) 3 | (defn sine [angle] 4 | (if (not (> (Math/abs angle) 0.1)) 5 | angle 6 | (p (sine (/ angle 3.0))))) 7 | -------------------------------------------------------------------------------- /resources/code/1.2.4.1.clj: -------------------------------------------------------------------------------- 1 | (defn expt [b n] 2 | (if (= n 0) 3 | 1 4 | (* b (expt b (- n 1))))) 5 | -------------------------------------------------------------------------------- /resources/code/1.2.4.2.clj: -------------------------------------------------------------------------------- 1 | (defn expt [b n] 2 | (expt-iter b n 1)) 3 | 4 | (defn expt-iter [b counter product] 5 | (if (= counter 0) 6 | product 7 | (expt-iter b 8 | (- counter 1) 9 | (* b product)))) 10 | -------------------------------------------------------------------------------- /resources/code/1.2.4.3.clj: -------------------------------------------------------------------------------- 1 | (defn fast-expt [b n] 2 | (cond (= n 0) 1 3 | (even? n) (square (fast-expt b (/ n 2))) 4 | :else (* b (fast-expt b (- n 1))))) 5 | 6 | -------------------------------------------------------------------------------- /resources/code/1.2.4.4.clj: -------------------------------------------------------------------------------- 1 | (defn * [a b] 2 | (if (= b 0) 3 | 0 4 | (+ a (* a (- b 1))))) 5 | -------------------------------------------------------------------------------- /resources/code/1.2.4.5.clj: -------------------------------------------------------------------------------- 1 | (defn fib [n] 2 | (fib-iter 1 0 0 1 n)) 3 | (defn fib-iter [a b p q counter] 4 | (cond ((= counter 0) b) 5 | ((even? counter) 6 | (fib-iter a 7 | b 8 | ; compute p' 9 | ; compute q' 10 | (/ count 2))) 11 | (else (fib-iter (+ (* b q) (* a q) (* a p)) 12 | (+ (* b p) (* a q)) 13 | p 14 | q 15 | (- count 1))))) 16 | -------------------------------------------------------------------------------- /resources/code/1.2.5.1.clj: -------------------------------------------------------------------------------- 1 | (defn gcd [a b] 2 | (if (= b 0) 3 | a 4 | (gcd b (mod a b)))) 5 | -------------------------------------------------------------------------------- /resources/code/1.2.6.1.clj: -------------------------------------------------------------------------------- 1 | (defn smallest-divisor [n] 2 | (find-divisor n 2)) 3 | 4 | (defn divides? [a b] 5 | (= (mod b a) 0)) 6 | 7 | (defn find-divisor [n test-divisor] 8 | (cond (> (square test-divisor) n) n 9 | (divides? test-divisor n) test-divisor 10 | :else (find-divisor n (+ test-divisor 1)))) 11 | -------------------------------------------------------------------------------- /resources/code/1.2.6.2.clj: -------------------------------------------------------------------------------- 1 | (defn prime? [n] 2 | (= n (smallest-divisor n))) 3 | -------------------------------------------------------------------------------- /resources/code/1.2.6.3.clj: -------------------------------------------------------------------------------- 1 | (defn expmod [base exp m] 2 | (cond (= exp 0) 1 3 | (even? exp) (mod (square (expmod base (/ exp 2) m)) 4 | m) 5 | :else (mod (* base (expmod base (- exp 1) m)) 6 | m))) 7 | -------------------------------------------------------------------------------- /resources/code/1.2.6.4.clj: -------------------------------------------------------------------------------- 1 | (defn fermat-test [n] 2 | (defn try-it [a] 3 | (= (expmod a n n) a)) 4 | (try-it (+ 1 (rand-int (- n 1))))) 5 | -------------------------------------------------------------------------------- /resources/code/1.2.6.5.clj: -------------------------------------------------------------------------------- 1 | (defn fast-prime? [n times] 2 | (cond (= times 0) true 3 | (fermat-test n) (fast-prime? n (- times 1)) 4 | :else false)) 5 | -------------------------------------------------------------------------------- /resources/code/1.2.6.6.clj: -------------------------------------------------------------------------------- 1 | (defn timed-prime-test [n] 2 | (time (prime? n))) 3 | -------------------------------------------------------------------------------- /resources/code/1.2.6.7.clj: -------------------------------------------------------------------------------- 1 | (defn expmod [base exp m] 2 | (remainder (fast-expt base exp) m)) 3 | -------------------------------------------------------------------------------- /resources/code/1.2.6.8.clj: -------------------------------------------------------------------------------- 1 | (defn expmod [base exp m] 2 | (cond (= exp 0) 1 3 | (even? exp) (mod (* (expmod base (/ exp 2) m) 4 | (expmod base (/ exp 2) m)) m) 5 | :else (mod (*' base (expmod base (-' exp 1) m)) m))) 6 | -------------------------------------------------------------------------------- /resources/code/1.2.6.9.clj: -------------------------------------------------------------------------------- 1 | (defn factorial [n] 2 | (defn fact-iter [product counter] 3 | (if (> counter n) 4 | product 5 | (fact-iter (* counter product) 6 | (+ counter 1) 7 | max-count))) 8 | (fact-iter 1 1)) 9 | -------------------------------------------------------------------------------- /resources/code/1.3.1.1.clj: -------------------------------------------------------------------------------- 1 | (defn sum-integers [a b] 2 | (if (> a b) 3 | 0 4 | (+ a (sum-integers (+ a 1) b)))) 5 | -------------------------------------------------------------------------------- /resources/code/1.3.1.10.clj: -------------------------------------------------------------------------------- 1 | (defn pi-sum [a b] 2 | (defn pi-term [x]) 3 | (/ 1.0 (* x (+ x 2))) 4 | (defn pi-next [x] 5 | (+ x 4)) 6 | (sum pi-term a pi-next b)) 7 | -------------------------------------------------------------------------------- /resources/code/1.3.1.11.clj: -------------------------------------------------------------------------------- 1 | (* 8 (pi-sum 1 1000)) 2 | 3.139592655589783 3 | -------------------------------------------------------------------------------- /resources/code/1.3.1.12.clj: -------------------------------------------------------------------------------- 1 | (defn integral [f a b dx] 2 | (defn add-dx [x] (+ x dx)) 3 | (* (sum f (+ a (/ dx 2.0)) add-dx b) 4 | dx)) 5 | (integral cube 0 1 0.01) 6 | 0.24998750000000042 7 | (integral cube 0 1 0.001) 8 | 0.249999875000001 9 | -------------------------------------------------------------------------------- /resources/code/1.3.1.13.clj: -------------------------------------------------------------------------------- 1 | (defn sum [term a next-term b] 2 | (defn iter [a result] 3 | (if <-??-> 4 | <-??-> 5 | (iter <-??-> <-??->))) 6 | (iter <-??-> <-??->)) 7 | -------------------------------------------------------------------------------- /resources/code/1.3.1.14.clj: -------------------------------------------------------------------------------- 1 | (accumulate combiner null-value term a next-term b) 2 | -------------------------------------------------------------------------------- /resources/code/1.3.1.2.clj: -------------------------------------------------------------------------------- 1 | (defn sum-cubes [a b] 2 | (if (> a b) 3 | 0 4 | (+ (cube a) (sum-cubes (+ a 1) b)))) 5 | -------------------------------------------------------------------------------- /resources/code/1.3.1.3.clj: -------------------------------------------------------------------------------- 1 | (defn pi-sum [a b] 2 | (if (> a b) 3 | 0 4 | (+ (/ 1.0 (* a (+ a 2))) (pi-sum (+ a 4) b)))) 5 | -------------------------------------------------------------------------------- /resources/code/1.3.1.4.clj: -------------------------------------------------------------------------------- 1 | (defn <-name-> [a b] 2 | (if (> a b) 3 | 0 4 | (+ (<-term-> a) 5 | (<-name-> (<-next-> a) b)))) 6 | -------------------------------------------------------------------------------- /resources/code/1.3.1.5.clj: -------------------------------------------------------------------------------- 1 | (defn sum [term a next-term b] 2 | (if (> a b) 3 | 0 4 | (+ (term a) 5 | (sum term (next-term a) next-term b)))) 6 | -------------------------------------------------------------------------------- /resources/code/1.3.1.6.clj: -------------------------------------------------------------------------------- 1 | (defn inc [n] (+ n 1)) 2 | (defn sum-cubes [a b] 3 | (sum cube a inc b)) 4 | -------------------------------------------------------------------------------- /resources/code/1.3.1.7.clj: -------------------------------------------------------------------------------- 1 | (sum-cubes 1 10) 2 | 3025 3 | -------------------------------------------------------------------------------- /resources/code/1.3.1.8.clj: -------------------------------------------------------------------------------- 1 | (defn identity [x] x) 2 | 3 | (defn sum-integers [a b] 4 | (sum identity a inc b)) 5 | -------------------------------------------------------------------------------- /resources/code/1.3.1.9.clj: -------------------------------------------------------------------------------- 1 | (sum-integers 1 10) 2 | 55 3 | -------------------------------------------------------------------------------- /resources/code/1.3.1.clj: -------------------------------------------------------------------------------- 1 | (defn cube [x] (* x x x)) 2 | -------------------------------------------------------------------------------- /resources/code/1.3.2.1.clj: -------------------------------------------------------------------------------- 1 | (fn [x] (+ x 4)) 2 | -------------------------------------------------------------------------------- /resources/code/1.3.2.10.clj: -------------------------------------------------------------------------------- 1 | (defn f [x y] 2 | (defn f-helper [a b] 3 | (+ (* x (square a)) 4 | (* y b) 5 | (* a b))) 6 | (f-helper (+ 1 (* x y)) 7 | (- 1 y))) 8 | -------------------------------------------------------------------------------- /resources/code/1.3.2.11.clj: -------------------------------------------------------------------------------- 1 | (defn f [x y] 2 | ((fn [a b] 3 | (+ (* x (square a)) 4 | (* y b) 5 | (* a b))) 6 | (+ 1 (* x y)) 7 | (- 1 y))) 8 | -------------------------------------------------------------------------------- /resources/code/1.3.2.12.clj: -------------------------------------------------------------------------------- 1 | (defn f [x y] 2 | (let [a (+ 1 (* x y)) 3 | b (- 1 y)] 4 | (+ (* x (square a)) 5 | (* y b) 6 | (* a b)))) 7 | -------------------------------------------------------------------------------- /resources/code/1.3.2.13.clj: -------------------------------------------------------------------------------- 1 | (let [<-var 1-> <-exp 1-> 2 | <-var 2-> <-exp 2-> 3 | ... 4 | <-var n-> <-exp n->] 5 | <-body->) 6 | -------------------------------------------------------------------------------- /resources/code/1.3.2.14.clj: -------------------------------------------------------------------------------- 1 | ((fn [<-var 1-> ... <-var n->] 2 | <-body->) 3 | <-exp 1-> 4 | ... 5 | <-exp n->) 6 | -------------------------------------------------------------------------------- /resources/code/1.3.2.15.clj: -------------------------------------------------------------------------------- 1 | (+ (let [x 3] 2 | (+ x (* x 10))) 3 | x) 4 | -------------------------------------------------------------------------------- /resources/code/1.3.2.16.clj: -------------------------------------------------------------------------------- 1 | (let [x 3 2 | y (+ x 2)] 3 | (* x y)) 4 | -------------------------------------------------------------------------------- /resources/code/1.3.2.17.clj: -------------------------------------------------------------------------------- 1 | (defn f [x y] 2 | (def a (+ 1 (* x y))) 3 | (def b (- 1 y)) 4 | (+ (* x (square a)) 5 | (* y b) 6 | (* a b))) 7 | -------------------------------------------------------------------------------- /resources/code/1.3.2.18.clj: -------------------------------------------------------------------------------- 1 | (defn f [g] 2 | (g 2)) 3 | -------------------------------------------------------------------------------- /resources/code/1.3.2.19.clj: -------------------------------------------------------------------------------- 1 | (f square) 2 | 4 3 | 4 | (f (fn [z] (* z (+ z 1)))) 5 | 6 6 | -------------------------------------------------------------------------------- /resources/code/1.3.2.2.clj: -------------------------------------------------------------------------------- 1 | (fn [x] (/ 1.0 (* x (+ x 2)))) 2 | -------------------------------------------------------------------------------- /resources/code/1.3.2.3.clj: -------------------------------------------------------------------------------- 1 | (defn pi-sum [a b] 2 | (sum (fn [x] (/ 1.0 (* x (+ x 2)))) 3 | a 4 | (fn [x] (+ x 4)) 5 | b)) 6 | -------------------------------------------------------------------------------- /resources/code/1.3.2.4.clj: -------------------------------------------------------------------------------- 1 | (defn integral [f a b dx] 2 | (* (sum f 3 | (+ a (/ dx 2.0)) 4 | (fn [x] (+ x dx)) 5 | b) 6 | dx)) 7 | -------------------------------------------------------------------------------- /resources/code/1.3.2.5.clj: -------------------------------------------------------------------------------- 1 | (fn [<-formal-parameters->] <-body->) 2 | -------------------------------------------------------------------------------- /resources/code/1.3.2.6.clj: -------------------------------------------------------------------------------- 1 | (defn plus4 [x] (+ x 4)) 2 | -------------------------------------------------------------------------------- /resources/code/1.3.2.7.clj: -------------------------------------------------------------------------------- 1 | (def plus4 (fn [x] (+ x 4))) 2 | -------------------------------------------------------------------------------- /resources/code/1.3.2.8.clj: -------------------------------------------------------------------------------- 1 | (fn [x] (+ x 4)) 2 | ; | | | | | 3 | ;The procedure of an argument x that adds x and 4. 4 | -------------------------------------------------------------------------------- /resources/code/1.3.2.9.clj: -------------------------------------------------------------------------------- 1 | ((fn [x y z] (+ x y (square z))) 1 2 3) 2 | 12 3 | -------------------------------------------------------------------------------- /resources/code/1.3.2.clj: -------------------------------------------------------------------------------- 1 | (* 3 3 3) 2 | (* x x x) 3 | (* y y y) 4 | -------------------------------------------------------------------------------- /resources/code/1.3.3.1.clj: -------------------------------------------------------------------------------- 1 | (defn search [f neg-point pos-point] 2 | (let [midpoint (average neg-point pos-point)] 3 | (if (close-enough? neg-point pos-point) 4 | midpoint 5 | (let [test-value (f midpoint)] 6 | (cond (positive? test-value) 7 | (search f neg-point midpoint) 8 | (negative? test-value) 9 | (search f midpoint pos-point) 10 | :else midpoint))))) 11 | -------------------------------------------------------------------------------- /resources/code/1.3.3.10.clj: -------------------------------------------------------------------------------- 1 | (defn sqrt [x] 2 | (fixed-point (fn [y] (/ (+ y (/ x y)) 2.0)) 1.0)) 3 | -------------------------------------------------------------------------------- /resources/code/1.3.3.11.clj: -------------------------------------------------------------------------------- 1 | (cont-frac (fn [i] 1.0) 2 | (fn [i] 1.0) 3 | k) 4 | -------------------------------------------------------------------------------- /resources/code/1.3.3.2.clj: -------------------------------------------------------------------------------- 1 | (defn close-enough? [x y] 2 | (< (Math/abs (- x y)) 0.001)) 3 | -------------------------------------------------------------------------------- /resources/code/1.3.3.3.clj: -------------------------------------------------------------------------------- 1 | (defn half-interval-method [f a b] 2 | (let [a-value (f a) 3 | b-value (f b)] 4 | (cond (and (negative? a-value) (positive? b-value)) 5 | (search f a b) 6 | (and (negative? b-value) (positive? a-value)) 7 | (search f b a) 8 | :else 9 | (throw (Exception. "Values are not of opposite sign"))))) 10 | -------------------------------------------------------------------------------- /resources/code/1.3.3.4.clj: -------------------------------------------------------------------------------- 1 | (half-interval-method Math/sin 2.0 4.0) 2 | 3.14111328125 3 | -------------------------------------------------------------------------------- /resources/code/1.3.3.5.clj: -------------------------------------------------------------------------------- 1 | (half-interval-method (fn [x] (- (* x x x) (* 2 x) 3)) 2 | 1.0 3 | 2.0) 4 | 1.89306640625 5 | -------------------------------------------------------------------------------- /resources/code/1.3.3.6.clj: -------------------------------------------------------------------------------- 1 | (defn fixed-point [f first-guess] 2 | (let [tolerance 0.00001] 3 | 4 | (defn close-enough? [v1 v2] 5 | 6 | (defn abs [x] 7 | (if (> 0 x) (- x) x)) 8 | 9 | (< (abs (- v1 v2)) tolerance)) 10 | 11 | (defn try-it [guess] 12 | (let [next-guess (f guess)] 13 | (if (close-enough? guess next-guess) 14 | next-guess 15 | (try-it next-guess)))) 16 | 17 | (try-it first-guess))) 18 | -------------------------------------------------------------------------------- /resources/code/1.3.3.7.clj: -------------------------------------------------------------------------------- 1 | (fixed-point cos 1.0) 2 | 0.7390822985224023 3 | -------------------------------------------------------------------------------- /resources/code/1.3.3.8.clj: -------------------------------------------------------------------------------- 1 | (fixed-point (fn [y] (+ (Math/sin y) (Math/cos y))) 2 | 1.0) 3 | 1.2587315962971173 4 | -------------------------------------------------------------------------------- /resources/code/1.3.3.9.clj: -------------------------------------------------------------------------------- 1 | (defn (sqrt x) 2 | (fixed-point (fn [y] (/ x y)) 1.0)) 3 | -------------------------------------------------------------------------------- /resources/code/1.3.4.1.clj: -------------------------------------------------------------------------------- 1 | (defn average-damp [f] 2 | (fn [x] (average x (f x)))) 3 | -------------------------------------------------------------------------------- /resources/code/1.3.4.10.clj: -------------------------------------------------------------------------------- 1 | (defn sqrt [x] 2 | (fixed-point-of-transform 3 | (fn [y] (/ x y)) 4 | average-damp 5 | 1.0)) 6 | -------------------------------------------------------------------------------- /resources/code/1.3.4.11.clj: -------------------------------------------------------------------------------- 1 | (defn sqrt [x] 2 | (fixed-point-of-transform 3 | (fn [y] (- (square y) x)) 4 | newton-transform 5 | 1.0)) 6 | -------------------------------------------------------------------------------- /resources/code/1.3.4.12.clj: -------------------------------------------------------------------------------- 1 | (newtons-method (cubic a b c) 1) 2 | -------------------------------------------------------------------------------- /resources/code/1.3.4.13.clj: -------------------------------------------------------------------------------- 1 | (((double-it (double-it double-it)) inc) 5) 2 | -------------------------------------------------------------------------------- /resources/code/1.3.4.14.clj: -------------------------------------------------------------------------------- 1 | ((compose square inc) 6) 2 | 49 3 | -------------------------------------------------------------------------------- /resources/code/1.3.4.15.clj: -------------------------------------------------------------------------------- 1 | ((repeated square 2) 5) 2 | 625 3 | -------------------------------------------------------------------------------- /resources/code/1.3.4.2.clj: -------------------------------------------------------------------------------- 1 | ((average-damp square) 10) 2 | 55 3 | -------------------------------------------------------------------------------- /resources/code/1.3.4.3.clj: -------------------------------------------------------------------------------- 1 | (defn sqrt [x] 2 | (fixed-point (average-damp (fn [y] (/ x y))) 3 | 1.0)) 4 | -------------------------------------------------------------------------------- /resources/code/1.3.4.4.clj: -------------------------------------------------------------------------------- 1 | (defn cube-root [x] 2 | (fixed-point (average-damp (fn [y] (/ x (square y)))) 3 | 1.0)) 4 | -------------------------------------------------------------------------------- /resources/code/1.3.4.5.clj: -------------------------------------------------------------------------------- 1 | (defn deriv [g] 2 | (let [dx 0.00001] 3 | (fn [x] 4 | (/ (- (g (+ x dx)) (g x)) 5 | dx)))) 6 | -------------------------------------------------------------------------------- /resources/code/1.3.4.6.clj: -------------------------------------------------------------------------------- 1 | (defn cube [x] 2 | (* x x x)) 3 | 4 | ((deriv cube) 5) ;; = 75 5 | 75.0014999664018 6 | -------------------------------------------------------------------------------- /resources/code/1.3.4.7.clj: -------------------------------------------------------------------------------- 1 | (defn newtons-method [g guess] 2 | (defn newton-transform [g] 3 | (fn [x] 4 | (- x (/ (g x) 5 | ((deriv g) x))))) 6 | (fixed-point (newton-transform g) guess)) 7 | -------------------------------------------------------------------------------- /resources/code/1.3.4.8.clj: -------------------------------------------------------------------------------- 1 | (defn sqrt [x] 2 | (newtons-method 3 | (fn [y] 4 | (- (square y) x)) 5 | 1.0)) 6 | -------------------------------------------------------------------------------- /resources/code/1.3.4.9.clj: -------------------------------------------------------------------------------- 1 | (defn fixed-point-of-transform [g transform guess] 2 | (fixed-point (transform g) guess)) 3 | -------------------------------------------------------------------------------- /resources/code/2.1.1.1.clj: -------------------------------------------------------------------------------- 1 | (defn add-rat [x y] 2 | (make-rat (+ (* (numer x) (denom y)) 3 | (* (numer y) (denom x))) 4 | (* (denom x) (denom y)))) 5 | 6 | (defn sub-rat [x y] 7 | (make-rat (- (* (numer x) (denom y)) 8 | (* (numer y) (denom x))) 9 | (* (denom x) (denom y)))) 10 | 11 | (defn mul-rat [x y] 12 | (make-rat (* (numer x) (numer y)) 13 | (* (denom x) (denom y)))) 14 | 15 | (defn div-rat [x y] 16 | (make-rat (* (numer x) (denom y)) 17 | (* (denom x) (numer y)))) 18 | 19 | (defn equal-rat? [x y] 20 | (= (* (numer x) (denom y)) 21 | (* (numer y) (denom x)))) 22 | -------------------------------------------------------------------------------- /resources/code/2.1.1.2.clj: -------------------------------------------------------------------------------- 1 | (def x (vector 1 2)) 2 | 3 | (first x) 4 | 1 5 | 6 | (second x) 7 | 2 8 | -------------------------------------------------------------------------------- /resources/code/2.1.1.3.clj: -------------------------------------------------------------------------------- 1 | (def x [1 2]) 2 | 3 | (first x) 4 | 1 5 | 6 | (second x) 7 | 2 8 | -------------------------------------------------------------------------------- /resources/code/2.1.1.4.clj: -------------------------------------------------------------------------------- 1 | (def x (vector 1 2)) 2 | 3 | (def y [3 4]) 4 | 5 | (def z [x y]) 6 | 7 | (first (first z)) 8 | 1 9 | 10 | (first (second z)) 11 | 3 12 | -------------------------------------------------------------------------------- /resources/code/2.1.1.5.clj: -------------------------------------------------------------------------------- 1 | (defn make-rat [n d] [n d]) 2 | 3 | (defn numer [x] (first x)) 4 | 5 | (defn denom [x] (second x)) 6 | -------------------------------------------------------------------------------- /resources/code/2.1.1.6.clj: -------------------------------------------------------------------------------- 1 | (defn print-rat [x] 2 | (print (numer x)) 3 | (print "/") 4 | (print (denom x)) 5 | (newline)) 6 | -------------------------------------------------------------------------------- /resources/code/2.1.1.7.clj: -------------------------------------------------------------------------------- 1 | (def one-half (make-rat 1 2)) 2 | 3 | (print-rat one-half) 4 | 1/2 5 | 6 | (def one-third (make-rat 1 3)) 7 | (print-rat (add-rat one-half one-third)) 8 | 5/6 9 | 10 | (print-rat (mul-rat one-half one-third)) 11 | 1/6 12 | 13 | (print-rat (add-rat one-third one-third)) 14 | 6/9 15 | -------------------------------------------------------------------------------- /resources/code/2.1.1.8.clj: -------------------------------------------------------------------------------- 1 | (defn make-rat [n d] 2 | (let [g (gcd n d)] 3 | [(/ n g) (/ d g)]) 4 | -------------------------------------------------------------------------------- /resources/code/2.1.1.9.clj: -------------------------------------------------------------------------------- 1 | (print-rat (add-rat one-third one-third)) 2 | 2/3 3 | -------------------------------------------------------------------------------- /resources/code/2.1.2.1.clj: -------------------------------------------------------------------------------- 1 | (defn make-rat [n d] 2 | [n d]) 3 | 4 | (defn numer [x] 5 | (let [g (gcd (first x) (second x))] 6 | (/ (first x) g))) 7 | 8 | (defn denom [x] 9 | (let [g (gcd (first x) (second x))] 10 | (/ (second x) g))) 11 | -------------------------------------------------------------------------------- /resources/code/2.1.2.2.clj: -------------------------------------------------------------------------------- 1 | (defn print-point [point] 2 | (print "(") 3 | (print (x-point point)) 4 | (print ",") 5 | (print (y-point point)) 6 | (print ")") 7 | (println)) 8 | -------------------------------------------------------------------------------- /resources/code/2.1.3.1.clj: -------------------------------------------------------------------------------- 1 | (defn vector [x y] 2 | (defn dispatch [m] 3 | (cond (= m 0) x 4 | (= m 1) y 5 | :else (throw (Exception. "Argument not 0 or 1" )))) 6 | dispatch) 7 | 8 | (defn first [z] (z 0)) 9 | 10 | (defn second [z] (z 1)) 11 | -------------------------------------------------------------------------------- /resources/code/2.1.3.2.clj: -------------------------------------------------------------------------------- 1 | (defn cons [x y] 2 | (defn dispatch [m] 3 | (cond (= m 0) x 4 | (= m 1) y 5 | :else (throw (Exception. "Argument not 0 or 1" )))) 6 | dispatch) 7 | 8 | (defn car [z] (z 0)) 9 | 10 | (defn cdr [z] (z 1)) 11 | -------------------------------------------------------------------------------- /resources/code/2.1.3.3.clj: -------------------------------------------------------------------------------- 1 | (defn vector [x y] 2 | (fn [m] (m x y))) 3 | 4 | (defn first [z] 5 | (z (fn [p q] p))) 6 | -------------------------------------------------------------------------------- /resources/code/2.1.3.4.clj: -------------------------------------------------------------------------------- 1 | (def zero (fn [f] (fn [x] x))) 2 | 3 | (def add-1 [n] 4 | (fn [f] (fn [x] (f ((n f) x))))) 5 | -------------------------------------------------------------------------------- /resources/code/2.1.4.1.clj: -------------------------------------------------------------------------------- 1 | (defn add-interval [x y] 2 | (make-interval (+ (lower-bound x) (lower-bound y)) 3 | (+ (upper-bound x) (upper-bound y)))) 4 | -------------------------------------------------------------------------------- /resources/code/2.1.4.2.clj: -------------------------------------------------------------------------------- 1 | (defn mul-interval [x y] 2 | (let [p1 (* (lower-bound x) (lower-bound y)) 3 | p2 (* (lower-bound x) (upper-bound y)) 4 | p3 (* (upper-bound x) (lower-bound y)) 5 | p4 (* (upper-bound x) (upper-bound y))] 6 | (make-interval (min p1 p2 p3 p4) 7 | (max p1 p2 p3 p4)))) 8 | -------------------------------------------------------------------------------- /resources/code/2.1.4.3.clj: -------------------------------------------------------------------------------- 1 | (defn div-interval [x y] 2 | (mul-interval x 3 | (make-interval (/ 1.0 (upper-bound y)) 4 | (/ 1.0 (lower-bound y))))) 5 | -------------------------------------------------------------------------------- /resources/code/2.1.4.4.clj: -------------------------------------------------------------------------------- 1 | (defn make-interval [a b] 2 | [a b]) 3 | -------------------------------------------------------------------------------- /resources/code/2.1.4.5.clj: -------------------------------------------------------------------------------- 1 | (defn make-center-width [c w] 2 | (make-interval (- c w) (+ c w))) 3 | 4 | (defn center [i] 5 | (/ (+ (lower-bound i) (upper-bound i)) 2)) 6 | 7 | (defn width [i] 8 | (/ (- (upper-bound i) (lower-bound i)) 2)) 9 | -------------------------------------------------------------------------------- /resources/code/2.1.4.6.clj: -------------------------------------------------------------------------------- 1 | (defn par1 [r1 r2] 2 | (div-interval (mul-interval r1 r2) 3 | (add-interval r1 r2))) 4 | 5 | (defn par2 [r1 r2] 6 | (let [one (make-interval 1 1)] 7 | (div-interval one 8 | (add-interval (div-interval one r1) 9 | (div-interval one r2))))) 10 | -------------------------------------------------------------------------------- /resources/code/2.1.4.7.clj: -------------------------------------------------------------------------------- 1 | (def make-rat vector) 2 | (def numer first) 3 | (def denom second) 4 | -------------------------------------------------------------------------------- /resources/code/2.1.clj: -------------------------------------------------------------------------------- 1 | (defn linear-combination [a b x y] 2 | (+ (* a x) (* b y))) 3 | -------------------------------------------------------------------------------- /resources/code/2.2.1.1.clj: -------------------------------------------------------------------------------- 1 | (cons 1 2 | (cons 2 3 | (cons 3 4 | (cons 4 '())))) 5 | -------------------------------------------------------------------------------- /resources/code/2.2.1.10.clj: -------------------------------------------------------------------------------- 1 | (defn concat [list1 list2] 2 | (if (empty? list1) 3 | list2 4 | (cons (first list1) 5 | (concat (rest list1) list2)))) 6 | -------------------------------------------------------------------------------- /resources/code/2.2.1.11.clj: -------------------------------------------------------------------------------- 1 | => (last-list (list 23 72 149 34)) 2 | (34) 3 | -------------------------------------------------------------------------------- /resources/code/2.2.1.12.clj: -------------------------------------------------------------------------------- 1 | => (reverse (list 1 4 9 16 25)) 2 | (25 16 9 4 1) 3 | -------------------------------------------------------------------------------- /resources/code/2.2.1.13.clj: -------------------------------------------------------------------------------- 1 | (def us-coins (list 50 25 10 5 1)) 2 | (def uk-coins (list 100 50 20 10 5 2 1 0.5)) 3 | -------------------------------------------------------------------------------- /resources/code/2.2.1.14.clj: -------------------------------------------------------------------------------- 1 | => (cc 100 us-coins) 2 | 292 3 | -------------------------------------------------------------------------------- /resources/code/2.2.1.15.clj: -------------------------------------------------------------------------------- 1 | (defn cc [amount coin-values] 2 | (cond (= amount 0) 1 3 | (or (< amount 0) (no-more? coin-values)) 0 4 | :else 5 | (+ (cc amount 6 | (except-first-denomination coin-values)) 7 | (cc (- amount 8 | (first-denomination coin-values)) 9 | coin-values)))) 10 | -------------------------------------------------------------------------------- /resources/code/2.2.1.16.clj: -------------------------------------------------------------------------------- 1 | (defn [f x y & z] <-body->) 2 | -------------------------------------------------------------------------------- /resources/code/2.2.1.17.clj: -------------------------------------------------------------------------------- 1 | (f 1 2 3 4 5 6) 2 | -------------------------------------------------------------------------------- /resources/code/2.2.1.18.clj: -------------------------------------------------------------------------------- 1 | (defn [g & w] <-body->) 2 | -------------------------------------------------------------------------------- /resources/code/2.2.1.19.clj: -------------------------------------------------------------------------------- 1 | (g 1 2 3 4 5 6) 2 | -------------------------------------------------------------------------------- /resources/code/2.2.1.2.clj: -------------------------------------------------------------------------------- 1 | (list <-a 1-> <-a 2-> ... <-a n->) 2 | -------------------------------------------------------------------------------- /resources/code/2.2.1.20.clj: -------------------------------------------------------------------------------- 1 | (same-parity 1 2 3 4 5 6 7) 2 | (1 3 5 7) 3 | 4 | (same-parity 2 3 4 5 6 7) 5 | (2 4 6) 6 | -------------------------------------------------------------------------------- /resources/code/2.2.1.21.clj: -------------------------------------------------------------------------------- 1 | (defn scale-list [items factor] 2 | (if (empty? items) 3 | nil 4 | (cons (* (first items) factor) 5 | (scale-list (rest items) factor)))) 6 | 7 | (scale-list (list 1 2 3 4 5) 10) 8 | (10 20 30 40 50) 9 | -------------------------------------------------------------------------------- /resources/code/2.2.1.22.clj: -------------------------------------------------------------------------------- 1 | (defn map [proc items] 2 | (if (empty? items) 3 | nil 4 | (list (proc (first items)) 5 | (map proc (rest items))))) 6 | 7 | => (map abs (list -10 2.5 -11.6 17)) 8 | (10 2.5 11.6 17) 9 | 10 | => (map (fn [x] (* x x)) 11 | (list 1 2 3 4)) 12 | (1 4 9 16) 13 | -------------------------------------------------------------------------------- /resources/code/2.2.1.23.clj: -------------------------------------------------------------------------------- 1 | (defn scale-list [items factor] 2 | (map (fn [x] (* x factor)) 3 | items)) 4 | -------------------------------------------------------------------------------- /resources/code/2.2.1.24.clj: -------------------------------------------------------------------------------- 1 | (square-list (list 1 2 3 4)) 2 | (1 4 9 16) 3 | -------------------------------------------------------------------------------- /resources/code/2.2.1.25.clj: -------------------------------------------------------------------------------- 1 | (defn square-list [items] 2 | (if (empty? items) 3 | nil 4 | (cons <-??-> <-??->))) 5 | 6 | (defn square-list [items] 7 | (map <-??-> <-??->)) 8 | -------------------------------------------------------------------------------- /resources/code/2.2.1.26.clj: -------------------------------------------------------------------------------- 1 | (defn square-list [items] 2 | (defn iter [things answer] 3 | (if (empty? things) 4 | answer 5 | (iter (rest things) 6 | (cons (square (first things)) 7 | answer)))) 8 | (iter items nil)) 9 | -------------------------------------------------------------------------------- /resources/code/2.2.1.27.clj: -------------------------------------------------------------------------------- 1 | (defn square-list [items] 2 | (defn iter [things answer] 3 | (if (empty? things) 4 | answer 5 | (iter (rest things) 6 | (cons answer 7 | (square (first things)))))) 8 | (iter items nil)) 9 | -------------------------------------------------------------------------------- /resources/code/2.2.1.28.clj: -------------------------------------------------------------------------------- 1 | => (doseq [i (list 57 321 88)] 2 | (println i)) 3 | 57 4 | 321 5 | 88 6 | -------------------------------------------------------------------------------- /resources/code/2.2.1.3.clj: -------------------------------------------------------------------------------- 1 | (cons <-a 1-> 2 | (cons <-a 2-> 3 | (cons ... 4 | (cons <-a n-> 5 | '()) 6 | ...))) 7 | -------------------------------------------------------------------------------- /resources/code/2.2.1.4.clj: -------------------------------------------------------------------------------- 1 | (def one-through-four (list 1 2 3 4)) 2 | 3 | => one-through-four 4 | (1 2 3 4) 5 | -------------------------------------------------------------------------------- /resources/code/2.2.1.5.clj: -------------------------------------------------------------------------------- 1 | => (first one-through-four) 2 | 1 3 | 4 | => (rest one-through-four) 5 | (2 3 4) 6 | 7 | => (first (rest one-through-four)) 8 | 2 9 | 10 | => (cons 10 one-through-four) 11 | (10 1 2 3 4) 12 | 13 | => (cons 5 one-through-four) 14 | (5 1 2 3 4) 15 | -------------------------------------------------------------------------------- /resources/code/2.2.1.6.clj: -------------------------------------------------------------------------------- 1 | (defn nth [items n] 2 | (if (= n 0) 3 | (first items) 4 | (nth (rest items) (- n 1)))) 5 | 6 | (def squares (list 1 4 9 16 25)) 7 | 8 | => (nth squares 3) 9 | 16 10 | -------------------------------------------------------------------------------- /resources/code/2.2.1.7.clj: -------------------------------------------------------------------------------- 1 | (defn count [items] 2 | (if (empty? items) 3 | 0 4 | (+ 1 (count (rest items))))) 5 | 6 | (def odds (list 1 3 5 7)) 7 | 8 | => (count odds) 9 | 4 10 | -------------------------------------------------------------------------------- /resources/code/2.2.1.8.clj: -------------------------------------------------------------------------------- 1 | (defn count [items] 2 | (defn count-iter [a length] 3 | (if (empty? a) 4 | length 5 | (count-iter (rest a) (+ 1 length)))) 6 | (count-iter items 0)) 7 | -------------------------------------------------------------------------------- /resources/code/2.2.1.9.clj: -------------------------------------------------------------------------------- 1 | => (concat squares odds) 2 | (1 4 9 16 25 1 3 5 7) 3 | 4 | => (concat odds squares) 5 | (1 3 5 7 1 9 16 25) 6 | -------------------------------------------------------------------------------- /resources/code/2.2.2.1.clj: -------------------------------------------------------------------------------- 1 | (cons (list 1 2) (list 3 4)) 2 | -------------------------------------------------------------------------------- /resources/code/2.2.2.10.clj: -------------------------------------------------------------------------------- 1 | (defn make-branch [length structure] 2 | (list length structure)) 3 | -------------------------------------------------------------------------------- /resources/code/2.2.2.11.clj: -------------------------------------------------------------------------------- 1 | (defn make-mobile [left right] 2 | (cons left right)) 3 | 4 | (defn make-branch [length structure] 5 | (cons length structure)) 6 | -------------------------------------------------------------------------------- /resources/code/2.2.2.12.clj: -------------------------------------------------------------------------------- 1 | (defn scale-tree [tree factor] 2 | (cond (not (seq? tree)) (* tree factor) 3 | (empty? tree) '() 4 | :else (cons (scale-tree (first tree) factor) 5 | (scale-tree (rest tree) factor)))) 6 | 7 | (scale-tree (list 1 (list 2 (list 3 4) 5) (list 6 7)) 8 | 10) 9 | (10 (20 (30 40) 50) (60 70)) 10 | -------------------------------------------------------------------------------- /resources/code/2.2.2.13.clj: -------------------------------------------------------------------------------- 1 | (defn scale-tree [tree factor] 2 | (map (fn [sub-tree] 3 | (if (seq? sub-tree) 4 | (scale-tree sub-tree factor) 5 | (* sub-tree factor))) 6 | tree)) 7 | -------------------------------------------------------------------------------- /resources/code/2.2.2.14.clj: -------------------------------------------------------------------------------- 1 | (square-tree 2 | (list 1 3 | (list 2 (list 3 4) 5) 4 | (list 6 7))) 5 | 6 | (1 (4 (9 16) 25) (36 49)) 7 | -------------------------------------------------------------------------------- /resources/code/2.2.2.15.clj: -------------------------------------------------------------------------------- 1 | (defn square-tree [tree] (tree-map square tree)) 2 | -------------------------------------------------------------------------------- /resources/code/2.2.2.16.clj: -------------------------------------------------------------------------------- 1 | (defn subsets [s] 2 | (if (empty? s) 3 | '() 4 | (let [more (subsets (cdr s))] 5 | (append more (map <-??-> more))))) 6 | -------------------------------------------------------------------------------- /resources/code/2.2.2.2.clj: -------------------------------------------------------------------------------- 1 | (def x (cons (list 1 2) (list 3 4))) 2 | 3 | => (count x) 4 | 3 5 | 6 | => (count-leaves x) 7 | 4 8 | 9 | => (list x x) 10 | (((1 2) 3 4) ((1 2) 3 4)) 11 | 12 | => (count (list x x)) 13 | 2 14 | 15 | => (count-leaves (list x x)) 16 | 8 17 | -------------------------------------------------------------------------------- /resources/code/2.2.2.3.clj: -------------------------------------------------------------------------------- 1 | (defn count-leaves [x] 2 | (cond (not (seq? x)) 1 3 | (empty? x) 0 4 | :else (+ (count-leaves (first x)) 5 | (count-leaves (rest x))))) 6 | -------------------------------------------------------------------------------- /resources/code/2.2.2.4.clj: -------------------------------------------------------------------------------- 1 | (1 3 (5 7) 9) 2 | 3 | ((7)) 4 | 5 | (1 (2 (3 (4 (5 (6 7)))))) 6 | -------------------------------------------------------------------------------- /resources/code/2.2.2.5.clj: -------------------------------------------------------------------------------- 1 | (def x (list 1 2 3)) 2 | (def y (list 4 5 6)) 3 | -------------------------------------------------------------------------------- /resources/code/2.2.2.6.clj: -------------------------------------------------------------------------------- 1 | (concat x y) 2 | 3 | (cons x y) 4 | 5 | (list x y) 6 | -------------------------------------------------------------------------------- /resources/code/2.2.2.7.clj: -------------------------------------------------------------------------------- 1 | (def x (list (list 1 2) (list 3 4))) 2 | 3 | => x 4 | ((1 2) (3 4)) 5 | 6 | => (reverse x) 7 | ((3 4) (1 2)) 8 | 9 | => (deep-reverse x) 10 | ((4 3) (2 1)) 11 | -------------------------------------------------------------------------------- /resources/code/2.2.2.8.clj: -------------------------------------------------------------------------------- 1 | (def x (list (list 1 2) (list 3 4))) 2 | 3 | => (fringe x) 4 | (1 2 3 4) 5 | 6 | => (fringe (list x x)) 7 | (1 2 3 4 1 2 3 4) 8 | -------------------------------------------------------------------------------- /resources/code/2.2.2.9.clj: -------------------------------------------------------------------------------- 1 | (defn make-mobile [left right] 2 | (list left right)) 3 | -------------------------------------------------------------------------------- /resources/code/2.2.3.1.clj: -------------------------------------------------------------------------------- 1 | (define sum-odd-squares [tree] 2 | (cond (empty? tree) 0 3 | (not (seq? tree)) 4 | (if (odd? tree) (square tree) 0) 5 | :else (+ (sum-odd-squares (first tree)) 6 | (sum-odd-squares (rest tree))))) 7 | -------------------------------------------------------------------------------- /resources/code/2.2.3.10.clj: -------------------------------------------------------------------------------- 1 | (defn even-fibs [n] 2 | (accumulate cons 3 | nil 4 | (filter even? 5 | (map fib 6 | (enumerate-interval 0 n))))) 7 | -------------------------------------------------------------------------------- /resources/code/2.2.3.11.clj: -------------------------------------------------------------------------------- 1 | (defn list-fib-squares [n] 2 | (accumulate cons 3 | nil 4 | (map square 5 | (map fib 6 | (enumerate-interval 0 n))))) 7 | 8 | (list-fib-squares 10) 9 | (0 1 1 4 9 25 64 169 441 1156 3025) 10 | -------------------------------------------------------------------------------- /resources/code/2.2.3.12.clj: -------------------------------------------------------------------------------- 1 | (defn product-of-squares-of-odd-elements [sequence] 2 | (accumulate * 3 | 1 4 | (map square 5 | (filter odd? sequence)))) 6 | 7 | (product-of-squares-of-odd-elements (list 1 2 3 4 5)) 8 | 225 9 | -------------------------------------------------------------------------------- /resources/code/2.2.3.13.clj: -------------------------------------------------------------------------------- 1 | (defn salary-of-highest-paid-programmer [records] 2 | (accumulate max 3 | 0 4 | (map salary 5 | (filter programmer? records)))) 6 | -------------------------------------------------------------------------------- /resources/code/2.2.3.14.clj: -------------------------------------------------------------------------------- 1 | (defn map [p sequence] 2 | (accumulate (fn [x y] <-??->) nil sequence)) 3 | 4 | (defn append [seq1 seq2] 5 | (accumulate cons <-??-> <-??->)) 6 | 7 | (defn length [sequence] 8 | (accumulate <-??-> 0 sequence)) 9 | -------------------------------------------------------------------------------- /resources/code/2.2.3.15.clj: -------------------------------------------------------------------------------- 1 | (defn horner-eval [x coefficient-sequence] 2 | (accumulate (fn [this-coeff higher-terms] <-??->) 3 | 0 4 | coefficient-sequence)) 5 | -------------------------------------------------------------------------------- /resources/code/2.2.3.16.clj: -------------------------------------------------------------------------------- 1 | (horner-eval 2 (list 1 3 0 5 0 1)) 2 | -------------------------------------------------------------------------------- /resources/code/2.2.3.17.clj: -------------------------------------------------------------------------------- 1 | (defn count-leaves [t] 2 | (accumulate <-??-> <-??-> (map <-??-> <-??->))) 3 | -------------------------------------------------------------------------------- /resources/code/2.2.3.18.clj: -------------------------------------------------------------------------------- 1 | (defn accumulate-n [op init seqs] 2 | (if (nil? (first seqs)) 3 | nil 4 | (cons (accumulate op init <-??->) 5 | (accumulate-n op init <-??->)))) 6 | -------------------------------------------------------------------------------- /resources/code/2.2.3.19.clj: -------------------------------------------------------------------------------- 1 | (defn dot-product [v w] 2 | (accumulate + 0 (map * v w))) 3 | -------------------------------------------------------------------------------- /resources/code/2.2.3.2.clj: -------------------------------------------------------------------------------- 1 | (defn even-fibs [n] 2 | (defn next [k] 3 | (if (> k n) 4 | nil 5 | (let [f (fib k)] 6 | (if (even? f) 7 | (cons f (next (+ k 1))) 8 | (next (+ k 1)))))) 9 | (next 0)) 10 | -------------------------------------------------------------------------------- /resources/code/2.2.3.20.clj: -------------------------------------------------------------------------------- 1 | (defn fold-left [op initial sequence] 2 | (defn iter [result more] 3 | (if (empty? more) 4 | result 5 | (iter (op result (first more)) 6 | (rest more)))) 7 | (iter initial sequence)) 8 | -------------------------------------------------------------------------------- /resources/code/2.2.3.21.clj: -------------------------------------------------------------------------------- 1 | (defn fold-left [op initial items] 2 | (defn iter [result more] 3 | (if (empty? more) 4 | result 5 | (iter (op result (first more)) 6 | (rest more)))) 7 | (iter initial items)) 8 | -------------------------------------------------------------------------------- /resources/code/2.2.3.22.clj: -------------------------------------------------------------------------------- 1 | (fold-right / 1 (list 1 2 3)) 2 | (fold-left / 1 (list 1 2 3)) 3 | (fold-right list nil (list 1 2 3)) 4 | (fold-left list nil (list 1 2 3)) 5 | -------------------------------------------------------------------------------- /resources/code/2.2.3.23.clj: -------------------------------------------------------------------------------- 1 | (defn reverse [sequence] 2 | (fold-right (fn [x y] <-??->) nil sequence)) 3 | 4 | (defn reverse [sequence] 5 | (fold-left (fn [x y] <-??->) nil sequence)) 6 | -------------------------------------------------------------------------------- /resources/code/2.2.3.24.clj: -------------------------------------------------------------------------------- 1 | (accumulate concat 2 | nil 3 | (map (fn [i] 4 | (map (fn [j] (list i j)) 5 | (enumerate-interval 1 (- i 1)))) 6 | (enumerate-interval 1 n))) 7 | -------------------------------------------------------------------------------- /resources/code/2.2.3.25.clj: -------------------------------------------------------------------------------- 1 | (defn flatmap [proc seq] 2 | (accumulate append nil (map proc seq))) 3 | -------------------------------------------------------------------------------- /resources/code/2.2.3.26.clj: -------------------------------------------------------------------------------- 1 | (defn prime-sum? [pair] 2 | (prime? (+ (first pair) (first (rest pair))))) 3 | -------------------------------------------------------------------------------- /resources/code/2.2.3.27.clj: -------------------------------------------------------------------------------- 1 | (defn make-pair-sum [pair] 2 | (list (first pair) 3 | (first (rest pair)) 4 | (+ (first pair) (first (rest pair))))) 5 | -------------------------------------------------------------------------------- /resources/code/2.2.3.28.clj: -------------------------------------------------------------------------------- 1 | (defn prime-sum-pairs [n] 2 | (map make-pair-sum 3 | (filter prime-sum? 4 | (flatmap 5 | (fn [i] 6 | (map (fn [j] (list i j)) 7 | (enumerate-interval 1 (- i 1)))) 8 | (enumerate-interval 1 n))))) 9 | -------------------------------------------------------------------------------- /resources/code/2.2.3.29.clj: -------------------------------------------------------------------------------- 1 | (defn permutations [s] 2 | (if (empty? s) ; empty set? 3 | (list nil) ; sequence containing empty set 4 | (flatmap (fn [x] 5 | (map (fn [p] (cons x p)) 6 | (permutations (remove x s)))) 7 | s))) 8 | -------------------------------------------------------------------------------- /resources/code/2.2.3.3.clj: -------------------------------------------------------------------------------- 1 | (map square (list 1 2 3 4 5)) 2 | 3 | (1 4 9 16 25) 4 | -------------------------------------------------------------------------------- /resources/code/2.2.3.30.clj: -------------------------------------------------------------------------------- 1 | (defn remove [item sequence] 2 | (filter (fn [x] (not (= x item))) 3 | sequence)) 4 | -------------------------------------------------------------------------------- /resources/code/2.2.3.31.clj: -------------------------------------------------------------------------------- 1 | (defn queens [board-size] 2 | (defn queen-cols [k] 3 | (if (= k 0) 4 | (list empty-board) 5 | (filter 6 | (fn [positions] (safe? k positions)) 7 | (flatmap 8 | (fn (rest-of-queens) 9 | (map (fn [new-row] 10 | (adjoin-position new-row k rest-of-queens)) 11 | (enumerate-interval 1 board-size))) 12 | (queen-cols (- k 1)))))) 13 | (queen-cols board-size)) 14 | -------------------------------------------------------------------------------- /resources/code/2.2.3.32.clj: -------------------------------------------------------------------------------- 1 | (flatmap 2 | (fn [new-row] 3 | (map (fn [rest-of-queens] 4 | (adjoin-position new-row k rest-of-queens)) 5 | (queen-cols (- k 1)))) 6 | (enumerate-interval 1 board-size)) 7 | -------------------------------------------------------------------------------- /resources/code/2.2.3.4.clj: -------------------------------------------------------------------------------- 1 | (defn filter [predicate sequence] 2 | (cond (empty? sequence) nil 3 | (predicate (rest sequence)) 4 | (cons (first sequence) 5 | (filter predicate (rest sequence)))) 6 | :else (filter predicate (rest sequence))) 7 | -------------------------------------------------------------------------------- /resources/code/2.2.3.5.clj: -------------------------------------------------------------------------------- 1 | (filter odd? (list 1 2 3 4 5)) 2 | (1 3 5) 3 | -------------------------------------------------------------------------------- /resources/code/2.2.3.6.clj: -------------------------------------------------------------------------------- 1 | (defn accumulate [op initial sequence] 2 | (if (empty? sequence) 3 | initial 4 | (op (first sequence) 5 | (accumulate op initial (rest sequence))))) 6 | 7 | (accumulate + 0 (list 1 2 3 4 5)) 8 | 15 9 | 10 | (accumulate * 1 (list 1 2 3 4 5)) 11 | 120 12 | 13 | (accumulate cons nil (list 1 2 3 4 5)) 14 | (1 2 3 4 5) 15 | -------------------------------------------------------------------------------- /resources/code/2.2.3.7.clj: -------------------------------------------------------------------------------- 1 | (defn enumerate-interval [low high] 2 | (if (> low high) 3 | nil 4 | (cons low (enumerate-interval (+ low 1) high)))) 5 | 6 | (enumerate-interval 2 7) 7 | (2 3 4 5 6 7) 8 | -------------------------------------------------------------------------------- /resources/code/2.2.3.8.clj: -------------------------------------------------------------------------------- 1 | (defn enumerate-tree [tree] 2 | (cond (not (seq? tree)) (list tree) 3 | (empty? tree) nil 4 | :else (concat (enumerate-tree (first tree)) 5 | (enumerate-tree (rest tree))))) 6 | 7 | (enumerate-tree (list 1 (list 2 (list 3 4)) 5)) 8 | (1 2 3 4 5) 9 | -------------------------------------------------------------------------------- /resources/code/2.2.3.9.clj: -------------------------------------------------------------------------------- 1 | (defn sum-odd-squares [tree] 2 | (accumulate + 3 | 0 4 | (map square 5 | (filter odd? 6 | (enumerate-tree tree))))) 7 | -------------------------------------------------------------------------------- /resources/code/2.2.4.1.clj: -------------------------------------------------------------------------------- 1 | (def wave2 (beside wave (flip-vert wave))) 2 | (def wave4 (below wave2 wave2)) 3 | -------------------------------------------------------------------------------- /resources/code/2.2.4.10.clj: -------------------------------------------------------------------------------- 1 | (defn square-limit [painter n] 2 | (let [combine4 (square-of-four flip-horiz identity 3 | rotate180 flip-vert)] 4 | (combine4 (corner-split painter n)))) 5 | -------------------------------------------------------------------------------- /resources/code/2.2.4.11.clj: -------------------------------------------------------------------------------- 1 | (def right-split (split beside below)) 2 | (def up-split (split below beside)) 3 | -------------------------------------------------------------------------------- /resources/code/2.2.4.12.clj: -------------------------------------------------------------------------------- 1 | (defn frame-coord-map [frame] 2 | (fn [v] 3 | (add-vect 4 | (origin-frame frame) 5 | (add-vect (scale-vect (xcor-vect v) 6 | (edge1-frame frame)) 7 | (scale-vect (ycor-vect v) 8 | (edge2-frame frame)))))) 9 | -------------------------------------------------------------------------------- /resources/code/2.2.4.13.clj: -------------------------------------------------------------------------------- 1 | ((frame-coord-map a-frame) (make-vect 0 0)) 2 | -------------------------------------------------------------------------------- /resources/code/2.2.4.14.clj: -------------------------------------------------------------------------------- 1 | (origin-frame a-frame) 2 | -------------------------------------------------------------------------------- /resources/code/2.2.4.15.clj: -------------------------------------------------------------------------------- 1 | (defn make-frame [origin edge1 edge2] 2 | (list origin edge1 edge2)) 3 | 4 | (defn make-frame [origin edge1 edge2] 5 | (cons origin (cons edge1 edge2))) 6 | -------------------------------------------------------------------------------- /resources/code/2.2.4.16.clj: -------------------------------------------------------------------------------- 1 | (defn segments->painter [segment-list] 2 | (fn [frame] 3 | (for-each 4 | (fn [segment] 5 | (draw-line 6 | ((frame-coord-map frame) (start-segment segment)) 7 | ((frame-coord-map frame) (end-segment segment)))) 8 | segment-list))) 9 | -------------------------------------------------------------------------------- /resources/code/2.2.4.17.clj: -------------------------------------------------------------------------------- 1 | (defn transform-painter [painter origin corner1 corner2] 2 | (fn [frame] 3 | (let [m (frame-coord-map frame)] 4 | (let [new-origin (m origin)] 5 | (painter 6 | (make-frame new-origin 7 | (sub-vect (m corner1) new-origin) 8 | (sub-vect (m corner2) new-origin))))))) 9 | -------------------------------------------------------------------------------- /resources/code/2.2.4.18.clj: -------------------------------------------------------------------------------- 1 | (defn flip-vert [painter] 2 | (transform-painter painter 3 | (make-vect 0.0 1.0) ; new origin 4 | (make-vect 1.0 1.0) ; new end of edge1 5 | (make-vect 0.0 0.0))) ; new end of edge2 6 | -------------------------------------------------------------------------------- /resources/code/2.2.4.19.clj: -------------------------------------------------------------------------------- 1 | (defn shrink-to-upper-right [painter] 2 | (transform-painter painter 3 | (make-vect 0.5 0.5) 4 | (make-vect 1.0 0.5) 5 | (make-vect 0.5 1.0))) 6 | -------------------------------------------------------------------------------- /resources/code/2.2.4.2.clj: -------------------------------------------------------------------------------- 1 | (def wave2 2 | (beside wave (flip-vert wave))) 3 | 4 | (def wave4 5 | (below wave2 wave2)) 6 | -------------------------------------------------------------------------------- /resources/code/2.2.4.20.clj: -------------------------------------------------------------------------------- 1 | (defn rotate90 [painter] 2 | (transform-painter painter 3 | (make-vect 1.0 0.0) 4 | (make-vect 1.0 1.0) 5 | (make-vect 0.0 0.0))) 6 | -------------------------------------------------------------------------------- /resources/code/2.2.4.21.clj: -------------------------------------------------------------------------------- 1 | (defn squash-inwards [painter] 2 | (transform-painter painter 3 | (make-vect 0.0 0.0) 4 | (make-vect 0.65 0.35) 5 | (make-vect 0.35 0.65))) 6 | -------------------------------------------------------------------------------- /resources/code/2.2.4.22.clj: -------------------------------------------------------------------------------- 1 | (defn beside [painter1 painter2] 2 | (let [split-point (make-vect 0.5 0.0)] 3 | (let [paint-left 4 | (transform-painter painter1 5 | (make-vect 0.0 0.0) 6 | split-point 7 | (make-vect 0.0 1.0)) 8 | paint-right 9 | (transform-painter painter2 10 | split-point 11 | (make-vect 1.0 0.0) 12 | (make-vect 0.5 1.0))] 13 | (fn [frame] 14 | (paint-left frame) 15 | (paint-right frame))))) 16 | -------------------------------------------------------------------------------- /resources/code/2.2.4.3.clj: -------------------------------------------------------------------------------- 1 | (defn flipped-pairs [painter] 2 | (let [painter2 (beside painter (flip-vert painter))] 3 | (below painter2 painter2))) 4 | -------------------------------------------------------------------------------- /resources/code/2.2.4.4.clj: -------------------------------------------------------------------------------- 1 | (def wave4 (flipped-pairs wave)) 2 | -------------------------------------------------------------------------------- /resources/code/2.2.4.5.clj: -------------------------------------------------------------------------------- 1 | (defn right-split [painter n] 2 | (if (= n 0) 3 | painter 4 | (let [smaller (right-split painter (- n 1))] 5 | (beside painter (below smaller smaller))))) 6 | -------------------------------------------------------------------------------- /resources/code/2.2.4.6.clj: -------------------------------------------------------------------------------- 1 | (defn corner-split [painter n] 2 | (if (= n 0) 3 | painter 4 | (let [up (up-split painter (- n 1))] 5 | (right (right-split painter (- n 1)))) 6 | (let [top-left (beside up up)] 7 | (bottom-right (below right right)) 8 | (corner (corner-split painter (- n 1)))) 9 | (beside (below painter top-left) 10 | (below bottom-right corner)))) 11 | -------------------------------------------------------------------------------- /resources/code/2.2.4.7.clj: -------------------------------------------------------------------------------- 1 | (defn square-limit [painter n] 2 | (let [quarter (corner-split painter n)] 3 | (let [half (beside (flip-horiz quarter) quarter))] 4 | (below (flip-vert half) half)))) 5 | -------------------------------------------------------------------------------- /resources/code/2.2.4.8.clj: -------------------------------------------------------------------------------- 1 | (defn square-of-four [tl tr bl br] 2 | (fn [painter] 3 | (let [top (beside (tl painter) (tr painter)) 4 | bottom (beside (bl painter) (br painter))] 5 | (below bottom top)))) 6 | -------------------------------------------------------------------------------- /resources/code/2.2.4.9.clj: -------------------------------------------------------------------------------- 1 | (defn flipped-pairs [painter] 2 | (let [combine4 (square-of-four identity flip-vert 3 | identity flip-vert)] 4 | (combine4 painter))) 5 | -------------------------------------------------------------------------------- /resources/code/2.2.clj: -------------------------------------------------------------------------------- 1 | (defn linear-combination [a b x y] 2 | (add (mul a x) (mul b y))) 3 | -------------------------------------------------------------------------------- /resources/code/2.3.1.1.clj: -------------------------------------------------------------------------------- 1 | (a b c d) 2 | (23 45 17) 3 | ((Norah 12) (Molly 9) (Anna 7) (Lauren 6) (Charlotte 4)) 4 | -------------------------------------------------------------------------------- /resources/code/2.3.1.10.clj: -------------------------------------------------------------------------------- 1 | (= '(this is a list) '(this (is a) list)) 2 | -------------------------------------------------------------------------------- /resources/code/2.3.1.11.clj: -------------------------------------------------------------------------------- 1 | (first ''abracadabra) 2 | -------------------------------------------------------------------------------- /resources/code/2.3.1.2.clj: -------------------------------------------------------------------------------- 1 | (* (+ 23 45) (+ x 9)) 2 | 3 | (defn fact [n] (if (= n 1) 1 (* n (fact (- n 1))))) 4 | -------------------------------------------------------------------------------- /resources/code/2.3.1.3.clj: -------------------------------------------------------------------------------- 1 | (def a 1) 2 | 3 | (def b 2) 4 | 5 | => (list a b) 6 | (1 2) 7 | 8 | => (list 'a 'b) 9 | (a b) 10 | 11 | => (list 'a b) 12 | (a 2) 13 | -------------------------------------------------------------------------------- /resources/code/2.3.1.4.clj: -------------------------------------------------------------------------------- 1 | => (first '(a b c)) 2 | a 3 | 4 | => (rest '(a b c)) 5 | (b c) 6 | -------------------------------------------------------------------------------- /resources/code/2.3.1.5.clj: -------------------------------------------------------------------------------- 1 | (defn memq [item x] 2 | (cond ((empty? x) false) 3 | ((eq? item (first x)) x) 4 | (else (memq item (rest x))))) 5 | -------------------------------------------------------------------------------- /resources/code/2.3.1.6.clj: -------------------------------------------------------------------------------- 1 | (memq 'apple '(pear banana prune)) 2 | -------------------------------------------------------------------------------- /resources/code/2.3.1.7.clj: -------------------------------------------------------------------------------- 1 | (memq 'apple '(x (apple sauce) y apple pear)) 2 | -------------------------------------------------------------------------------- /resources/code/2.3.1.8.clj: -------------------------------------------------------------------------------- 1 | (list 'a 'b 'c) 2 | 3 | (list (list 'george)) 4 | (rest '((x1 x2) (y1 y2))) 5 | 6 | (first (rest '((x1 x2) (y1 y2)))) 7 | (seq? (first '(a short list))) 8 | (memq 'red '((red shoes) (blue socks))) 9 | 10 | (memq 'red '(red shoes blue socks)) 11 | -------------------------------------------------------------------------------- /resources/code/2.3.1.9.clj: -------------------------------------------------------------------------------- 1 | (= '(this is a list) '(this is a list)) 2 | -------------------------------------------------------------------------------- /resources/code/2.3.2.1.clj: -------------------------------------------------------------------------------- 1 | (variable? e) 2 | -------------------------------------------------------------------------------- /resources/code/2.3.2.10.clj: -------------------------------------------------------------------------------- 1 | (multiplicand e) 2 | -------------------------------------------------------------------------------- /resources/code/2.3.2.11.clj: -------------------------------------------------------------------------------- 1 | (defn deriv [exp v] 2 | (cond (number? exp) 0 3 | (variable? exp) 4 | (if (same-variable? exp v) 1 0) 5 | (sum? exp) 6 | (make-sum (deriv (addend exp) v) 7 | (deriv (augend exp) v)) 8 | (product? exp) 9 | (make-sum 10 | (make-product (multiplier exp) 11 | (deriv (multiplicand exp) v)) 12 | (make-product (deriv (multiplier exp) v) 13 | (multiplicand exp)))) 14 | (else 15 | (error "unknown expression type \-- DERIV" exp))) 16 | -------------------------------------------------------------------------------- /resources/code/2.3.2.2.clj: -------------------------------------------------------------------------------- 1 | (same-variable? v1 v2) 2 | -------------------------------------------------------------------------------- /resources/code/2.3.2.3.clj: -------------------------------------------------------------------------------- 1 | (sum? e) 2 | -------------------------------------------------------------------------------- /resources/code/2.3.2.4.clj: -------------------------------------------------------------------------------- 1 | (addend e) 2 | -------------------------------------------------------------------------------- /resources/code/2.3.2.5.clj: -------------------------------------------------------------------------------- 1 | (augend e) 2 | -------------------------------------------------------------------------------- /resources/code/2.3.2.6.clj: -------------------------------------------------------------------------------- 1 | (make-sum a1 a2) 2 | -------------------------------------------------------------------------------- /resources/code/2.3.2.7.clj: -------------------------------------------------------------------------------- 1 | (product? e) 2 | -------------------------------------------------------------------------------- /resources/code/2.3.2.8.clj: -------------------------------------------------------------------------------- 1 | (multiplier e) 2 | -------------------------------------------------------------------------------- /resources/code/2.3.2.9.clj: -------------------------------------------------------------------------------- 1 | (multiplicand e) 2 | -------------------------------------------------------------------------------- /resources/static/css/_solarized.scss: -------------------------------------------------------------------------------- 1 | $base03: #002b36; 2 | $base02: #073642; 3 | $base01: #586e75; 4 | $base00: #657b83; 5 | $base0: #839496; 6 | $base1: #93a1a1; 7 | $base2: #eee8d5; 8 | $base3: #fdf6e3; 9 | $yellow: #b58900; 10 | $orange: #cb4b16; 11 | $red: #d30102; 12 | $magenta: #d33682; 13 | $violet: #6c71c4; 14 | $blue: #268bd2; 15 | $cyan: #2aa198; 16 | $green: #859900; 17 | @mixin rebase($rebase03,$rebase02,$rebase01,$rebase00,$rebase0,$rebase1,$rebase2,$rebase3) 18 | { 19 | background-color:$rebase03; 20 | color:$rebase0; 21 | * { color:$rebase0; } 22 | h1,h2,h3,h4,h5,h6 { color:$rebase1; border-color: $rebase0; } 23 | a, a:active, a:visited { color: $rebase1; } 24 | } 25 | @mixin accentize($accent1, $accent2) { 26 | a, a:active, a:visited, a:hover, code.url, a > em { color: $accent1; } 27 | h1,h2,h3,h4,h5,h6 {color:$accent1;} 28 | code {color:$accent2;} 29 | .quote h1 { color: $accent2; } 30 | 31 | } 32 | @mixin solarized-light-code { 33 | /*Original Style from ethanschoonover.com/solarized (c) Jeremy Hull */ 34 | 35 | pre code { 36 | display: block; padding: 0.5em; 37 | background: #fdf6e3; color: #657b83; 38 | } 39 | 40 | pre .comment, 41 | pre .template_comment, 42 | pre .diff .header, 43 | pre .doctype, 44 | pre .pi, 45 | pre .lisp .string, 46 | pre .javadoc { 47 | color: #93a1a1; 48 | font-style: italic; 49 | } 50 | 51 | pre .keyword, 52 | pre .winutils, 53 | pre .method, 54 | pre .addition, 55 | pre .css .tag, 56 | pre .request, 57 | pre .status, 58 | pre .nginx .title { 59 | color: #859900; 60 | } 61 | 62 | pre .number, 63 | pre .command, 64 | pre .string, 65 | pre .tag .value, 66 | pre .phpdoc, 67 | pre .tex .formula, 68 | pre .regexp, 69 | pre .hexcolor { 70 | color: #2aa198; 71 | } 72 | 73 | pre .title, 74 | pre .localvars, 75 | pre .chunk, 76 | pre .decorator, 77 | pre .built_in, 78 | pre .identifier, 79 | pre .vhdl .literal, 80 | pre .id { 81 | color: #268bd2; 82 | } 83 | 84 | pre .attribute, 85 | pre .variable, 86 | pre .lisp .body, 87 | pre .smalltalk .number, 88 | pre .constant, 89 | pre .class .title, 90 | pre .parent, 91 | pre .haskell .type { 92 | color: #b58900; 93 | } 94 | 95 | pre .preprocessor, 96 | pre .preprocessor .keyword, 97 | pre .shebang, 98 | pre .symbol, 99 | pre .symbol .string, 100 | pre .diff .change, 101 | pre .special, 102 | pre .attr_selector, 103 | pre .important, 104 | pre .subst, 105 | pre .cdata, 106 | pre .clojure .title { 107 | color: #cb4b16; 108 | } 109 | 110 | pre .deletion { 111 | color: #dc322f; 112 | } 113 | 114 | pre .tex .formula { 115 | background: #eee8d5; 116 | } 117 | } 118 | 119 | @mixin solarized-dark-code { 120 | pre code { 121 | display: block; padding: 0.5em; 122 | background: #002b36; color: #839496; 123 | } 124 | 125 | pre .comment, 126 | pre .template_comment, 127 | pre .diff .header, 128 | pre .doctype, 129 | pre .pi, 130 | pre .lisp .string, 131 | pre .javadoc { 132 | color: #586e75; 133 | font-style: italic; 134 | } 135 | 136 | pre .keyword, 137 | pre .winutils, 138 | pre .method, 139 | pre .addition, 140 | pre .css .tag, 141 | pre .request, 142 | pre .status, 143 | pre .nginx .title { 144 | color: #859900; 145 | } 146 | 147 | pre .number, 148 | pre .command, 149 | pre .string, 150 | pre .tag .value, 151 | pre .phpdoc, 152 | pre .tex .formula, 153 | pre .regexp, 154 | pre .hexcolor { 155 | color: #2aa198; 156 | } 157 | 158 | pre .title, 159 | pre .localvars, 160 | pre .chunk, 161 | pre .decorator, 162 | pre .built_in, 163 | pre .identifier, 164 | pre .vhdl .literal, 165 | pre .id { 166 | color: #268bd2; 167 | } 168 | 169 | pre .attribute, 170 | pre .variable, 171 | pre .lisp .body, 172 | pre .smalltalk .number, 173 | pre .constant, 174 | pre .class .title, 175 | pre .parent, 176 | pre .haskell .type { 177 | color: #b58900; 178 | } 179 | 180 | pre .preprocessor, 181 | pre .preprocessor .keyword, 182 | pre .shebang, 183 | pre .symbol, 184 | pre .symbol .string, 185 | pre .diff .change, 186 | pre .special, 187 | pre .attr_selector, 188 | pre .important, 189 | pre .subst, 190 | pre .cdata, 191 | pre .clojure .title { 192 | color: #cb4b16; 193 | } 194 | 195 | pre .deletion { 196 | color: #dc322f; 197 | } 198 | 199 | pre .tex .formula { 200 | background: #073642; 201 | } 202 | } 203 | /* light is default mode, so pair with general html definition */ 204 | html, .light { 205 | @include rebase($base3,$base2,$base1,$base0,$base00,$base01,$base02,$base03) 206 | } 207 | 208 | html, .light { @include solarized-light-code } 209 | 210 | .dark { 211 | @include rebase($base03,$base02,$base01,$base00,$base0,$base1,$base2,$base3) 212 | } 213 | 214 | .dark { @include accentize($cyan, $yellow); } 215 | 216 | .dark { @include solarized-dark-code } 217 | 218 | html * { 219 | @include accentize($red, $blue); 220 | color-profile: sRGB; 221 | rendering-intent: auto; 222 | } 223 | -------------------------------------------------------------------------------- /resources/static/css/custom.scss: -------------------------------------------------------------------------------- 1 | @import "grid"; 2 | @import "solarized"; 3 | 4 | $sixteen: 16pt; 5 | $fourteen: 14pt; 6 | $twelve: 12pt; 7 | 8 | @mixin scalefonts($base) { 9 | body { font-size: $base; } 10 | code { font-size: $base; } 11 | footer { font-size: $base - 4pt; } 12 | .h1 { font-size: $base; } 13 | .h2 { font-size: $base - 2pt; } 14 | .h3 { font-size: $base - 4pt; } 15 | .view-source { font-size: $base - 6pt; } 16 | .text-source-link { font-size: $base - 4pt; } 17 | #toc .h1 { font-size: $base + 4pt; } 18 | #toc .h2 { font-size: $base + 2pt; } 19 | #toc .h3 { font-size: $base; } 20 | } 21 | 22 | @include scalefonts($sixteen); 23 | 24 | body { 25 | margin-top: 2em; 26 | font-family: 'Lora', 'Georgia', serif; 27 | } 28 | 29 | 30 | @media (min-width: 768px) { 31 | .sidebar { 32 | position: fixed; 33 | top: 2em; 34 | bottom: 0; 35 | overflow: auto; 36 | } 37 | } 38 | 39 | @media (max-width: 767px) { 40 | .sidebar { position:static; } 41 | footer { 42 | position: absolute; 43 | right: 4em; 44 | top: 4em; 45 | } 46 | 47 | } 48 | 49 | @media (max-width: 480px) { 50 | @include scalefonts($twelve); 51 | } 52 | 53 | @media (min-width: 768px) and (max-width: 978px) { 54 | @include scalefonts($fourteen); 55 | } 56 | 57 | h1, h2, h3 { 58 | font-weight: 700; 59 | } 60 | 61 | code { 62 | font-family: 'Ubuntu Mono', 'Monaco', monospace; 63 | } 64 | 65 | pre { 66 | margin-left: 1em; 67 | } 68 | 69 | .title h1 { font-size: 400%; } 70 | 71 | .quote { 72 | margin-bottom: 3em; 73 | } 74 | 75 | 76 | nav { 77 | position: relative; 78 | line-height: 1em; 79 | ul { 80 | list-style-type: none; 81 | padding: 0px; 82 | margin: 0px; 83 | } 84 | } 85 | 86 | footer { 87 | p { margin: 0px; } 88 | } 89 | 90 | header > ul { 91 | list-style-type: none; 92 | padding-left: 0px; 93 | } 94 | 95 | 96 | .content { 97 | max-width: 650px; 98 | padding-left: 5%; 99 | padding-right: 5%; 100 | 101 | .text-source-link { 102 | position: relative; 103 | top: -10px; 104 | font-family: 'Ubuntu Mono', 'Monaco', monospace; 105 | float: right; 106 | opacity: 0; 107 | transition: opacity 1s ease; 108 | -webkit-transition: opacity 1s ease; 109 | } 110 | } 111 | 112 | .content:hover { 113 | .text-source-link { 114 | opacity: 1; 115 | } 116 | } 117 | 118 | .chaptertext { 119 | line-height: 1.5em; 120 | } 121 | 122 | 123 | .h2 { 124 | padding-left: 10px; 125 | padding-top: 5px; 126 | } 127 | 128 | .h3 { 129 | padding-left: 20px; 130 | padding-top: 5px; 131 | } 132 | 133 | .code-div { 134 | position: relative; 135 | a { 136 | opacity: 0; 137 | transition: opacity 1s ease; 138 | -webkit-transition: opacity 1s ease; 139 | } 140 | } 141 | 142 | .code-div:hover { 143 | a { 144 | opacity: 1; 145 | } 146 | } 147 | 148 | .source-link { 149 | font-family: 'Ubuntu Mono', 'Monaco', monospace; 150 | position: absolute; 151 | padding: 0px; 152 | margin: 0px; 153 | right: 1em; 154 | bottom: -.5em; 155 | z-index: 1; 156 | } 157 | 158 | .hidden { 159 | display: none; 160 | } 161 | #edit { 162 | margin-bottom: 0px; 163 | } 164 | #colorscheme, #view-source-link { 165 | cursor: pointer; 166 | text-decoration: underline; 167 | } 168 | 169 | #toc > ul { 170 | list-style-type: none; 171 | padding-left: 0px; 172 | } 173 | -------------------------------------------------------------------------------- /resources/static/img/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecmendenhall/sicpclojure/06d4fab5c514b423b5a073a28746c757c1b097b2/resources/static/img/favicon.ico -------------------------------------------------------------------------------- /resources/static/img/fig1.1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecmendenhall/sicpclojure/06d4fab5c514b423b5a073a28746c757c1b097b2/resources/static/img/fig1.1.png -------------------------------------------------------------------------------- /resources/static/img/fig1.2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecmendenhall/sicpclojure/06d4fab5c514b423b5a073a28746c757c1b097b2/resources/static/img/fig1.2.png -------------------------------------------------------------------------------- /resources/static/img/fig1.5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecmendenhall/sicpclojure/06d4fab5c514b423b5a073a28746c757c1b097b2/resources/static/img/fig1.5.png -------------------------------------------------------------------------------- /resources/static/img/fig2.1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecmendenhall/sicpclojure/06d4fab5c514b423b5a073a28746c757c1b097b2/resources/static/img/fig2.1.png -------------------------------------------------------------------------------- /resources/static/img/fig2.2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecmendenhall/sicpclojure/06d4fab5c514b423b5a073a28746c757c1b097b2/resources/static/img/fig2.2.png -------------------------------------------------------------------------------- /resources/static/img/fig2.3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecmendenhall/sicpclojure/06d4fab5c514b423b5a073a28746c757c1b097b2/resources/static/img/fig2.3.png -------------------------------------------------------------------------------- /resources/static/img/fig2.4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecmendenhall/sicpclojure/06d4fab5c514b423b5a073a28746c757c1b097b2/resources/static/img/fig2.4.png -------------------------------------------------------------------------------- /resources/static/img/fig2.5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecmendenhall/sicpclojure/06d4fab5c514b423b5a073a28746c757c1b097b2/resources/static/img/fig2.5.png -------------------------------------------------------------------------------- /resources/static/img/fig2.6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecmendenhall/sicpclojure/06d4fab5c514b423b5a073a28746c757c1b097b2/resources/static/img/fig2.6.png -------------------------------------------------------------------------------- /resources/static/img/licensebadge-dark.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecmendenhall/sicpclojure/06d4fab5c514b423b5a073a28746c757c1b097b2/resources/static/img/licensebadge-dark.png -------------------------------------------------------------------------------- /resources/static/img/licensebadge-light.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecmendenhall/sicpclojure/06d4fab5c514b423b5a073a28746c757c1b097b2/resources/static/img/licensebadge-light.png -------------------------------------------------------------------------------- /resources/static/img/licensebadge.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecmendenhall/sicpclojure/06d4fab5c514b423b5a073a28746c757c1b097b2/resources/static/img/licensebadge.png -------------------------------------------------------------------------------- /resources/static/js/analytics.js: -------------------------------------------------------------------------------- 1 | var _gaq = _gaq || []; 2 | _gaq.push(['_setAccount', 'UA-38081658-1']); 3 | _gaq.push(['_trackPageview']); 4 | 5 | (function() { 6 | var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; 7 | ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; 8 | var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); 9 | })(); 10 | -------------------------------------------------------------------------------- /resources/static/js/highlight.pack.js: -------------------------------------------------------------------------------- 1 | define(function(){var a=new function(){function m(p){return p.replace(/&/gm,"&").replace(//gm,">")}function c(q){for(var p=q.firstChild;p;p=p.nextSibling){if(p.nodeName=="CODE"){return p}if(!(p.nodeType==3&&p.nodeValue.match(/\s+/))){break}}}function i(q,p){return Array.prototype.map.call(q.childNodes,function(r){if(r.nodeType==3){return p?r.nodeValue.replace(/\n/g,""):r.nodeValue}if(r.nodeName=="BR"){return"\n"}return i(r,p)}).join("")}function b(r){var q=(r.className+" "+(r.parentNode?r.parentNode.className:"")).split(/\s+/);q=q.map(function(s){return s.replace(/^language-/,"")});for(var p=0;p"}while(y.length||w.length){var v=u().splice(0,1)[0];z+=m(x.substr(q,v.offset-q));q=v.offset;if(v.event=="start"){z+=t(v.node);s.push(v.node)}else{if(v.event=="stop"){var p,r=s.length;do{r--;p=s[r];z+=("")}while(p!=v.node);s.splice(r,1);while(r'+N[0]+""}else{r+=N[0]}P=B.lR.lastIndex;N=B.lR.exec(M)}return r+M.substr(P)}function A(){if(B.sL&&!f[B.sL]){return m(x)}var r=B.sL?e(B.sL,x):h(x);if(B.r>0){w+=r.keyword_count;C+=r.r}return''+r.value+""}function L(){return B.sL!==undefined?A():I()}function K(N,r){var M=N.cN?'':"";if(N.rB){y+=M;x=""}else{if(N.eB){y+=m(r)+M;x=""}else{y+=M;x=r}}B=Object.create(N,{parent:{value:B}});C+=N.r}function E(M,r){x+=M;if(r===undefined){y+=L();return 0}var N=p(r,B);if(N){y+=L();K(N,r);return N.rB?0:r.length}var O=t(B,r);if(O){if(!(O.rE||O.eE)){x+=r}y+=L();do{if(B.cN){y+=""}B=B.parent}while(B!=O.parent);if(O.eE){y+=m(r)}x="";if(O.starts){K(O.starts,"")}return O.rE?0:r.length}if(u(r,B)){throw"Illegal"}x+=r;return r.length||1}var H=f[F];g(H);var B=H;var x="";var C=0;var w=0;var y="";try{var v,s,q=0;while(true){B.t.lastIndex=q;v=B.t.exec(G);if(!v){break}s=E(G.substr(q,v.index-q),v[0]);q=v.index+s}E(G.substr(q));return{r:C,keyword_count:w,value:y,language:F}}catch(J){if(J=="Illegal"){return{r:0,keyword_count:0,value:m(G)}}else{throw J}}}function h(t){var p={keyword_count:0,r:0,value:m(t)};var r=p;for(var q in f){if(!f.hasOwnProperty(q)){continue}var s=e(q,t,false);s.language=q;if(s.keyword_count+s.r>r.keyword_count+r.r){r=s}if(s.keyword_count+s.r>p.keyword_count+p.r){r=p;p=s}}if(r.language){p.second_best=r}return p}function j(r,q,p){if(q){r=r.replace(/^((<[^>]+>|\t)+)/gm,function(t,w,v,u){return w.replace(/\t/g,q)})}if(p){r=r.replace(/\n/g,"
")}return r}function n(s,v,q){var w=i(s,q);var u=b(s);if(u=="no-highlight"){return}var x=u?e(u,w,true):h(w);u=x.language;var p=d(s);if(p.length){var r=document.createElement("pre");r.innerHTML=x.value;x.value=k(p,d(r),w)}x.value=j(x.value,v,q);var t=s.className;if(!t.match("(\\s|^)(language-)?"+u+"(\\s|$)")){t=t?(t+" "+u):u}s.innerHTML=x.value;s.className=t;s.result={language:u,kw:x.keyword_count,re:x.r};if(x.second_best){s.second_best={language:x.second_best.language,kw:x.second_best.keyword_count,re:x.second_best.r}}}function o(){if(o.called){return}o.called=true;Array.prototype.map.call(document.getElementsByTagName("pre"),c).filter(Boolean).forEach(function(p){n(p,a.tabReplace)})}function l(){window.addEventListener("DOMContentLoaded",o,false);window.addEventListener("load",o,false)}var f={};this.LANGUAGES=f;this.highlight=e;this.highlightAuto=h;this.fixMarkup=j;this.highlightBlock=n;this.initHighlighting=o;this.initHighlightingOnLoad=l;this.IR="[a-zA-Z][a-zA-Z0-9_]*";this.UIR="[a-zA-Z_][a-zA-Z0-9_]*";this.NR="\\b\\d+(\\.\\d+)?";this.CNR="(\\b0[xX][a-fA-F0-9]+|(\\b\\d+(\\.\\d*)?|\\.\\d+)([eE][-+]?\\d+)?)";this.BNR="\\b(0b[01]+)";this.RSR="!|!=|!==|%|%=|&|&&|&=|\\*|\\*=|\\+|\\+=|,|\\.|-|-=|/|/=|:|;|<<|<<=|<=|<|===|==|=|>>>=|>>=|>=|>>>|>>|>|\\?|\\[|\\{|\\(|\\^|\\^=|\\||\\|=|\\|\\||~";this.BE={b:"\\\\[\\s\\S]",r:0};this.ASM={cN:"string",b:"'",e:"'",i:"\\n",c:[this.BE],r:0};this.QSM={cN:"string",b:'"',e:'"',i:"\\n",c:[this.BE],r:0};this.CLCM={cN:"comment",b:"//",e:"$"};this.CBLCLM={cN:"comment",b:"/\\*",e:"\\*/"};this.HCM={cN:"comment",b:"#",e:"$"};this.NM={cN:"number",b:this.NR,r:0};this.CNM={cN:"number",b:this.CNR,r:0};this.BNM={cN:"number",b:this.BNR,r:0};this.inherit=function(r,s){var p={};for(var q in r){p[q]=r[q]}if(s){for(var q in s){p[q]=s[q]}}return p}}();a.LANGUAGES.clojure=function(m){var f={built_in:"def cond apply if-not if-let if not not= = < < > <= <= >= == + / * - rem quot neg? pos? delay? symbol? keyword? true? false? integer? empty? coll? list? set? ifn? fn? associative? sequential? sorted? counted? reversible? number? decimal? class? distinct? isa? float? rational? reduced? ratio? odd? even? char? seq? vector? string? map? nil? contains? zero? instance? not-every? not-any? libspec? -> ->> .. . inc compare do dotimes mapcat take remove take-while drop letfn drop-last take-last drop-while while intern condp case reduced cycle split-at split-with repeat replicate iterate range merge zipmap declare line-seq sort comparator sort-by dorun doall nthnext nthrest partition eval doseq await await-for let agent atom send send-off release-pending-sends add-watch mapv filterv remove-watch agent-error restart-agent set-error-handler error-handler set-error-mode! error-mode shutdown-agents quote var fn loop recur throw try monitor-enter monitor-exit defmacro defn defn- macroexpand macroexpand-1 for doseq dosync dotimes and or when when-not when-let comp juxt partial sequence memoize constantly complement identity assert peek pop doto proxy defstruct first rest cons defprotocol cast coll deftype defrecord last butlast sigs reify second ffirst fnext nfirst nnext defmulti defmethod meta with-meta ns in-ns create-ns import intern refer keys select-keys vals key val rseq name namespace promise into transient persistent! conj! assoc! dissoc! pop! disj! import use class type num float double short byte boolean bigint biginteger bigdec print-method print-dup throw-if throw printf format load compile get-in update-in pr pr-on newline flush read slurp read-line subvec with-open memfn time ns assert re-find re-groups rand-int rand mod locking assert-valid-fdecl alias namespace resolve ref deref refset swap! reset! set-validator! compare-and-set! alter-meta! reset-meta! commute get-validator alter ref-set ref-history-count ref-min-history ref-max-history ensure sync io! new next conj set! memfn to-array future future-call into-array aset gen-class reduce merge map filter find empty hash-map hash-set sorted-map sorted-map-by sorted-set sorted-set-by vec vector seq flatten reverse assoc dissoc list disj get union difference intersection extend extend-type extend-protocol int nth delay count concat chunk chunk-buffer chunk-append chunk-first chunk-rest max min dec unchecked-inc-int unchecked-inc unchecked-dec-inc unchecked-dec unchecked-negate unchecked-add-int unchecked-add unchecked-subtract-int unchecked-subtract chunk-next chunk-cons chunked-seq? prn vary-meta lazy-seq spread list* str find-keyword keyword symbol gensym force rationalize"};var g="[a-zA-Z_0-9\\!\\.\\?\\-\\+\\*\\/\\<\\=\\>\\&\\#\\$';]+";var b="[\\s:\\(\\{]+\\d+(\\.\\d+)?";var e={cN:"number",b:b,r:0};var l={cN:"string",b:'"',e:'"',c:[m.BE],r:0};var p={cN:"comment",b:";",e:"$",r:0};var o={cN:"collection",b:"[\\[\\{]",e:"[\\]\\}]"};var d={cN:"comment",b:"\\^"+g};var c={cN:"comment",b:"\\^\\{",e:"\\}"};var i={cN:"attribute",b:"[:]"+g};var n={cN:"list",b:"\\(",e:"\\)"};var h={eW:true,eE:true,k:{literal:"true false nil"},r:0};var j={k:f,l:g,cN:"title",b:g,starts:h};n.c=[{cN:"comment",b:"comment"},j];h.c=[n,l,d,c,p,i,o,e];o.c=[n,l,d,p,i,o,e];return{i:"\\S",c:[p,n]}}(a);return a}); -------------------------------------------------------------------------------- /resources/static/js/main.js: -------------------------------------------------------------------------------- 1 | require(["mathjax"], function(mjsettings) { 2 | mjsettings.injectConfig(mjsettings.mathJaxConfig); 3 | mjsettings.mathJaxDefaults(); 4 | }); 5 | 6 | require(["highlight.pack"], function(hljs) { 7 | hljs.initHighlighting(); 8 | }); 9 | -------------------------------------------------------------------------------- /resources/static/js/mathjax.js: -------------------------------------------------------------------------------- 1 | define( function () { 2 | 3 | function customMathJaxSettings () { 4 | 5 | this.injectConfig = function (fn, type) { 6 | var script = document.createElement('script'); 7 | script.setAttribute("type", "text/x-mathjax-config"); 8 | script.textContent = '(' + fn + ')();'; 9 | document.body.appendChild(script); 10 | }; 11 | 12 | this.mathJaxConfig = function () { 13 | MathJax.Hub.Config({ 14 | messageStyle: "none", 15 | config: ["MMLorHTML.js"], 16 | jax: ["input/TeX", 17 | "input/MathML", 18 | "output/HTML-CSS", 19 | "output/NativeMML"], 20 | extensions: ["tex2jax.js", 21 | "mml2jax.js", 22 | "MathMenu.js", 23 | "MathZoom.js"], 24 | TeX: { 25 | extensions: ["AMSmath.js", 26 | "AMSsymbols.js", 27 | "noErrors.js", 28 | "noUndefined.js"] 29 | } 30 | }); 31 | }; 32 | 33 | this.mathJaxDefaults = function () { 34 | var script = document.createElement('script'); 35 | script.setAttribute("type", "text/javascript"); 36 | script.src = "http://cdn.mathjax.org/mathjax/latest/MathJax.js"; 37 | document.body.appendChild(script); 38 | }; 39 | } 40 | 41 | return new customMathJaxSettings(); 42 | }); 43 | -------------------------------------------------------------------------------- /resources/static/js/modernizr.js: -------------------------------------------------------------------------------- 1 | /* Modernizr 2.6.2 (Custom Build) | MIT & BSD 2 | * Build: http://modernizr.com/download/#-canvas-canvastext-hashchange-audio-video-input-inputtypes-localstorage-websockets-webworkers-geolocation-svg-shiv-cssclasses-hasevent-domprefixes-load 3 | */ 4 | ;window.Modernizr=function(a,b,c){function A(a){j.cssText=a}function B(a,b){return A(prefixes.join(a+";")+(b||""))}function C(a,b){return typeof a===b}function D(a,b){return!!~(""+a).indexOf(b)}function E(a,b,d){for(var e in a){var f=b[a[e]];if(f!==c)return d===!1?a[e]:C(f,"function")?f.bind(d||b):f}return!1}function F(){e.input=function(c){for(var d=0,e=c.length;d7)},r.websockets=function(){return"WebSocket"in a||"MozWebSocket"in a},r.video=function(){var a=b.createElement("video"),c=!1;try{if(c=!!a.canPlayType)c=new Boolean(c),c.ogg=a.canPlayType('video/ogg; codecs="theora"').replace(/^no$/,""),c.h264=a.canPlayType('video/mp4; codecs="avc1.42E01E"').replace(/^no$/,""),c.webm=a.canPlayType('video/webm; codecs="vp8, vorbis"').replace(/^no$/,"")}catch(d){}return c},r.audio=function(){var a=b.createElement("audio"),c=!1;try{if(c=!!a.canPlayType)c=new Boolean(c),c.ogg=a.canPlayType('audio/ogg; codecs="vorbis"').replace(/^no$/,""),c.mp3=a.canPlayType("audio/mpeg;").replace(/^no$/,""),c.wav=a.canPlayType('audio/wav; codecs="1"').replace(/^no$/,""),c.m4a=(a.canPlayType("audio/x-m4a;")||a.canPlayType("audio/aac;")).replace(/^no$/,"")}catch(d){}return c},r.localstorage=function(){try{return localStorage.setItem(h,h),localStorage.removeItem(h),!0}catch(a){return!1}},r.webworkers=function(){return!!a.Worker},r.svg=function(){return!!b.createElementNS&&!!b.createElementNS(q.svg,"svg").createSVGRect};for(var G in r)z(r,G)&&(w=G.toLowerCase(),e[w]=r[G](),u.push((e[w]?"":"no-")+w));return e.input||F(),e.addTest=function(a,b){if(typeof a=="object")for(var d in a)z(a,d)&&e.addTest(d,a[d]);else{a=a.toLowerCase();if(e[a]!==c)return e;b=typeof b=="function"?b():b,typeof f!="undefined"&&f&&(g.className+=" "+(b?"":"no-")+a),e[a]=b}return e},A(""),i=k=null,function(a,b){function k(a,b){var c=a.createElement("p"),d=a.getElementsByTagName("head")[0]||a.documentElement;return c.innerHTML="x",d.insertBefore(c.lastChild,d.firstChild)}function l(){var a=r.elements;return typeof a=="string"?a.split(" "):a}function m(a){var b=i[a[g]];return b||(b={},h++,a[g]=h,i[h]=b),b}function n(a,c,f){c||(c=b);if(j)return c.createElement(a);f||(f=m(c));var g;return f.cache[a]?g=f.cache[a].cloneNode():e.test(a)?g=(f.cache[a]=f.createElem(a)).cloneNode():g=f.createElem(a),g.canHaveChildren&&!d.test(a)?f.frag.appendChild(g):g}function o(a,c){a||(a=b);if(j)return a.createDocumentFragment();c=c||m(a);var d=c.frag.cloneNode(),e=0,f=l(),g=f.length;for(;e",f="hidden"in a,j=a.childNodes.length==1||function(){b.createElement("a");var a=b.createDocumentFragment();return typeof a.cloneNode=="undefined"||typeof a.createDocumentFragment=="undefined"||typeof a.createElement=="undefined"}()}catch(c){f=!0,j=!0}})();var r={elements:c.elements||"abbr article aside audio bdi canvas data datalist details figcaption figure footer header hgroup mark meter nav output progress section summary time video",shivCSS:c.shivCSS!==!1,supportsUnknownElements:j,shivMethods:c.shivMethods!==!1,type:"default",shivDocument:q,createElement:n,createDocumentFragment:o};a.html5=r,q(b)}(this,b),e._version=d,e._domPrefixes=p,e._cssomPrefixes=o,e.hasEvent=x,g.className=g.className.replace(/(^|\s)no-js(\s|$)/,"$1$2")+(f?" js "+u.join(" "):""),e}(this,this.document),function(a,b,c){function d(a){return"[object Function]"==o.call(a)}function e(a){return"string"==typeof a}function f(){}function g(a){return!a||"loaded"==a||"complete"==a||"uninitialized"==a}function h(){var a=p.shift();q=1,a?a.t?m(function(){("c"==a.t?B.injectCss:B.injectJs)(a.s,0,a.a,a.x,a.e,1)},0):(a(),h()):q=0}function i(a,c,d,e,f,i,j){function k(b){if(!o&&g(l.readyState)&&(u.r=o=1,!q&&h(),l.onload=l.onreadystatechange=null,b)){"img"!=a&&m(function(){t.removeChild(l)},50);for(var d in y[c])y[c].hasOwnProperty(d)&&y[c][d].onload()}}var j=j||B.errorTimeout,l=b.createElement(a),o=0,r=0,u={t:d,s:c,e:f,a:i,x:j};1===y[c]&&(r=1,y[c]=[]),"object"==a?l.data=c:(l.src=c,l.type=a),l.width=l.height="0",l.onerror=l.onload=l.onreadystatechange=function(){k.call(this,r)},p.splice(e,0,u),"img"!=a&&(r||2===y[c]?(t.insertBefore(l,s?null:n),m(k,j)):y[c].push(l))}function j(a,b,c,d,f){return q=0,b=b||"j",e(a)?i("c"==b?v:u,a,b,this.i++,c,d,f):(p.splice(this.i++,0,a),1==p.length&&h()),this}function k(){var a=B;return a.loader={load:j,i:0},a}var l=b.documentElement,m=a.setTimeout,n=b.getElementsByTagName("script")[0],o={}.toString,p=[],q=0,r="MozAppearance"in l.style,s=r&&!!b.createRange().compareNode,t=s?l:n.parentNode,l=a.opera&&"[object Opera]"==o.call(a.opera),l=!!b.attachEvent&&!l,u=r?"object":l?"script":"img",v=l?"script":u,w=Array.isArray||function(a){return"[object Array]"==o.call(a)},x=[],y={},z={timeout:function(a,b){return b.length&&(a.timeout=b[0]),a}},A,B;B=function(a){function b(a){var a=a.split("!"),b=x.length,c=a.pop(),d=a.length,c={url:c,origUrl:c,prefixes:a},e,f,g;for(f=0;f We now come to the decisive step of mathematical abstraction: we forget about 6 | what the symbols stand for. ...[The mathematician] need not be idle; there 7 | are many operations which he may carry out with these symbols, without ever 8 | having to look at the things they stand for. 9 | 10 | —Hermann Weyl, _The Mathematical Way of Thinking_ 11 | 12 | We concentrated in chapter 1 on computational processes and on the role of 13 | procedures in program design. We saw how to use primitive data (numbers) and 14 | primitive operations (arithmetic operations), how to combine procedures to 15 | form compound procedures through composition, conditionals, and the use of 16 | parameters, and how to abstract procedures by using `defn`. We saw that a 17 | procedure can be regarded as a pattern for the local evolution of a process, 18 | and we classified, reasoned about, and performed simple algorithmic analyses 19 | of some common patterns for processes as embodied in procedures. We also saw 20 | that higher-order procedures enhance the power of our language by enabling us 21 | to manipulate, and thereby to reason in terms of, general methods of 22 | computation. This is much of the essence of programming. 23 | 24 | In this chapter we are going to look at more complex data. All the procedures 25 | in chapter 1 operate on simple numerical data, and simple data are not 26 | sufficient for many of the problems we wish to address using computation. 27 | Programs are typically designed to model complex phenomena, and more often 28 | than not one must construct computational objects that have several parts in 29 | order to model real-world phenomena that have several aspects. Thus, whereas 30 | our focus in chapter 1 was on building abstractions by combining procedures to 31 | form compound procedures, we turn in this chapter to another key aspect of any 32 | programming language: the means it provides for building abstractions by 33 | combining data objects to form _compound data_. 34 | 35 | Why do we want compound data in a programming language? For the same reasons 36 | that we want compound procedures: to elevate the conceptual level at which we 37 | can design our programs, to increase the modularity of our designs, and to 38 | enhance the expressive power of our language. Just as the ability to define 39 | procedures enables us to deal with processes at a higher conceptual level than 40 | that of the primitive operations of the language, the ability to construct 41 | compound data objects enables us to deal with data at a higher conceptual 42 | level than that of the primitive data objects of the language. 43 | 44 | Consider the task of designing a system to perform arithmetic with rational 45 | numbers. We could imagine an operation `add-rat` that takes two rational 46 | numbers and produces their sum. In terms of simple data, a rational number can 47 | be thought of as two integers: a numerator and a denominator. Thus, we could 48 | design a program in which each rational number would be represented by two 49 | integers (a numerator and a denominator) and where `add-rat` would be 50 | implemented by two procedures (one producing the numerator of the sum and one 51 | producing the denominator). But this would be awkward, because we would then 52 | need to explicitly keep track of which numerators corresponded to which 53 | denominators. In a system intended to perform many operations on many rational 54 | numbers, such bookkeeping details would clutter the programs substantially, to 55 | say nothing of what they would do to our minds. It would be much better if we 56 | could "glue together" a numerator and denominator to form a pair -- a 57 | _compound data object_ \-- that our programs could manipulate in a way that 58 | would be consistent with regarding a rational number as a single conceptual 59 | unit. 60 | 61 | The use of compound data also enables us to increase the modularity of our 62 | programs. If we can manipulate rational numbers directly as objects in their 63 | own right, then we can separate the part of our program that deals with 64 | rational numbers per se from the details of how rational numbers may be 65 | represented as pairs of integers. The general technique of isolating the parts 66 | of a program that deal with how data objects are represented from the parts of 67 | a program that deal with how data objects are used is a powerful design 68 | methodology called _data abstraction_. We will see how data abstraction makes 69 | programs much easier to design, maintain, and modify. 70 | 71 | The use of compound data leads to a real increase in the expressive power of 72 | our programming language. Consider the idea of forming a linear 73 | combination" \\(ax + by \\). We might like to write a procedure that would 74 | accept _a_, _b_, _x_, and _y_ as arguments and return the value of 75 | \\(ax + by \\). This presents no difficulty if the arguments are to be numbers, 76 | because we can readily define the procedure 77 | 78 | {{ 2.1.clj }} 79 | 80 | But suppose we are not concerned only with numbers. Suppose we would like to 81 | express, in procedural terms, the idea that one can form linear combinations 82 | whenever addition and multiplication are defined -- for rational numbers, 83 | complex numbers, polynomials, or whatever. We could express this as a 84 | procedure of the form 85 | 86 | {{ 2.2.clj }} 87 | 88 | where `add` and `mul` are not the primitive procedures `+` and `*` but rather 89 | more complex things that will perform the appropriate operations for whatever 90 | kinds of data we pass in as the arguments `a`, `b`, `x`, and `y`. The key 91 | point is that the only thing `linear-combination` should need to know about 92 | `a`, `b`, `x`, and `y` is that the procedures `add` and `mul` will perform the 93 | appropriate manipulations. From the perspective of the procedure 94 | `linear-combination`, it is irrelevant what `a`, `b`, `x`, and `y` are and even more 95 | irrelevant how they might happen to be represented in terms of more primitive 96 | data. This same example shows why it is important that our programming 97 | language provide the ability to manipulate compound objects directly: Without 98 | this, there is no way for a procedure such as `linear-combination` to pass its 99 | arguments along to `add` and `mul` without having to know their detailed 100 | structure.[^fn-1] We begin this chapter by implementing the rational-number 101 | arithmetic system mentioned above. This will form the background for our 102 | discussion of compound data and data abstraction. As with compound procedures, 103 | the main issue to be addressed is that of abstraction as a technique for 104 | coping with complexity, and we will see how data abstraction enables us to 105 | erect suitable _abstraction barriers_ between different parts of a program. 106 | 107 | We will see that the key to forming compound data is that a programming 108 | language should provide some kind of "glue" so that data objects can be 109 | combined to form more complex data objects. There are many possible kinds of 110 | glue. Indeed, we will discover how to form compound data using no special 111 | "data" operations at all, only procedures. This will further blur the 112 | distinction between "procedure" and "data," which was already becoming 113 | tenuous toward the end of chapter 1\. We will also explore some conventional 114 | techniques for representing sequences and trees. One key idea in dealing with 115 | compound data is the notion of _closure_ \-- that the glue we use for 116 | combining data objects should allow us to combine not only primitive data 117 | objects, but compound data objects as well. Another key idea is that compound 118 | data objects can serve as _conventional interfaces_ for combining program 119 | modules in mix-and-match ways. We illustrate some of these ideas by presenting 120 | a simple graphics language that exploits closure. 121 | 122 | We will then augment the representational power of our language by introducing 123 | _symbolic expressions_ \-- data whose elementary parts can be arbitrary 124 | symbols rather than only numbers. We explore various alternatives for 125 | representing sets of objects. We will find that, just as a given numerical 126 | function can be computed by many different computational processes, there are 127 | many ways in which a given data structure can be represented in terms of 128 | simpler objects, and the choice of representation can have significant impact 129 | on the time and space requirements of processes that manipulate the data. We 130 | will investigate these ideas in the context of symbolic differentiation, the 131 | representation of sets, and the encoding of information. 132 | 133 | Next we will take up the problem of working with data that may be represented 134 | differently by different parts of a program. This leads to the need to 135 | implement _generic operations_, which must handle many different types of 136 | data. Maintaining modularity in the presence of generic operations requires 137 | more powerful abstraction barriers than can be erected with simple data 138 | abstraction alone. In particular, we introduce _data-directed programming_ as 139 | a technique that allows individual data representations to be designed in 140 | isolation and then combined _additively_ (i.e., without modification). To 141 | illustrate the power of this approach to system design, we close the chapter 142 | by applying what we have learned to the implementation of a package for 143 | performing symbolic arithmetic on polynomials, in which the coefficients of 144 | the polynomials can be integers, rational numbers, complex numbers, and even 145 | other polynomials. 146 | 147 | * * * 148 | 149 | [fn-1] The ability to directly manipulate procedures provides an analogous increase 150 | in the expressive power of a programming language. For example, in section 151 | [1.3.1](12.html#sec_1.3.1) we introduced the `sum` procedure, which 152 | takes a procedure `term` as an argument and computes the sum of the values of 153 | `term` over some specified interval. In order to define `sum`, it is crucial 154 | that we be able to speak of a procedure such as `term` as an entity in its own 155 | right, without regard for how `term` might be expressed with more primitive 156 | operations. Indeed, if we did not have the notion of "a procedure," it is 157 | doubtful that we would ever even think of the possibility of defining an 158 | operation such as `sum`. Moreover, insofar as performing the summation is 159 | concerned, the details of how `term` may be constructed from more primitive 160 | operations are irrelevant. 161 | -------------------------------------------------------------------------------- /resources/text/19.md: -------------------------------------------------------------------------------- 1 | [Go to [first](book.html), [previous](book-Z-H-18.html), 2 | [next](book-Z-H-20.html) page; [contents](book-Z-H-4.html#%_toc_start); 3 | [index](book-Z-H-38.html#%_index_start)] 4 | 5 | # 6 | 7 | [Chapter 3](book-Z-H-4.html#chap_3) 8 | 9 | [Modularity, Objects, and State](book-Z-H-4.html#chap_3) 10 | 11 | M![](ch3-Z-G-1.gif) 12 | 13 | (Even while it changes, it stands still.) 14 | 15 | Heraclitus 16 | 17 | Plus ?a change, plus c'est la meme chose. 18 | 19 | Alphonse Karr 20 | 21 | The preceding chapters introduced the basic elements from which programs are 22 | made. We saw how primitive procedures and primitive data are combined to 23 | construct compound entities, and we learned that abstraction is vital in 24 | helping us to cope with the complexity of large systems. But these tools are 25 | not sufficient for designing programs. Effective program synthesis also 26 | requires organizational principles that can guide us in formulating the 27 | overall design of a program. In particular, we need strategies to help us 28 | structure large systems so that they will be _modular_, that is, so that they 29 | can be divided ``naturally'' into coherent parts that can be separately 30 | developed and maintained. 31 | 32 | One powerful design strategy, which is particularly appropriate to the 33 | construction of programs for modeling physical systems, is to base the 34 | structure of our programs on the structure of the system being modeled. For 35 | each object in the system, we construct a corresponding computational object. 36 | For each system action, we define a symbolic operation in our computational 37 | model. Our hope in using this strategy is that extending the model to 38 | accommodate new objects or new actions will require no strategic changes to 39 | the program, only the addition of the new symbolic analogs of those objects or 40 | actions. If we have been successful in our system organization, then to add a 41 | new feature or debug an old one we will have to work on only a localized part 42 | of the system. 43 | 44 | To a large extent, then, the way we organize a large program is dictated by 45 | our perception of the system to be modeled. In this chapter we will 46 | investigate two prominent organizational strategies arising from two rather 47 | different ``world views'' of the structure of systems. The first 48 | organizational strategy concentrates on _objects_, viewing a large system as a 49 | collection of distinct objects whose behaviors may change over time. An 50 | alternative organizational strategy concentrates on the _streams_ of 51 | information that flow in the system, much as an electrical engineer views a 52 | signal-processing system. 53 | 54 | Both the object-based approach and the stream-processing approach raise 55 | significant linguistic issues in programming. With objects, we must be 56 | concerned with how a computational object can change and yet maintain its 57 | identity. This will force us to abandon our old substitution model of 58 | computation (section [1.1.5](book-Z-H-10.html#%_sec_1.1.5)) in favor of a more 59 | mechanistic but less theoretically tractable _environment model_ of 60 | computation. The difficulties of dealing with objects, change, and identity 61 | are a fundamental consequence of the need to grapple with time in our 62 | computational models. These difficulties become even greater when we allow the 63 | possibility of concurrent execution of programs. The stream approach can be 64 | most fully exploited when we decouple simulated time in our model from the 65 | order of the events that take place in the computer during evaluation. We will 66 | accomplish this using a technique known as _delayed evaluation_. 67 | 68 | [Go to [first](book.html), [previous](book-Z-H-18.html), 69 | [next](book-Z-H-20.html) page; [contents](book-Z-H-4.html#%_toc_start); 70 | [index](book-Z-H-38.html#%_index_start)] 71 | 72 | -------------------------------------------------------------------------------- /resources/text/2.md: -------------------------------------------------------------------------------- 1 | [Go to [first](book.html), [previous](book-Z-H-1.html), 2 | [next](book-Z-H-3.html) page; [contents](book-Z-H-4.html#%_toc_start); 3 | [index](book-Z-H-38.html#%_index_start)] 4 | 5 | This book is one of a series of texts written by faculty of the Electrical 6 | Engineering and Computer Science Department at the Massachusetts Institute of 7 | Technology. It was edited and produced by The MIT Press under a joint 8 | production-distribution arrangement with the McGraw-Hill Book Company. 9 | 10 | **Ordering Information:** 11 | 12 | _North America_ 13 | Text orders should be addressed to the McGraw-Hill Book Company. 14 | All other orders should be addressed to The MIT Press. 15 | 16 | _Outside North America_ 17 | All orders should be addressed to The MIT Press or its local distributor. 18 | 19 | (C) 1996 by The Massachusetts Institute of Technology 20 | 21 | Second edition 22 | 23 | All rights reserved. No part of this book may be reproduced in any form or by 24 | any electronic or mechanical means (including photocopying, recording, or 25 | information storage and retrieval) without permission in writing from the 26 | publisher. 27 | 28 | ![Creative Commons License](http://i.creativecommons.org/l/by- 29 | nc/3.0/88x31.png) 30 | This work is licensed under a [Creative Commons Attribution-Noncommercial 3.0 31 | Unported License](http://creativecommons.org/licenses/by-nc/3.0/). 32 | 33 | This book was set by the authors using the LATEX typesetting system and was 34 | printed and bound in the United States of America. 35 | 36 | **Library of Congress Cataloging-in-Publication Data** 37 | 38 | Abelson, Harold 39 | Structure and interpretation of computer programs / Harold Abelson 40 | and Gerald Jay Sussman, with Julie Sussman. -- 2nd ed. 41 | p. cm. -- (Electrical engineering and computer science 42 | series) 43 | Includes bibliographical references and index. 44 | ISBN 0-262-01153-0 (MIT Press hardcover) 45 | ISBN 0-262-51087-1 (MIT Press paperback) 46 | ISBN 0-07-000484-6 (McGraw-Hill hardcover) 47 | 1\. Electronic digital computers -- Programming. 2. LISP (Computer 48 | program language) I. Sussman, Gerald Jay. II. Sussman, Julie. 49 | III. Title. IV. Series: MIT electrical engineering and computer 50 | science series. 51 | QA76.6.A255 1996 52 | 005.13'3 -- dc20 96-17756 53 | 54 | Fourth printing, 1999 55 | 56 | [Go to [first](book.html), [previous](book-Z-H-1.html), 57 | [next](book-Z-H-3.html) page; [contents](book-Z-H-4.html#%_toc_start); 58 | [index](book-Z-H-38.html#%_index_start)] 59 | 60 | -------------------------------------------------------------------------------- /resources/text/25.md: -------------------------------------------------------------------------------- 1 | [Go to [first](book.html), [previous](book-Z-H-24.html), 2 | [next](book-Z-H-26.html) page; [contents](book-Z-H-4.html#%_toc_start); 3 | [index](book-Z-H-38.html#%_index_start)] 4 | 5 | # 6 | 7 | [Chapter 4](book-Z-H-4.html#chap_4) 8 | 9 | [Metalinguistic Abstraction](book-Z-H-4.html#chap_4) 10 | 11 | `...` It's in words that the magic is -- Abracadabra, Open Sesame, and the 12 | rest -- but the magic words in one story aren't magical in the next. The real 13 | magic is to understand which words work, and when, and for what; the trick is 14 | to learn the trick. 15 | `...` And those words are made from the letters of our alphabet: a couple- 16 | dozen squiggles we can draw with the pen. This is the key! And the treasure, 17 | too, if we can only get our hands on it! It's as if -- as if the key to the 18 | treasure _is_ the treasure! 19 | 20 | John Barth, _Chimera_ 21 | 22 | In our study of program design, we have seen that expert programmers control 23 | the complexity of their designs with the same general techniques used by 24 | designers of all complex systems. They combine primitive elements to form 25 | compound objects, they abstract compound objects to form higher-level building 26 | blocks, and they preserve modularity by adopting appropriate large-scale views 27 | of system structure. In illustrating these techniques, we have used Lisp as a 28 | language for describing processes and for constructing computational data 29 | objects and processes to model complex phenomena in the real world. However, 30 | as we confront increasingly complex problems, we will find that Lisp, or 31 | indeed any fixed programming language, is not sufficient for our needs. We 32 | must constantly turn to new languages in order to express our ideas more 33 | effectively. Establishing new languages is a powerful strategy for controlling 34 | complexity in engineering design; we can often enhance our ability to deal 35 | with a complex problem by adopting a new language that enables us to describe 36 | (and hence to think about) the problem in a different way, using primitives, 37 | means of combination, and means of abstraction that are particularly well 38 | suited to the problem at hand.1 39 | 40 | Programming is endowed with a multitude of languages. There are physical 41 | languages, such as the machine languages for particular computers. These 42 | languages are concerned with the representation of data and control in terms 43 | of individual bits of storage and primitive machine instructions. The machine- 44 | language programmer is concerned with using the given hardware to erect 45 | systems and utilities for the efficient implementation of resource-limited 46 | computations. High-level languages, erected on a machine-language substrate, 47 | hide concerns about the representation of data as collections of bits and the 48 | representation of programs as sequences of primitive instructions. These 49 | languages have means of combination and abstraction, such as procedure 50 | definition, that are appropriate to the larger-scale organization of systems. 51 | 52 | _Metalinguistic abstraction_ \-- establishing new languages -- plays an 53 | important role in all branches of engineering design. It is particularly 54 | important to computer programming, because in programming not only can we 55 | formulate new languages but we can also implement these languages by 56 | constructing evaluators. An _evaluator_ (or _interpreter_) for a programming 57 | language is a procedure that, when applied to an expression of the language, 58 | performs the actions required to evaluate that expression. 59 | 60 | It is no exaggeration to regard this as the most fundamental idea in 61 | programming: 62 | 63 | > The evaluator, which determines the meaning of expressions in a programming 64 | language, is just another program. 65 | 66 | To appreciate this point is to change our images of ourselves as programmers. 67 | We come to see ourselves as designers of languages, rather than only users of 68 | languages designed by others. 69 | 70 | In fact, we can regard almost any program as the evaluator for some language. 71 | For instance, the polynomial manipulation system of section 72 | [2.5.3](book-Z-H-18.html#%_sec_2.5.3) embodies the rules of polynomial 73 | arithmetic and implements them in terms of operations on list-structured data. 74 | If we augment this system with procedures to read and print polynomial 75 | expressions, we have the core of a special-purpose language for dealing with 76 | problems in symbolic mathematics. The digital-logic simulator of section 77 | [3.3.4](book-Z-H-22.html#%_sec_3.3.4) and the constraint propagator of section 78 | [3.3.5](book-Z-H-22.html#%_sec_3.3.5) are legitimate languages in their own 79 | right, each with its own primitives, means of combination, and means of 80 | abstraction. Seen from this perspective, the technology for coping with large- 81 | scale computer systems merges with the technology for building new computer 82 | languages, and computer science itself becomes no more (and no less) than the 83 | discipline of constructing appropriate descriptive languages. 84 | 85 | We now embark on a tour of the technology by which languages are established 86 | in terms of other languages. In this chapter we shall use Lisp as a base, 87 | implementing evaluators as Lisp procedures. Lisp is particularly well suited 88 | to this task, because of its ability to represent and manipulate symbolic 89 | expressions. We will take the first step in understanding how languages are 90 | implemented by building an evaluator for Lisp itself. The language implemented 91 | by our evaluator will be a subset of the Scheme dialect of Lisp that we use in 92 | this book. Although the evaluator described in this chapter is written for a 93 | particular dialect of Lisp, it contains the essential structure of an 94 | evaluator for any expression-oriented language designed for writing programs 95 | for a sequential machine. (In fact, most language processors contain, deep 96 | within them, a little ``Lisp'' evaluator.) The evaluator has been simplified 97 | for the purposes of illustration and discussion, and some features have been 98 | left out that would be important to include in a production-quality Lisp 99 | system. Nevertheless, this simple evaluator is adequate to execute most of the 100 | programs in this book.2 101 | 102 | An important advantage of making the evaluator accessible as a Lisp program is 103 | that we can implement alternative evaluation rules by describing these as 104 | modifications to the evaluator program. One place where we can use this power 105 | to good effect is to gain extra control over the ways in which computational 106 | models embody the notion of time, which was so central to the discussion in 107 | chapter 3\. There, we mitigated some of the complexities of state and 108 | assignment by using streams to decouple the representation of time in the 109 | world from time in the computer. Our stream programs, however, were sometimes 110 | cumbersome, because they were constrained by the applicative-order evaluation 111 | of Scheme. In section [4.2](book-Z-H-27.html#%_sec_4.2), we'll change the 112 | underlying language to provide for a more elegant approach, by modifying the 113 | evaluator to provide for _normal-order evaluation_. 114 | 115 | Section [4.3](book-Z-H-28.html#%_sec_4.3) implements a more ambitious 116 | linguistic change, whereby expressions have many values, rather than just a 117 | single value. In this language of _nondeterministic computing_, it is natural 118 | to express processes that generate all possible values for expressions and 119 | then search for those values that satisfy certain constraints. In terms of 120 | models of computation and time, this is like having time branch into a set of 121 | ``possible futures'' and then searching for appropriate time lines. With our 122 | nondeterministic evaluator, keeping track of multiple values and performing 123 | searches are handled automatically by the underlying mechanism of the 124 | language. 125 | 126 | In section [4.4](book-Z-H-29.html#%_sec_4.4) we implement a _logic- 127 | programming_ language in which knowledge is expressed in terms of relations, 128 | rather than in terms of computations with inputs and outputs. Even though this 129 | makes the language drastically different from Lisp, or indeed from any 130 | conventional language, we will see that the logic-programming evaluator shares 131 | the essential structure of the Lisp evaluator. 132 | 133 | * * * 134 | 135 | 1 The same idea is pervasive throughout all of engineering. For example, 136 | electrical engineers use many different languages for describing circuits. Two 137 | of these are the language of electrical _networks_ and the language of 138 | electrical _systems_. The network language emphasizes the physical modeling of 139 | devices in terms of discrete electrical elements. The primitive objects of the 140 | network language are primitive electrical components such as resistors, 141 | capacitors, inductors, and transistors, which are characterized in terms of 142 | physical variables called voltage and current. When describing circuits in the 143 | network language, the engineer is concerned with the physical characteristics 144 | of a design. In contrast, the primitive objects of the system language are 145 | signal-processing modules such as filters and amplifiers. Only the functional 146 | behavior of the modules is relevant, and signals are manipulated without 147 | concern for their physical realization as voltages and currents. The system 148 | language is erected on the network language, in the sense that the elements of 149 | signal-processing systems are constructed from electrical networks. Here, 150 | however, the concerns are with the large-scale organization of electrical 151 | devices to solve a given application problem; the physical feasibility of the 152 | parts is assumed. This layered collection of languages is another example of 153 | the stratified design technique illustrated by the picture language of section 154 | [2.2.4](book-Z-H-15.html#%_sec_2.2.4). 155 | 156 | 2 The most important features that our evaluator leaves out are mechanisms for 157 | handling errors and supporting debugging. For a more extensive discussion of 158 | evaluators, see Friedman, Wand, and Haynes 1992, which gives an exposition of 159 | programming languages that proceeds via a sequence of evaluators written in 160 | Scheme. 161 | 162 | [Go to [first](book.html), [previous](book-Z-H-24.html), 163 | [next](book-Z-H-26.html) page; [contents](book-Z-H-4.html#%_toc_start); 164 | [index](book-Z-H-38.html#%_index_start)] 165 | 166 | -------------------------------------------------------------------------------- /resources/text/3.md: -------------------------------------------------------------------------------- 1 | [Go to [first](book.html), [previous](book-Z-H-2.html), 2 | [next](book-Z-H-4.html) page; [contents](book-Z-H-4.html#%_toc_start); 3 | [index](book-Z-H-38.html#%_index_start)] 4 | 5 | This book is dedicated, in respect and admiration, to the spirit that lives in 6 | the computer. 7 | 8 | ``I think that it's extraordinarily important that we in computer science keep 9 | fun in computing. When it started out, it was an awful lot of fun. Of course, 10 | the paying customers got shafted every now and then, and after a while we 11 | began to take their complaints seriously. We began to feel as if we really 12 | were responsible for the successful, error-free perfect use of these machines. 13 | I don't think we are. I think we're responsible for stretching them, setting 14 | them off in new directions, and keeping fun in the house. I hope the field of 15 | computer science never loses its sense of fun. Above all, I hope we don't 16 | become missionaries. Don't feel as if you're Bible salesmen. The world has too 17 | many of those already. What you know about computing other people will learn. 18 | Don't feel as if the key to successful computing is only in your hands. What's 19 | in your hands, I think and hope, is intelligence: the ability to see the 20 | machine as more than when you were first led up to it, that you can make it 21 | more.'' 22 | 23 | Alan J. Perlis (April 1, 1922-February 7, 1990) 24 | 25 | [Go to [first](book.html), [previous](book-Z-H-2.html), 26 | [next](book-Z-H-4.html) page; [contents](book-Z-H-4.html#%_toc_start); 27 | [index](book-Z-H-38.html#%_index_start)] 28 | 29 | -------------------------------------------------------------------------------- /resources/text/30.md: -------------------------------------------------------------------------------- 1 | [Go to [first](book.html), [previous](book-Z-H-29.html), 2 | [next](book-Z-H-31.html) page; [contents](book-Z-H-4.html#%_toc_start); 3 | [index](book-Z-H-38.html#%_index_start)] 4 | 5 | # 6 | 7 | [Chapter 5](book-Z-H-4.html#chap_5) 8 | 9 | [Computing with Register Machines](book-Z-H-4.html#chap_5) 10 | 11 | My aim is to show that the heavenly machine is not a kind of divine, live 12 | being, but a kind of clockwork (and he who believes that a clock has soul 13 | attributes the maker's glory to the work), insofar as nearly all the manifold 14 | motions are caused by a most simple and material force, just as all motions of 15 | the clock are caused by a single weight. 16 | 17 | Johannes Kepler (letter to Herwart von Hohenburg, 1605) 18 | 19 | We began this book by studying processes and by describing processes in terms 20 | of procedures written in Lisp. To explain the meanings of these procedures, we 21 | used a succession of models of evaluation: the substitution model of chapter 22 | 1, the environment model of chapter 3, and the metacircular evaluator of 23 | chapter 4\. Our examination of the metacircular evaluator, in particular, 24 | dispelled much of the mystery of how Lisp-like languages are interpreted. But 25 | even the metacircular evaluator leaves important questions unanswered, because 26 | it fails to elucidate the mechanisms of control in a Lisp system. For 27 | instance, the evaluator does not explain how the evaluation of a subexpression 28 | manages to return a value to the expression that uses this value, nor does the 29 | evaluator explain how some recursive procedures generate iterative processes 30 | (that is, are evaluated using constant space) whereas other recursive 31 | procedures generate recursive processes. These questions remain unanswered 32 | because the metacircular evaluator is itself a Lisp program and hence inherits 33 | the control structure of the underlying Lisp system. In order to provide a 34 | more complete description of the control structure of the Lisp evaluator, we 35 | must work at a more primitive level than Lisp itself. 36 | 37 | In this chapter we will describe processes in terms of the step-by-step 38 | operation of a traditional computer. Such a computer, or _register machine_, 39 | sequentially executes _instructions_ that manipulate the contents of a fixed 40 | set of storage elements called _registers_. A typical register-machine 41 | instruction applies a primitive operation to the contents of some registers 42 | and assigns the result to another register. Our descriptions of processes 43 | executed by register machines will look very much like ``machine-language'' 44 | programs for traditional computers. However, instead of focusing on the 45 | machine language of any particular computer, we will examine several Lisp 46 | procedures and design a specific register machine to execute each procedure. 47 | Thus, we will approach our task from the perspective of a hardware architect 48 | rather than that of a machine-language computer programmer. In designing 49 | register machines, we will develop mechanisms for implementing important 50 | programming constructs such as recursion. We will also present a language for 51 | describing designs for register machines. In section 52 | [5.2](book-Z-H-32.html#%_sec_5.2) we will implement a Lisp program that uses 53 | these descriptions to simulate the machines we design. 54 | 55 | Most of the primitive operations of our register machines are very simple. For 56 | example, an operation might add the numbers fetched from two registers, 57 | producing a result to be stored into a third register. Such an operation can 58 | be performed by easily described hardware. In order to deal with list 59 | structure, however, we will also use the memory operations `car`, `cdr`, and 60 | `cons`, which require an elaborate storage-allocation mechanism. In section 61 | [5.3](book-Z-H-33.html#%_sec_5.3) we study their implementation in terms of 62 | more elementary operations. 63 | 64 | In section [5.4](book-Z-H-34.html#%_sec_5.4), after we have accumulated 65 | experience formulating simple procedures as register machines, we will design 66 | a machine that carries out the algorithm described by the metacircular 67 | evaluator of section [4.1](book-Z-H-26.html#%_sec_4.1). This will fill in the 68 | gap in our understanding of how Scheme expressions are interpreted, by 69 | providing an explicit model for the mechanisms of control in the evaluator. In 70 | section [5.5](book-Z-H-35.html#%_sec_5.5) we will study a simple compiler that 71 | translates Scheme programs into sequences of instructions that can be executed 72 | directly with the registers and operations of the evaluator register machine. 73 | 74 | [Go to [first](book.html), [previous](book-Z-H-29.html), 75 | [next](book-Z-H-31.html) page; [contents](book-Z-H-4.html#%_toc_start); 76 | [index](book-Z-H-38.html#%_index_start)] 77 | 78 | -------------------------------------------------------------------------------- /resources/text/4.md: -------------------------------------------------------------------------------- 1 | [Go to [first](book.html), [previous](book-Z-H-3.html), 2 | [next](book-Z-H-5.html) page; contents; 3 | [index](book-Z-H-38.html#%_index_start)] 4 | 5 | # 6 | 7 | 8 | 9 | Contents 10 | 11 | ** [Foreword](book-Z-H-5.html#%_chap_Temp_2)** 12 | 13 | ** [Preface to the Second Edition](book-Z-H-6.html#%_chap_Temp_3)** 14 | 15 | ** [Preface to the First Edition](book-Z-H-7.html#%_chap_Temp_4)** 16 | 17 | ** [Acknowledgments](book-Z-H-8.html#%_chap_Temp_5)** 18 | 19 | ** [1 Building Abstractions with Procedures](book-Z-H-9.html#%_chap_1)** 20 | [1.1 The Elements of Programming](book-Z-H-10.html#%_sec_1.1) 21 | [1.1.1 Expressions](book-Z-H-10.html#%_sec_1.1.1) 22 | [1.1.2 Naming and the Environment](book-Z-H-10.html#%_sec_1.1.2) 23 | [1.1.3 Evaluating Combinations](book-Z-H-10.html#%_sec_1.1.3) 24 | [1.1.4 Compound Procedures](book-Z-H-10.html#%_sec_1.1.4) 25 | [1.1.5 The Substitution Model for Procedure Application](book-Z-H-10.html#%_sec_1.1.5) 26 | [1.1.6 Conditional Expressions and Predicates](book-Z-H-10.html#%_sec_1.1.6) 27 | [1.1.7 Example: Square Roots by Newton's Method](book-Z-H-10.html#%_sec_1.1.7) 28 | [1.1.8 Procedures as Black-Box Abstractions](book-Z-H-10.html#%_sec_1.1.8) 29 | [1.2 Procedures and the Processes They Generate](book-Z-H-11.html#%_sec_1.2) 30 | [1.2.1 Linear Recursion and Iteration](book-Z-H-11.html#%_sec_1.2.1) 31 | [1.2.2 Tree Recursion](book-Z-H-11.html#%_sec_1.2.2) 32 | [1.2.3 Orders of Growth](book-Z-H-11.html#%_sec_1.2.3) 33 | [1.2.4 Exponentiation](book-Z-H-11.html#%_sec_1.2.4) 34 | [1.2.5 Greatest Common Divisors](book-Z-H-11.html#%_sec_1.2.5) 35 | [1.2.6 Example: Testing for Primality](book-Z-H-11.html#%_sec_1.2.6) 36 | [1.3 Formulating Abstractions with Higher-Order Procedures](book-Z-H-12.html#%_sec_1.3) 37 | [1.3.1 Procedures as Arguments](book-Z-H-12.html#%_sec_1.3.1) 38 | [1.3.2 Constructing Procedures Using `Lambda`](book-Z-H-12.html#%_sec_1.3.2) 39 | [1.3.3 Procedures as General Methods](book-Z-H-12.html#%_sec_1.3.3) 40 | [1.3.4 Procedures as Returned Values](book-Z-H-12.html#%_sec_1.3.4) 41 | 42 | ** [2 Building Abstractions with Data](book-Z-H-13.html#%_chap_2)** 43 | [2.1 Introduction to Data Abstraction](book-Z-H-14.html#%_sec_2.1) 44 | [2.1.1 Example: Arithmetic Operations for Rational Numbers](book-Z-H-14.html#%_sec_2.1.1) 45 | [2.1.2 Abstraction Barriers](book-Z-H-14.html#%_sec_2.1.2) 46 | [2.1.3 What Is Meant by Data?](book-Z-H-14.html#%_sec_2.1.3) 47 | [2.1.4 Extended Exercise: Interval Arithmetic](book-Z-H-14.html#%_sec_2.1.4) 48 | [2.2 Hierarchical Data and the Closure Property](book-Z-H-15.html#%_sec_2.2) 49 | [2.2.1 Representing Sequences](book-Z-H-15.html#%_sec_2.2.1) 50 | [2.2.2 Hierarchical Structures](book-Z-H-15.html#%_sec_2.2.2) 51 | [2.2.3 Sequences as Conventional Interfaces](book-Z-H-15.html#%_sec_2.2.3) 52 | [2.2.4 Example: A Picture Language](book-Z-H-15.html#%_sec_2.2.4) 53 | [2.3 Symbolic Data](book-Z-H-16.html#%_sec_2.3) 54 | [2.3.1 Quotation](book-Z-H-16.html#%_sec_2.3.1) 55 | [2.3.2 Example: Symbolic Differentiation](book-Z-H-16.html#%_sec_2.3.2) 56 | [2.3.3 Example: Representing Sets](book-Z-H-16.html#%_sec_2.3.3) 57 | [2.3.4 Example: Huffman Encoding Trees](book-Z-H-16.html#%_sec_2.3.4) 58 | [2.4 Multiple Representations for Abstract Data](book-Z-H-17.html#%_sec_2.4) 59 | [2.4.1 Representations for Complex Numbers](book-Z-H-17.html#%_sec_2.4.1) 60 | [2.4.2 Tagged data](book-Z-H-17.html#%_sec_2.4.2) 61 | [2.4.3 Data-Directed Programming and Additivity](book-Z-H-17.html#%_sec_2.4.3) 62 | [2.5 Systems with Generic Operations](book-Z-H-18.html#%_sec_2.5) 63 | [2.5.1 Generic Arithmetic Operations](book-Z-H-18.html#%_sec_2.5.1) 64 | [2.5.2 Combining Data of Different Types](book-Z-H-18.html#%_sec_2.5.2) 65 | [2.5.3 Example: Symbolic Algebra](book-Z-H-18.html#%_sec_2.5.3) 66 | 67 | ** [3 Modularity, Objects, and State](book-Z-H-19.html#%_chap_3)** 68 | [3.1 Assignment and Local State](book-Z-H-20.html#%_sec_3.1) 69 | [3.1.1 Local State Variables](book-Z-H-20.html#%_sec_3.1.1) 70 | [3.1.2 The Benefits of Introducing Assignment](book-Z-H-20.html#%_sec_3.1.2) 71 | [3.1.3 The Costs of Introducing Assignment](book-Z-H-20.html#%_sec_3.1.3) 72 | [3.2 The Environment Model of Evaluation](book-Z-H-21.html#%_sec_3.2) 73 | [3.2.1 The Rules for Evaluation](book-Z-H-21.html#%_sec_3.2.1) 74 | [3.2.2 Applying Simple Procedures](book-Z-H-21.html#%_sec_3.2.2) 75 | [3.2.3 Frames as the Repository of Local State](book-Z-H-21.html#%_sec_3.2.3) 76 | [3.2.4 Internal Definitions](book-Z-H-21.html#%_sec_3.2.4) 77 | [3.3 Modeling with Mutable Data](book-Z-H-22.html#%_sec_3.3) 78 | [3.3.1 Mutable List Structure](book-Z-H-22.html#%_sec_3.3.1) 79 | [3.3.2 Representing Queues](book-Z-H-22.html#%_sec_3.3.2) 80 | [3.3.3 Representing Tables](book-Z-H-22.html#%_sec_3.3.3) 81 | [3.3.4 A Simulator for Digital Circuits](book-Z-H-22.html#%_sec_3.3.4) 82 | [3.3.5 Propagation of Constraints](book-Z-H-22.html#%_sec_3.3.5) 83 | [3.4 Concurrency: Time Is of the Essence](book-Z-H-23.html#%_sec_3.4) 84 | [3.4.1 The Nature of Time in Concurrent Systems](book-Z-H-23.html#%_sec_3.4.1) 85 | [3.4.2 Mechanisms for Controlling Concurrency](book-Z-H-23.html#%_sec_3.4.2) 86 | [3.5 Streams](book-Z-H-24.html#%_sec_3.5) 87 | [3.5.1 Streams Are Delayed Lists](book-Z-H-24.html#%_sec_3.5.1) 88 | [3.5.2 Infinite Streams](book-Z-H-24.html#%_sec_3.5.2) 89 | [3.5.3 Exploiting the Stream Paradigm](book-Z-H-24.html#%_sec_3.5.3) 90 | [3.5.4 Streams and Delayed Evaluation](book-Z-H-24.html#%_sec_3.5.4) 91 | [3.5.5 Modularity of Functional Programs and Modularity of Objects](book-Z-H-24.html#%_sec_3.5.5) 92 | 93 | ** [4 Metalinguistic Abstraction](book-Z-H-25.html#%_chap_4)** 94 | [4.1 The Metacircular Evaluator](book-Z-H-26.html#%_sec_4.1) 95 | [4.1.1 The Core of the Evaluator](book-Z-H-26.html#%_sec_4.1.1) 96 | [4.1.2 Representing Expressions](book-Z-H-26.html#%_sec_4.1.2) 97 | [4.1.3 Evaluator Data Structures](book-Z-H-26.html#%_sec_4.1.3) 98 | [4.1.4 Running the Evaluator as a Program](book-Z-H-26.html#%_sec_4.1.4) 99 | [4.1.5 Data as Programs](book-Z-H-26.html#%_sec_4.1.5) 100 | [4.1.6 Internal Definitions](book-Z-H-26.html#%_sec_4.1.6) 101 | [4.1.7 Separating Syntactic Analysis from Execution](book-Z-H-26.html#%_sec_4.1.7) 102 | [4.2 Variations on a Scheme -- Lazy Evaluation](book-Z-H-27.html#%_sec_4.2) 103 | [4.2.1 Normal Order and Applicative Order](book-Z-H-27.html#%_sec_4.2.1) 104 | [4.2.2 An Interpreter with Lazy Evaluation](book-Z-H-27.html#%_sec_4.2.2) 105 | [4.2.3 Streams as Lazy Lists](book-Z-H-27.html#%_sec_4.2.3) 106 | [4.3 Variations on a Scheme -- Nondeterministic Computing](book-Z-H-28.html#%_sec_4.3) 107 | [4.3.1 Amb and Search](book-Z-H-28.html#%_sec_4.3.1) 108 | [4.3.2 Examples of Nondeterministic Programs](book-Z-H-28.html#%_sec_4.3.2) 109 | [4.3.3 Implementing the `Amb` Evaluator](book-Z-H-28.html#%_sec_4.3.3) 110 | [4.4 Logic Programming](book-Z-H-29.html#%_sec_4.4) 111 | [4.4.1 Deductive Information Retrieval](book-Z-H-29.html#%_sec_4.4.1) 112 | [4.4.2 How the Query System Works](book-Z-H-29.html#%_sec_4.4.2) 113 | [4.4.3 Is Logic Programming Mathematical Logic?](book-Z-H-29.html#%_sec_4.4.3) 114 | [4.4.4 Implementing the Query System](book-Z-H-29.html#%_sec_4.4.4) 115 | 116 | ** [5 Computing with Register Machines](book-Z-H-30.html#%_chap_5)** 117 | [5.1 Designing Register Machines](book-Z-H-31.html#%_sec_5.1) 118 | [5.1.1 A Language for Describing Register Machines](book-Z-H-31.html#%_sec_5.1.1) 119 | [5.1.2 Abstraction in Machine Design](book-Z-H-31.html#%_sec_5.1.2) 120 | [5.1.3 Subroutines](book-Z-H-31.html#%_sec_5.1.3) 121 | [5.1.4 Using a Stack to Implement Recursion](book-Z-H-31.html#%_sec_5.1.4) 122 | [5.1.5 Instruction Summary](book-Z-H-31.html#%_sec_5.1.5) 123 | [5.2 A Register-Machine Simulator](book-Z-H-32.html#%_sec_5.2) 124 | [5.2.1 The Machine Model](book-Z-H-32.html#%_sec_5.2.1) 125 | [5.2.2 The Assembler](book-Z-H-32.html#%_sec_5.2.2) 126 | [5.2.3 Generating Execution Procedures for Instructions](book-Z-H-32.html#%_sec_5.2.3) 127 | [5.2.4 Monitoring Machine Performance](book-Z-H-32.html#%_sec_5.2.4) 128 | [5.3 Storage Allocation and Garbage Collection](book-Z-H-33.html#%_sec_5.3) 129 | [5.3.1 Memory as Vectors](book-Z-H-33.html#%_sec_5.3.1) 130 | [5.3.2 Maintaining the Illusion of Infinite Memory](book-Z-H-33.html#%_sec_5.3.2) 131 | [5.4 The Explicit-Control Evaluator](book-Z-H-34.html#%_sec_5.4) 132 | [5.4.1 The Core of the Explicit-Control Evaluator](book-Z-H-34.html#%_sec_5.4.1) 133 | [5.4.2 Sequence Evaluation and Tail Recursion](book-Z-H-34.html#%_sec_5.4.2) 134 | [5.4.3 Conditionals, Assignments, and Definitions](book-Z-H-34.html#%_sec_5.4.3) 135 | [5.4.4 Running the Evaluator](book-Z-H-34.html#%_sec_5.4.4) 136 | [5.5 Compilation](book-Z-H-35.html#%_sec_5.5) 137 | [5.5.1 Structure of the Compiler](book-Z-H-35.html#%_sec_5.5.1) 138 | [5.5.2 Compiling Expressions](book-Z-H-35.html#%_sec_5.5.2) 139 | [5.5.3 Compiling Combinations](book-Z-H-35.html#%_sec_5.5.3) 140 | [5.5.4 Combining Instruction Sequences](book-Z-H-35.html#%_sec_5.5.4) 141 | [5.5.5 An Example of Compiled Code](book-Z-H-35.html#%_sec_5.5.5) 142 | [5.5.6 Lexical Addressing](book-Z-H-35.html#%_sec_5.5.6) 143 | [5.5.7 Interfacing Compiled Code to the Evaluator](book-Z-H-35.html#%_sec_5.5.7) 144 | 145 | ** [References](book-Z-H-36.html#%_chap_Temp_849)** 146 | 147 | ** [List of Exercises](book-Z-H-37.html#%_chap_Temp_850)** 148 | 149 | ** [Index](book-Z-H-38.html#%_chap_Temp_851)** 150 | 151 | [Go to [first](book.html), [previous](book-Z-H-3.html), 152 | [next](book-Z-H-5.html) page; contents; 153 | [index](book-Z-H-38.html#%_index_start)] 154 | 155 | -------------------------------------------------------------------------------- /resources/text/6.md: -------------------------------------------------------------------------------- 1 | #[Preface to the Second Edition](4.html#chap_Temp_3) 2 | 3 | >Is it possible that software is not like anything else, that it is meant to be 4 | discarded: that the whole point is to always see it as a soap bubble? 5 | 6 | &emdash;Alan J. Perlis 7 | 8 | The material in this book has been the basis of MIT's entry-level computer 9 | science subject since 1980. We had been teaching this material for four years 10 | when the first edition was published, and twelve more years have elapsed until 11 | the appearance of this second edition. We are pleased that our work has been 12 | widely adopted and incorporated into other texts. We have seen our students 13 | take the ideas and programs in this book and build them in as the core of new 14 | computer systems and languages. In literal realization of an ancient Talmudic 15 | pun, our students have become our builders. We are lucky to have such capable 16 | students and such accomplished builders. 17 | 18 | In preparing this edition, we have incorporated hundreds of clarifications 19 | suggested by our own teaching experience and the comments of colleagues at MIT 20 | and elsewhere. We have redesigned most of the major programming systems in the 21 | book, including the generic-arithmetic system, the interpreters, the register- 22 | machine simulator, and the compiler; and we have rewritten all the program 23 | examples to ensure that any Scheme implementation conforming to the IEEE 24 | Scheme standard (IEEE 1990) will be able to run the code. 25 | 26 | This edition emphasizes several new themes. The most important of these is the 27 | central role played by different approaches to dealing with time in 28 | computational models: objects with state, concurrent programming, functional 29 | programming, lazy evaluation, and nondeterministic programming. We have 30 | included new sections on concurrency and nondeterminism, and we have tried to 31 | integrate this theme throughout the book. 32 | 33 | The first edition of the book closely followed the syllabus of our MIT one- 34 | semester subject. With all the new material in the second edition, it will not 35 | be possible to cover everything in a single semester, so the instructor will 36 | have to pick and choose. In our own teaching, we sometimes skip the section on 37 | logic programming (section [4.4](29.html#sec_4.4)), we have 38 | students use the register-machine simulator but we do not cover its 39 | implementation (section [5.2](32.html#sec_5.2)), and we give only a 40 | cursory overview of the compiler (section [5.5](35.html#sec_5.5)). 41 | Even so, this is still an intense course. Some instructors may wish to cover 42 | only the first three or four chapters, leaving the other material for 43 | subsequent courses. 44 | 45 | The World-Wide-Web site "www.mitpress.mit.edu/sicp" provides support for users 46 | of this book. This includes programs from the book, sample programming 47 | assignments, supplementary materials, and downloadable implementations of the 48 | Scheme dialect of Lisp. 49 | -------------------------------------------------------------------------------- /resources/text/7.md: -------------------------------------------------------------------------------- 1 | #[Preface to the First Edition](4.html#chap_Temp_4) 2 | 3 | >A computer is like a violin. You can imagine a novice trying first a 4 | phonograph and then a violin. The latter, he says, sounds terrible. That is 5 | the argument we have heard from our humanists and most of our computer 6 | scientists. Computer programs are good, they say, for particular purposes, but 7 | they aren't flexible. Neither is a violin, or a typewriter, until you learn 8 | how to use it. 9 | 10 | &emdash;Marvin Minsky, "Why Programming Is a Good 11 | Medium for Expressing Poorly-Understood and Sloppily-Formulated Ideas" 12 | 13 | "The Structure and Interpretation of Computer Programs" is the entry-level 14 | subject in computer science at the Massachusetts Institute of Technology. It 15 | is required of all students at MIT who major in electrical engineering or in 16 | computer science, as one-fourth of the "common core curriculum," which also 17 | includes two subjects on circuits and linear systems and a subject on the 18 | design of digital systems. We have been involved in the development of this 19 | subject since 1978, and we have taught this material in its present form since 20 | the fall of 1980 to between 600 and 700 students each year. Most of these 21 | students have had little or no prior formal training in computation, although 22 | many have played with computers a bit and a few have had extensive programming 23 | or hardware-design experience. 24 | 25 | Our design of this introductory computer-science subject reflects two major 26 | concerns. First, we want to establish the idea that a computer language is not 27 | just a way of getting a computer to perform operations but rather that it is a 28 | novel formal medium for expressing ideas about methodology. Thus, programs 29 | must be written for people to read, and only incidentally for machines to 30 | execute. Second, we believe that the essential material to be addressed by a 31 | subject at this level is not the syntax of particular programming-language 32 | constructs, nor clever algorithms for computing particular functions 33 | efficiently, nor even the mathematical analysis of algorithms and the 34 | foundations of computing, but rather the techniques used to control the 35 | intellectual complexity of large software systems. 36 | 37 | Our goal is that students who complete this subject should have a good feel 38 | for the elements of style and the aesthetics of programming. They should have 39 | command of the major techniques for controlling complexity in a large system. 40 | They should be capable of reading a 50-page-long program, if it is written in 41 | an exemplary style. They should know what not to read, and what they need not 42 | understand at any moment. They should feel secure about modifying a program, 43 | retaining the spirit and style of the original author. 44 | 45 | These skills are by no means unique to computer programming. The techniques we 46 | teach and draw upon are common to all of engineering design. We control 47 | complexity by building abstractions that hide details when appropriate. We 48 | control complexity by establishing conventional interfaces that enable us to 49 | construct systems by combining standard, well-understood pieces in a "mix and 50 | match" way. We control complexity by establishing new languages for 51 | describing a design, each of which emphasizes particular aspects of the design 52 | and deemphasizes others. 53 | 54 | Underlying our approach to this subject is our conviction that "computer 55 | science" is not a science and that its significance has little to do with 56 | computers. The computer revolution is a revolution in the way we think and in 57 | the way we express what we think. The essence of this change is the emergence 58 | of what might best be called _procedural epistemology_ \-- the study of the 59 | structure of knowledge from an imperative point of view, as opposed to the 60 | more declarative point of view taken by classical mathematical subjects. 61 | Mathematics provides a framework for dealing precisely with notions of "what 62 | is." Computation provides a framework for dealing precisely with notions of 63 | "how to." 64 | 65 | In teaching our material we use a dialect of the programming language Lisp. We 66 | never formally teach the language, because we don't have to. We just use it, 67 | and students pick it up in a few days. This is one great advantage of Lisp- 68 | like languages: They have very few ways of forming compound expressions, and 69 | almost no syntactic structure. All of the formal properties can be covered in 70 | an hour, like the rules of chess. After a short time we forget about syntactic 71 | details of the language (because there are none) and get on with the real 72 | issues -- figuring out what we want to compute, how we will decompose problems 73 | into manageable parts, and how we will work on the parts. Another advantage of 74 | Lisp is that it supports (but does not enforce) more of the large-scale 75 | strategies for modular decomposition of programs than any other language we 76 | know. We can make procedural and data abstractions, we can use higher-order 77 | functions to capture common patterns of usage, we can model local state using 78 | assignment and data mutation, we can link parts of a program with streams and 79 | delayed evaluation, and we can easily implement embedded languages. All of 80 | this is embedded in an interactive environment with excellent support for 81 | incremental program design, construction, testing, and debugging. We thank all 82 | the generations of Lisp wizards, starting with John McCarthy, who have 83 | fashioned a fine tool of unprecedented power and elegance. 84 | 85 | Scheme, the dialect of Lisp that we use, is an attempt to bring together the 86 | power and elegance of Lisp and Algol. From Lisp we take the metalinguistic 87 | power that derives from the simple syntax, the uniform representation of 88 | programs as data objects, and the garbage-collected heap-allocated data. From 89 | Algol we take lexical scoping and block structure, which are gifts from the 90 | pioneers of programming-language design who were on the Algol committee. We 91 | wish to cite John Reynolds and Peter Landin for their insights into the 92 | relationship of Church's lambda calculus to the structure of programming 93 | languages. We also recognize our debt to the mathematicians who scouted out 94 | this territory decades before computers appeared on the scene. These pioneers 95 | include Alonzo Church, Barkley Rosser, Stephen Kleene, and Haskell Curry. 96 | -------------------------------------------------------------------------------- /resources/text/8.md: -------------------------------------------------------------------------------- 1 | #[Acknowledgments](4.html#chap_Temp_5) 2 | 3 | We would like to thank the many people who have helped us develop this book 4 | and this curriculum. 5 | 6 | Our subject is a clear intellectual descendant of "6.231," a wonderful 7 | subject on programming linguistics and the lambda calculus taught at MIT in 8 | the late 1960s by Jack Wozencraft and Arthur Evans, Jr. 9 | 10 | We owe a great debt to Robert Fano, who reorganized MIT's introductory 11 | curriculum in electrical engineering and computer science to emphasize the 12 | principles of engineering design. He led us in starting out on this enterprise 13 | and wrote the first set of subject notes from which this book evolved. 14 | 15 | Much of the style and aesthetics of programming that we try to teach were 16 | developed in conjunction with Guy Lewis Steele Jr., who collaborated with 17 | Gerald Jay Sussman in the initial development of the Scheme language. In 18 | addition, David Turner, Peter Henderson, Dan Friedman, David Wise, and Will 19 | Clinger have taught us many of the techniques of the functional programming 20 | community that appear in this book. 21 | 22 | Joel Moses taught us about structuring large systems. His experience with the 23 | Macsyma system for symbolic computation provided the insight that one should 24 | avoid complexities of control and concentrate on organizing the data to 25 | reflect the real structure of the world being modeled. 26 | 27 | Marvin Minsky and Seymour Papert formed many of our attitudes about 28 | programming and its place in our intellectual lives. To them we owe the 29 | understanding that computation provides a means of expression for exploring 30 | ideas that would otherwise be too complex to deal with precisely. They 31 | emphasize that a student's ability to write and modify programs provides a 32 | powerful medium in which exploring becomes a natural activity. 33 | 34 | We also strongly agree with Alan Perlis that programming is lots of fun and we 35 | had better be careful to support the joy of programming. Part of this joy 36 | derives from observing great masters at work. We are fortunate to have been 37 | apprentice programmers at the feet of Bill Gosper and Richard Greenblatt. 38 | 39 | It is difficult to identify all the people who have contributed to the 40 | development of our curriculum. We thank all the lecturers, recitation 41 | instructors, and tutors who have worked with us over the past fifteen years 42 | and put in many extra hours on our subject, especially Bill Siebert, Albert 43 | Meyer, Joe Stoy, Randy Davis, Louis Braida, Eric Grimson, Rod Brooks, Lynn 44 | Stein, and Peter Szolovits. We would like to specially acknowledge the 45 | outstanding teaching contributions of Franklyn Turbak, now at Wellesley; his 46 | work in undergraduate instruction set a standard that we can all aspire to. We 47 | are grateful to Jerry Saltzer and Jim Miller for helping us grapple with the 48 | mysteries of concurrency, and to Peter Szolovits and David McAllester for 49 | their contributions to the exposition of nondeterministic evaluation in 50 | chapter 4. 51 | 52 | Many people have put in significant effort presenting this material at other 53 | universities. Some of the people we have worked closely with are Jacob 54 | Katzenelson at the Technion, Hardy Mayer at the University of California at 55 | Irvine, Joe Stoy at Oxford, Elisha Sacks at Purdue, and Jan Komorowski at the 56 | Norwegian University of Science and Technology. We are exceptionally proud of 57 | our colleagues who have received major teaching awards for their adaptations 58 | of this subject at other universities, including Kenneth Yip at Yale, Brian 59 | Harvey at the University of California at Berkeley, and Dan Huttenlocher at 60 | Cornell. 61 | 62 | Al Moye arranged for us to teach this material to engineers at Hewlett- 63 | Packard, and for the production of videotapes of these lectures. We would like 64 | to thank the talented instructors -- in particular Jim Miller, Bill Siebert, 65 | and Mike Eisenberg -- who have designed continuing education courses 66 | incorporating these tapes and taught them at universities and industry all 67 | over the world. 68 | 69 | Many educators in other countries have put in significant work translating the 70 | first edition. Michel Briand, Pierre Chamard, and Andre Pic produced a French 71 | edition; Susanne Daniels-Herold produced a German edition; and Fumio Motoyoshi 72 | produced a Japanese edition. We do not know who produced the Chinese edition, 73 | but we consider it an honor to have been selected as the subject of an 74 | "unauthorized" translation. 75 | 76 | It is hard to enumerate all the people who have made technical contributions 77 | to the development of the Scheme systems we use for instructional purposes. In 78 | addition to Guy Steele, principal wizards have included Chris Hanson, Joe 79 | Bowbeer, Jim Miller, Guillermo Rozas, and Stephen Adams. Others who have put 80 | in significant time are Richard Stallman, Alan Bawden, Kent Pitman, Jon Taft, 81 | Neil Mayle, John Lamping, Gwyn Osnos, Tracy Larrabee, George Carrette, Soma 82 | Chaudhuri, Bill Chiarchiaro, Steven Kirsch, Leigh Klotz, Wayne Noss, Todd 83 | Cass, Patrick O'Donnell, Kevin Theobald, Daniel Weise, Kenneth Sinclair, 84 | Anthony Courtemanche, Henry M. Wu, Andrew Berlin, and Ruth Shyu. 85 | 86 | Beyond the MIT implementation, we would like to thank the many people who 87 | worked on the IEEE Scheme standard, including William Clinger and Jonathan 88 | Rees, who edited the R4RS, and Chris Haynes, David Bartley, Chris Hanson, and 89 | Jim Miller, who prepared the IEEE standard. 90 | 91 | Dan Friedman has been a long-time leader of the Scheme community. The 92 | community's broader work goes beyond issues of language design to encompass 93 | significant educational innovations, such as the high-school curriculum based 94 | on EdScheme by Schemer's Inc., and the wonderful books by Mike Eisenberg and 95 | by Brian Harvey and Matthew Wright. 96 | 97 | We appreciate the work of those who contributed to making this a real book, 98 | especially Terry Ehling, Larry Cohen, and Paul Bethge at the MIT Press. Ella 99 | Mazel found the wonderful cover image. For the second edition we are 100 | particularly grateful to Bernard and Ella Mazel for help with the book design, 101 | and to David Jones, TEX wizard extraordinaire. We also are indebted to those 102 | readers who made penetrating comments on the new draft: Jacob Katzenelson, 103 | Hardy Mayer, Jim Miller, and especially Brian Harvey, who did unto this book 104 | as Julie did unto his book _Simply Scheme_. 105 | 106 | Finally, we would like to acknowledge the support of the organizations that 107 | have encouraged this work over the years, including support from Hewlett- 108 | Packard, made possible by Ira Goldstein and Joel Birnbaum, and support from 109 | DARPA, made possible by Bob Kahn. 110 | -------------------------------------------------------------------------------- /resources/text/9.md: -------------------------------------------------------------------------------- 1 | #[Chapter 1](4.html) 2 | ##[Building Abstractions with Procedures](4.html#chap_1) 3 | 4 | > The acts of the mind, wherein it exerts its power over simple ideas, are 5 | chiefly these three: 1. Combining several simple ideas into one compound one, 6 | and thus all complex ideas are made. 2. The second is bringing two ideas, 7 | whether simple or complex, together, and setting them by one another so as to 8 | take a view of them at once, without uniting them into one, by which it gets 9 | all its ideas of relations. 3. The third is separating them from all other 10 | ideas that accompany them in their real existence: this is called abstraction, 11 | and thus all its general ideas are made. 12 | 13 | —John Locke, _An Essay Concerning Human Understanding_ (1690) 14 | 15 | We are about to study the idea of a _computational process_. Computational 16 | processes are abstract beings that inhabit computers. As they evolve, 17 | processes manipulate other abstract things called _data_. The evolution of a 18 | process is directed by a pattern of rules called a _program_. People create 19 | programs to direct processes. In effect, we conjure the spirits of the 20 | computer with our spells. 21 | 22 | A computational process is indeed much like a sorcerer's idea of a spirit. It 23 | cannot be seen or touched. It is not composed of matter at all. However, it is 24 | very real. It can perform intellectual work. It can answer questions. It can 25 | affect the world by disbursing money at a bank or by controlling a robot arm 26 | in a factory. The programs we use to conjure processes are like a sorcerer's 27 | spells. They are carefully composed from symbolic expressions in arcane and 28 | esoteric _programming languages_ that prescribe the tasks we want our 29 | processes to perform. 30 | 31 | A computational process, in a correctly working computer, executes programs 32 | precisely and accurately. Thus, like the sorcerer's apprentice, novice 33 | programmers must learn to understand and to anticipate the consequences of 34 | their conjuring. Even small errors (usually called _bugs_ or _glitches_) in 35 | programs can have complex and unanticipated consequences. 36 | 37 | Fortunately, learning to program is considerably less dangerous than learning 38 | sorcery, because the spirits we deal with are conveniently contained in a 39 | secure way. Real-world programming, however, requires care, expertise, and 40 | wisdom. A small bug in a computer-aided design program, for example, can lead 41 | to the catastrophic collapse of an airplane or a dam or the self-destruction 42 | of an industrial robot. 43 | 44 | Master software engineers have the ability to organize programs so that they 45 | can be reasonably sure that the resulting processes will perform the tasks 46 | intended. They can visualize the behavior of their systems in advance. They 47 | know how to structure programs so that unanticipated problems do not lead to 48 | catastrophic consequences, and when problems do arise, they can _debug_ their 49 | programs. Well-designed computational systems, like well-designed automobiles 50 | or nuclear reactors, are designed in a modular manner, so that the parts can 51 | be constructed, replaced, and debugged separately. 52 | 53 | #### [Programming in Lisp](4.html#sec_Temp_6) 54 | 55 | We need an appropriate language for describing processes, and we will use for 56 | this purpose the programming language Lisp. Just as our everyday thoughts are 57 | usually expressed in our natural language (such as English, French, or 58 | Japanese), and descriptions of quantitative phenomena are expressed with 59 | mathematical notations, our procedural thoughts will be expressed in Lisp. 60 | Lisp was invented in the late 1950s as a formalism for reasoning about the use 61 | of certain kinds of logical expressions, called _recursion equations_, as a 62 | model for computation. The language was conceived by John McCarthy and is 63 | based on his paper "Recursive Functions of Symbolic Expressions and Their 64 | Computation by Machine" (McCarthy 1960). 65 | 66 | Despite its inception as a mathematical formalism, Lisp is a practical 67 | programming language. A Lisp _interpreter_ is a machine that carries out 68 | processes described in the Lisp language. The first Lisp interpreter was 69 | implemented by McCarthy with the help of colleagues and students in the 70 | Artificial Intelligence Group of the MIT Research Laboratory of Electronics 71 | and in the MIT Computation Center.[^fn-1] Lisp, whose name is an acronym for LISt 72 | Processing, was designed to provide symbol-manipulating capabilities for 73 | attacking programming problems such as the symbolic differentiation and 74 | integration of algebraic expressions. It included for this purpose new data 75 | objects known as atoms and lists, which most strikingly set it apart from all 76 | other languages of the period. 77 | 78 | Lisp was not the product of a concerted design effort. Instead, it evolved 79 | informally in an experimental manner in response to users' needs and to 80 | pragmatic implementation considerations. Lisp's informal evolution has 81 | continued through the years, and the community of Lisp users has traditionally 82 | resisted attempts to promulgate any "official" definition of the language. 83 | This evolution, together with the flexibility and elegance of the initial 84 | conception, has enabled Lisp, which is the second oldest language in 85 | widespread use today (only Fortran is older), to continually adapt to 86 | encompass the most modern ideas about program design. Thus, Lisp is by now a 87 | family of dialects, which, while sharing most of the original features, may 88 | differ from one another in significant ways. The dialect of Lisp used in this 89 | book is called Clojure.[^fn-2] 90 | 91 | Because of its experimental character and its emphasis on symbol manipulation, 92 | Lisp was at first very inefficient for numerical computations, at least in 93 | comparison with Fortran. Over the years, however, Lisp compilers have been 94 | developed that translate programs into machine code that can perform numerical 95 | computations reasonably efficiently. And for special applications, Lisp has 96 | been used with great effectiveness.[^fn-3] Although Lisp has not yet overcome its 97 | old reputation as hopelessly inefficient, Lisp is now used in many 98 | applications where efficiency is not the central concern. For example, Lisp 99 | has become a language of choice for operating-system shell languages and for 100 | extension languages for editors and computer-aided design systems. 101 | 102 | If Lisp is not a mainstream language, why are we using it as the framework for 103 | our discussion of programming? Because the language possesses unique features 104 | that make it an excellent medium for studying important programming constructs 105 | and data structures and for relating them to the linguistic features that 106 | support them. The most significant of these features is the fact that Lisp 107 | descriptions of processes, called _procedures_, can themselves be represented 108 | and manipulated as Lisp data. The importance of this is that there are 109 | powerful program-design techniques that rely on the ability to blur the 110 | traditional distinction between "passive" data and "active" processes. As 111 | we shall discover, Lisp's flexibility in handling procedures as data makes it 112 | one of the most convenient languages in existence for exploring these 113 | techniques. The ability to represent procedures as data also makes Lisp an 114 | excellent language for writing programs that must manipulate other programs as 115 | data, such as the interpreters and compilers that support computer languages. 116 | Above and beyond these considerations, programming in Lisp is great fun. 117 | 118 | * * * 119 | 120 | [fn-1] The _Lisp 1 Programmer's Manual_ appeared in 1960, and the _Lisp 1.5 121 | Programmer's Manual_ (McCarthy 1965) was published in 1962. The early history 122 | of Lisp is described in McCarthy 1978. 123 | 124 | [fn-2] The two dialects in which most major Lisp programs of the 1970s were written 125 | are MacLisp (Moon 1978; Pitman 1983), developed at the MIT Project MAC, and 126 | Interlisp (Teitelman 1974), developed at Bolt Beranek and Newman Inc. and the 127 | Xerox Palo Alto Research Center. Portable Standard Lisp (Hearn 1969; Griss 128 | 1981) was a Lisp dialect designed to be easily portable between different 129 | machines. MacLisp spawned a number of subdialects, such as Franz Lisp, which 130 | was developed at the University of California at Berkeley, and Zetalisp (Moon 131 | 1981), which was based on a special-purpose processor designed at the MIT 132 | Artificial Intelligence Laboratory to run Lisp very efficiently. The Lisp 133 | used in the original version of this book, called Scheme (Steele 1975), 134 | was invented in 1975 by Guy Lewis Steele Jr. and Gerald Jay Sussman of the MIT 135 | Artificial Intelligence Laboratory and later reimplemented for instructional 136 | use at MIT. Scheme became an IEEE standard in 1990 (IEEE 1990). The Common Lisp 137 | dialect (Steele 1982, Steele 1990) was developed by the Lisp community to combine 138 | features from the earlier Lisp dialects to make an industrial standard for 139 | Lisp. Common Lisp became an ANSI standard in 1994 (ANSI 1994). 140 | 141 | [fn-3] One such special application was a breakthrough computation of scientific 142 | importance -- an integration of the motion of the Solar System that extended 143 | previous results by nearly two orders of magnitude, and demonstrated that the 144 | dynamics of the Solar System is chaotic. This computation was made possible by 145 | new integration algorithms, a special-purpose compiler, and a special-purpose 146 | computer all implemented with the aid of software tools written in Lisp 147 | (Abelson et al. 1992; Sussman and Wisdom 1992). 148 | -------------------------------------------------------------------------------- /resources/text/about.md: -------------------------------------------------------------------------------- 1 | # [About this project](about.html) 2 | 3 | This is a version of the classic [_Structure and Interpretation of Computer Programs_](http://mitpress.mit.edu/sicp/) 4 | by Hal Abelson and Jerry & Julie Sussman, revised with code examples in [Clojure](http://clojure.org) instead 5 | of the original [Scheme](http://en.wikipedia.org/wiki/Scheme_\(programming_language\)). 6 | 7 | The original text is licensed under a Creative Commons [BY-SA 3.0](http://creativecommons.org/licenses/by-sa/3.0/) 8 | license (so this site is too). If you successfully complete all the exercises, 9 | [Peter Norvig](http://www.amazon.com/review/R403HR4VL71K8) will send you a free wizard hat. 10 | 11 | The [code samples](https://github.com/ecmendenhall/sicpclojure/tree/master/resources/code) and 12 | [chapter text](https://github.com/ecmendenhall/sicpclojure/tree/master/resources/text) are available 13 | on [GitHub](https://github.com/ecmendenhall/sicpclojure), so you can contribute to the project yourself. If you see 14 | an example that could be improved, want to add a footnote or aside about Clojure, or want to jump in and edit 15 | one of the uncompleted chapters, please make a fix and submit a pull request. Guidelines for editing the source text 16 | are posted [here](https://github.com/ecmendenhall/sicpclojure/blob/master/README.md). I'm a newcomer to Clojure (the 17 | static site generator that powers this site is my first real project), so I hope more experienced Clojurists will 18 | help out. I'll be posting chapters [bird by bird](http://www.goodreads.com/book/show/12543.Bird_by_Bird) as I work 19 | through SICP myself, so it might be a while before the whole book is finished. 20 | 21 | Behind the scenes, this site uses [Requirejs](http://requirejs.org/), [MathJax](http://www.mathjax.org/), 22 | [Highlight.js](http://softwaremaniacs.org/soft/highlight/en/), [Google Web Fonts](http://www.google.com/webfonts), 23 | and the [Solarized](http://ethanschoonover.com/solarized) [sass](http://sass-lang.com/) template, plus 24 | the [pegdown](https://github.com/sirthias/pegdown) markdown processor, [Hiccup](https://github.com/weavejester/hiccup) 25 | templates, and the [Hickory](https://github.com/davidsantiago/hickory) HTML to Hiccup parser. 26 | 27 | —[@ecmendenhall](https://twitter.com/ecmendenhall) 28 | -------------------------------------------------------------------------------- /src/sicpclojure/colorscheme.cljs: -------------------------------------------------------------------------------- 1 | (ns sicpclojure.colorscheme 2 | (:require [jayq.core :as jq])) 3 | 4 | (defn reverse-scheme [scheme] 5 | (if (= "Dark") "Light" "Dark")) 6 | 7 | (defn set-scheme [color] 8 | (let [a (jq/$ :#currentcolor)] 9 | (if (= "Dark" color) 10 | (do 11 | (jq/text (jq/$ a) "Light") 12 | (jq/attr (jq/$ :html) "class" "dark") 13 | (.setItem js/localStorage "colorscheme" "Dark")) 14 | (do 15 | (jq/text (jq/$ a) "Dark") 16 | (jq/attr (jq/$ :html) "class" "") 17 | (.setItem js/localStorage "colorscheme" "Light"))))) 18 | 19 | (defn change-scheme [] 20 | (let [a (jq/$ :#currentcolor)] 21 | (if (= "Dark" (jq/text a)) 22 | (set-scheme "Dark") 23 | (set-scheme "Light")))) 24 | 25 | (defn create-scheme-anchor [] 26 | (let [color-p (jq/$ :#colorscheme) 27 | color-a (.createElement js/document "a")] 28 | (-> 29 | (jq/$ color-a) 30 | (jq/attr "id" "currentcolor") 31 | (jq/bind "click" change-scheme)) 32 | (jq/append color-p color-a))) 33 | 34 | (defn get-colors [] 35 | (let [color-pref (.getItem js/localStorage "colorscheme")] 36 | (create-scheme-anchor) 37 | (set-scheme color-pref))) 38 | 39 | (jq/document-ready (fn [] 40 | (get-colors))) 41 | -------------------------------------------------------------------------------- /src/sicpclojure/config.clj: -------------------------------------------------------------------------------- 1 | (ns sicpclojure.config) 2 | 3 | (def build {:path-to-code "resources/code/" ; Path to code excerpts. 4 | :path-to-text "resources/text/" ; Path to chapter text. 5 | :deploy-directory "deploy/" 6 | :ignore #{".scss"} ; Ignore these filetypes when deploying. 7 | :complete [9 10 11 12 13 14 15] ; Vector of completed pages. 8 | :repo-url "https://github.com/ecmendenhall/sicpclojure/"}) 9 | 10 | (def templates {:js {:local ["modernizr.js" 11 | "require.js" 12 | "colorscheme.js" 13 | "analytics.js"] 14 | :external ["//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"]} 15 | :css ["style.css"] 16 | :fonts [["Lora" 17 | :size [400 700] 18 | :style ["italic"]] 19 | ["Ubuntu Mono"]] 20 | :font-url "http://fonts.googleapis.com/css?family=" 21 | :static-dir "static/"}) 22 | -------------------------------------------------------------------------------- /src/sicpclojure/link-regex.clj: -------------------------------------------------------------------------------- 1 | (ns links 2 | (:require [clojure.string :as string])) 3 | 4 | ;; A one-off script that cleans up links in the source text markdown files. 5 | (defn fix-links! [page] 6 | (defn replace-links [match] 7 | (if (= "4.html" (nth match 2)) 8 | (str "(contents.html#" 9 | (nth match 3) 10 | ")") 11 | (str "(" (nth match 1) ")"))) 12 | 13 | ;; Matches the crazy links in the original SICP pages: 14 | ;; "book-Z-H-(n).html#sec_(n)" 15 | (let [link-regex #"\(book-Z-H-(([0-9]\.html)#(sec_[0-9]+(:?\.[0-9])*))\)" 16 | page-text (slurp page)] 17 | (spit page (string/replace page-text 18 | link-regex 19 | replace-links)))) 20 | 21 | (map fix-links! (map (fn [n] (str "resources/text/" n ".md")) (range 9 39))) 22 | -------------------------------------------------------------------------------- /src/sicpclojure/templates/404.clj: -------------------------------------------------------------------------------- 1 | (ns sicpclojure.templates.404 2 | (:require [hiccup.core :refer [html]]) 3 | (:require [hiccup.page :refer [html5]]) 4 | (:use [sicpclojure.templates.base :exclude [render]])) 5 | 6 | (def content 7 | (html 8 | [:h1 "Page not found"] 9 | [:p "We can't find the page you requested. If you clicked a link to a later 10 | chapter, it might not be ready yet."])) 11 | 12 | (defn render [] 13 | (html5 {:lang "en"} 14 | (let [title (head :title) 15 | js (head :js) 16 | css (head :css) 17 | fonts (head :fonts)] 18 | [:head 19 | [:meta {:charset "utf-8"}] 20 | title 21 | js 22 | css 23 | fonts]) 24 | [:body 25 | [:div.sidebar 26 | [:nav 27 | [:p [:a {:href "pages/contents.html"} "Contents"]] 28 | [:p#colorscheme ]] 29 | [:footer footer]] 30 | [:div.content content]])) 31 | -------------------------------------------------------------------------------- /src/sicpclojure/templates/about.clj: -------------------------------------------------------------------------------- 1 | (ns sicpclojure.templates.about 2 | (:require [sicpclojure.config :as config]) 3 | (:require [hiccup.core :refer [html]]) 4 | (:require [hiccup.page :refer [html5]]) 5 | (:use [sicpclojure.templates.base :exclude [render]])) 6 | 7 | (defn render [content] 8 | (html5 {:lang "en"} 9 | (let [title (head :title) 10 | js (head :js) 11 | css (head :css) 12 | fonts (head :fonts)] 13 | [:head 14 | [:meta {:charset "utf-8"}] 15 | title 16 | js 17 | css 18 | fonts]) 19 | [:body 20 | [:div.container 21 | [:div.row 22 | [:div {:class "sidebar span3"} 23 | [:nav 24 | [:p [:a {:href "pages/contents.html"} "Contents"]] 25 | [:p#colorscheme ]] 26 | [:footer footer]] 27 | [:div {:class "content span9 offset3"} 28 | [:div.chaptertext content]]]]])) 29 | -------------------------------------------------------------------------------- /src/sicpclojure/templates/base.clj: -------------------------------------------------------------------------------- 1 | (ns sicpclojure.templates.base 2 | (:require [sicpclojure.config :as config]) 3 | (:require [hiccup.core :refer [html]]) 4 | (:require [hiccup.page :refer [html5, include-js, include-css]]) 5 | (:require [clojure.string :as string]) 6 | (:require [clojure.zip :as zip])) 7 | 8 | (defn include-font 9 | "Takes a font vector from config/templates, with optional size and style 10 | subvectors. Formats it as a Google Web Fonts URL. Returns a Hiccup :link element." 11 | [font] 12 | (let [font-name (string/replace (first font) " " "+") 13 | font-sizes (second (rest font)) 14 | font-styles (last (rest font))] 15 | (include-css (str (config/templates :font-url) 16 | font-name 17 | (when font-sizes 18 | (str ":" 19 | (string/join "," font-sizes))) 20 | (when font-styles 21 | (string/join (for [size font-sizes 22 | style font-styles] 23 | (str "," size style)))))))) 24 | 25 | (defn include-local 26 | "Takes a vector of filenames from config/templates, the static directory path 27 | relative to the rendered template, and a Hiccup include function. Generates the 28 | correct path to the file and returns a Hiccup element based on the filetype." 29 | [file static-dir include-fn] 30 | (let [filetype (second (re-find #"\.([0-9a-z]+)$" file))] ; Matches filetype 31 | (include-fn (str static-dir 32 | filetype 33 | "/" 34 | file)))) 35 | 36 | (defn include-link 37 | "Takes the URL to a remote script or stylesheet and a Hiccup include function. 38 | Returns a :link or :script element." 39 | [file include-fn] 40 | (include-fn file)) 41 | 42 | (defn make-head 43 | "Generates a page header including the js, css, and fonts in config/templates." 44 | [static-dir] 45 | {:title [:title "SICP in Clojure"] 46 | :js (list (map (fn [url] (include-link url include-js)) 47 | ((config/templates :js) :external)) 48 | [:script {:data-main (str static-dir "js/main") 49 | :type "text/javascript" 50 | :src (str static-dir "js/require.js")}] 51 | (map (fn [file] (include-local file static-dir include-js)) 52 | ((config/templates :js) :local))) 53 | :css (map (fn [file] (include-local file static-dir include-css)) 54 | (config/templates :css)) 55 | :fonts (map (fn [file] (include-link file include-font)) 56 | (config/templates :fonts))}) 57 | 58 | (def head (make-head (config/templates :static-dir))) 59 | 60 | (defn make-footer 61 | "Generates a page footer. Takes a path to the root directory, relative to the 62 | rendered template." 63 | [root-dir] 64 | [:div 65 | [:p 66 | [:a {:href (str root-dir "about.html")} "About"]] 67 | [:p 68 | [:a {:href "http://creativecommons.org/licenses/by-sa/3.0/"} 69 | [:img {:src (str root-dir "static/img/licensebadge.png") 70 | :alt "CC-BY-NC 3.0"}]]]]) 71 | 72 | (def footer (make-footer "" )) 73 | 74 | (defn render 75 | "Returns the rendered Hiccup template as HTML5." 76 | [contents content] 77 | (html5 {:lang "en"} 78 | (let [title (head :title) 79 | js (head :js) 80 | css (head :css) 81 | fonts (head :fonts)] 82 | [:head 83 | [:meta {:charset "utf-8"}] 84 | [:meta {:name "viewport" :content "width=device-width, initial-scale=1.0"}] 85 | title 86 | js 87 | css 88 | fonts]) 89 | [:body 90 | [:div.container 91 | [:div.row 92 | [:div {:class "sidebar span3"} 93 | [:nav 94 | [:p ""] 95 | contents 96 | [:p#colorscheme]] 97 | [:footer footer]] 98 | [:div {:class "content span9 offset3"} content]]]])) 99 | -------------------------------------------------------------------------------- /src/sicpclojure/templates/contents.clj: -------------------------------------------------------------------------------- 1 | (ns sicpclojure.templates.contents 2 | (:require [sicpclojure.config :as config]) 3 | (:require [hiccup.core :refer [html]]) 4 | (:require [hiccup.page :refer [html5]]) 5 | (:use [sicpclojure.templates.base :exclude [render head footer]])) 6 | 7 | (def head (make-head "../static/")) 8 | 9 | (def footer (make-footer "../")) 10 | 11 | (defn render [contents] 12 | (html5 {:lang "en"} 13 | (let [title (head :title) 14 | js (head :js) 15 | css (head :css) 16 | fonts (head :fonts)] 17 | [:head 18 | [:meta {:charset "utf-8"}] 19 | [:meta {:name "viewport" :content "width=device-width, initial-scale=1.0"}] 20 | title 21 | js 22 | css 23 | fonts]) 24 | [:body 25 | [:div.container 26 | [:div.row 27 | [:div {:class "sidebar span3"} 28 | [:nav 29 | [:p "| " [:a {:href (str (first (config/build :complete)) ".html")} "Next"]] 30 | [:p#colorscheme]] 31 | [:footer footer]] 32 | [:div {:class "content span9 offset3" :id "toc"} contents]]]])) 33 | -------------------------------------------------------------------------------- /src/sicpclojure/templates/cover.clj: -------------------------------------------------------------------------------- 1 | (ns sicpclojure.templates.cover 2 | (:require [hiccup.core :refer [html]]) 3 | (:require [hiccup.page :refer [html5]]) 4 | (:use [sicpclojure.templates.base :exclude [render]])) 5 | 6 | (def content 7 | (html 8 | [:div.title 9 | [:h1 "SICP In Clojure"]] 10 | [:div.quote 11 | [:h1 "“I personally don't think SICP will help you much with Clojure. YMMV.”"] 12 | [:p "—Rich Hickey, author, Clojure programming language"]])) 13 | 14 | (defn render [] 15 | (html5 {:lang "en"} 16 | (let [title (head :title) 17 | js (head :js) 18 | css (head :css) 19 | fonts (head :fonts)] 20 | [:head 21 | [:meta {:charset "utf-8"}] 22 | [:meta {:name "viewport" :content "width=device-width, initial-scale=1.0"}] 23 | title 24 | js 25 | css 26 | fonts]) 27 | [:body 28 | [:div.container 29 | [:div.row 30 | [:div {:class "sidebar span3"} 31 | [:nav 32 | [:p [:a {:href "pages/contents.html"} "Contents"]] 33 | [:p#colorscheme ]] 34 | [:footer footer]] 35 | [:div {:class "content span9 offset3"} content]]]])) 36 | -------------------------------------------------------------------------------- /src/sicpclojure/templates/page.clj: -------------------------------------------------------------------------------- 1 | (ns sicpclojure.templates.page 2 | (:require [sicpclojure.config :as config]) 3 | (:require [hiccup.core :refer [html]]) 4 | (:require [hiccup.page :refer [html5]]) 5 | (:use [sicpclojure.templates.base :exclude [render head footer]])) 6 | 7 | (def head (make-head "../static/")) 8 | 9 | (def footer (make-footer "../")) 10 | 11 | (defn page-src [page] 12 | (str (config/build :repo-url) "blob/master/resources/text/" page ".md")) 13 | 14 | (defn make-nav 15 | "Takes a Hiccup vector as page contents and a page number. Generates a Hiccup :nav 16 | element based on the number of completed pages listed in config/build." 17 | [contents page] 18 | (let [prev-page (if (< (dec page) (first (config/build :complete))) 19 | "../index.html" 20 | (str (dec page) ".html")) 21 | next-page (if (> (inc page) (last (config/build :complete))) 22 | false 23 | (str (inc page) ".html"))] 24 | [:nav 25 | [:p 26 | [:a {:href prev-page} "Prev"] 27 | " | " 28 | (when next-page [:a {:href next-page} "Next"])] 29 | [:p [:a {:href "contents.html"} "Contents"]] 30 | contents 31 | [:p#colorscheme]])) 32 | 33 | (defn render [contents content page] 34 | (html5 {:lang "en"} 35 | (let [title (head :title) 36 | js (head :js) 37 | css (head :css) 38 | fonts (head :fonts)] 39 | [:head 40 | [:meta {:charset "utf-8"}] 41 | [:meta {:name "viewport" :content "width=device-width, initial-scale=1.0"}] 42 | title 43 | js 44 | css 45 | fonts]) 46 | [:body 47 | [:div.container 48 | [:div.row 49 | [:div {:class "sidebar span3"} 50 | (make-nav contents page) 51 | [:footer footer]] 52 | [:div {:class "content span9 offset3"} 53 | [:a {:href (page-src page) :class "text-source-link"} "Page text"] 54 | [:div.chaptertext content]]]]])) 55 | -------------------------------------------------------------------------------- /test/sicpclojure/core_test.clj: -------------------------------------------------------------------------------- 1 | (ns sicpclojure.core-test 2 | (:use clojure.test 3 | sicpclojure.core)) 4 | 5 | (deftest a-test 6 | (testing "FIXME, I fail." 7 | (is (= 0 1)))) --------------------------------------------------------------------------------