Skip to content

Commit

Permalink
Submit first group of exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
dwinston committed Jun 10, 2014
1 parent b582361 commit 4528f85
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
sicp-study-group
================

A lay-led study of [SICP](https://mitpress.mit.edu/sicp/) in Berkeley, CA.

* http://www.meetup.com/codeselfstudy/events/187351632/
* http://www.meetup.com/codeselfstudy/events/187758402/
* ...

This repo houses my solutions to exercises.
79 changes: 79 additions & 0 deletions ex1.1.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
;;; Installed Scheme via `brew install mit-scheme` on OS X.
;;; Running Scheme interpreter in Emacs via `M-x run-scheme`.
;;; Donny Winston, 2014-06-10

;; MIT/GNU Scheme running under OS X
;; Type `^C' (control-C) followed by `H' to obtain information about interrupts.

;; Copyright (C) 2014 Massachusetts Institute of Technology
;; This is free software; see the source for copying conditions. There is NO
;; warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
;; PURPOSE.

;; Image saved on Friday May 23, 2014 at 5:50:11 PM
;; Release 9.2 || Microcode 15.3 || Runtime 15.7 || SF 4.41 || LIAR/C 4.118
;; Edwin 3.116


;; Exercise 1.1

1 ]=> 10

;Value: 10

1 ]=> (+ 5 3 4)

;Value: 12

1 ]=> (- 9 1)

;Value: 8

1 ]=> (/ 6 2)

;Value: 3

1 ]=> (+ (* 2 4) (- 4 6))

;Value: 6

1 ]=> (define a 3)

;Value: a

1 ]=> (define b (+ a 1))

;Value: b

1 ]=> (+ a b (* a b))

;Value: 19

1 ]=> (= a b)

;Value: #f

1 ]=> (if (and (> b a) (< b (* a b)))
b
a)

;Value: 4

1 ]=> (cond ((= a 4) 6)
((= b 4) (+ 6 7 a))
(else 25))

;Value: 16

1 ]=> (+ 2 (if (> b a) b a))

;Value: 6

1 ]=> (* (cond ((> a b) a)
((< a b) b)
(else -1))
(+ a 1))

;Value: 16

1 ]=>
6 changes: 6 additions & 0 deletions ex1.2.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
;; Exercise 1.2

(/ (+ 4 5 (- 2 (- 3 (+ 6 (/ 4 5)))))
(* 3 (- 6 2) (- 2 7)))

;Value: -37/150
11 changes: 11 additions & 0 deletions ex1.3.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
;; Exercise 1.3

(define (sum-of-squares x y) (+ (square x) (square y)))
(define (sum-squares-of-two-largest a b c)
(cond ((and (> a c) (> b c)) (sum-of-squares a b))
((and (> a b) (> c b)) (sum-of-squares a c))
(else (sum-of-squares b c))))

(= (sum-squares-of-two-largest 3 4 5) (sum-of-squares 4 5))
(= (sum-squares-of-two-largest 5 4 3) (sum-of-squares 4 5))
(= (sum-squares-of-two-largest 3 5 4) (sum-of-squares 4 5))
9 changes: 9 additions & 0 deletions ex1.4.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
;; Exercise 1.4

(define (a-plus-abs-b a b)
((if (> b 0) + -) a b))

;; If b > 0, return `(+ a b)`.
;; Otherwise, return `(- a b)`.
;; Thus, return `(+ a (abs b))`,
;; where `(abs b)` is the absolute value of b.
21 changes: 21 additions & 0 deletions ex1.5.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
;; Exercise 1.5

;; normal-order evaluation: "fully expand and then reduce"
;; applicative-order evaluation: "evaluate the arguments and then apply"
;; Lisp uses applicative-order evaluation, but normal-order can be useful
;; e.g. for stream processing (to be introduced in Ch 3).

(define (p) (p))

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

(test 0 (p))

;; With applicative-order evaluation, the evaluation will never return a value
;; because `(p)` will never return. With normal-order evaluation, because the
;; arguments to `test` are not evaluated prior to evaluating the body of `test`,
;; and because `y` is never evaluated (because `(= x 0)` evaluates to true),
;; `(test 0 (p))` will return 0.

0 comments on commit 4528f85

Please sign in to comment.