Permalink
Browse files

Week 1 : Exercises 1.1 - 1.5

  • Loading branch information...
pastafari committed Jul 30, 2012
1 parent 9a8fe7a commit 0ded16f54e46dc8b451dd39c72edc08af776477c
Showing with 148 additions and 0 deletions.
  1. +62 −0 exercises/1/1.1.scm
  2. +14 −0 exercises/1/1.2.scm
  3. +19 −0 exercises/1/1.3.scm
  4. +21 −0 exercises/1/1.4.scm
  5. +32 −0 exercises/1/1.5.scm
View
@@ -0,0 +1,62 @@
+;; This buffer is for notes you don't want to save, and for Lisp evaluation.
+;; If you want to create a file, visit that file with C-x C-f,
+;; then enter the text in that file's own buffer.
+
+MIT/GNU Scheme running under MacOSX
+
+Copyright (C) 2011 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 Tuesday November 8, 2011 at 10:45:46 PM
+ Release 9.1.1 || Microcode 15.3 || Runtime 15.7 || SF 4.41
+ LIAR/x86-64 4.118 || Edwin 3.116
+
+10
+;Value: 10
+
+(+ 5 3 4)
+;Value: 12
+
+(- 9 1)
+;Value: 8
+
+(/ 6 2)
+;Value: 3
+
+(+ (* 2 4)
+ (- 4 6))
+;Value: 6
+
+(define a 3)
+;Value: a
+
+(define b (+ a 1))
+;Value: b
+
+(+ a b (* a b))
+;Value: 19
+
+(= a b)
+;Value: #f
+
+(if (and (> b a) (< b (* a b)))
+ b
+ a)
+;Value: 4
+
+(cond ((= a 4) 6)
+ ((= b 4) (+ 6 7 a))
+ (else 25))
+;Value: 16
+
+(+ 2 (if (> b a) b a))
+;Value: 6
+
+(* (cond ((> a b) a)
+ ((< a b) b)
+ (else -1))
+ (+ a 1))
+;Value: 16
+
+
View
@@ -0,0 +1,14 @@
+MIT/GNU Scheme running under MacOSX
+
+Copyright (C) 2011 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 Tuesday November 8, 2011 at 10:45:46 PM
+ Release 9.1.1 || Microcode 15.3 || Runtime 15.7 || SF 4.41
+ LIAR/x86-64 4.118 || Edwin 3.116
+
+(/ (+ 5 4 (- 2 (- 3 (+ 6 (/ 4 5)))))
+ (* 3 (- 6 2) (- 2 7)))
+;Value: -37/150
+
View
@@ -0,0 +1,19 @@
+(define (sum-of-squares-of-two-largest-numbers x y z)
+ (cond ((and (< x y) (< x z)) (+ (* y y) (* z z)))
+ ((and (< y x) (< y z)) (+ (* x x) (* z z)))
+ (else (+ (* x x) (* y y)))))
+
+;Value: sum-of-squares-of-two-largest-numbers
+
+1 (sum-of-squares-of-two-largest-numbers 1 2 3)
+
+;Value: 13
+
+1 (sum-of-squares-of-two-largest-numbers -1 -2 -3)
+
+;Value: 5
+
+(sum-of-squares-of-two-largest-numbers 20 2 1)
+
+;Value: 404
+
View
@@ -0,0 +1,21 @@
+; The body of the abstraction a-plus-abs-b has as its operator a special form "if"
+; The interpreter first evals the if which yields either + or - depending on whether
+; the formal parameter b is > 0 or not.
+; The primitive operator yielded by the if is then applied to the arguments a b
+; Thus if b is indeed positive, the abstraction will yield a + b, else it will yield a - b
+; effectively computing the sum of a and the absolute value of b
+
+
+(define (a-plus-abs-b a b)
+ ((if (> b 0) + -) a b))
+
+;Value: a-plus-abs-b
+
+(a-plus-abs-b 1 2)
+
+;Value: 3
+
+(a-plus-abs-b 1 -2)
+
+;Value: 3
+
View
@@ -0,0 +1,32 @@
+; First an observation:
+; The abstraction 'p' yields itself parenthesized, i.e. p -> (p)
+; If eval'ed, this will lead to a infinite recursion yielding (((((((.........(p).........)))))))
+;
+; Moving on to the abstraction 'test', it yields 0 if x is 0, or y otherwise
+; Consider the expression: (test 0 (p))
+; Consider the two cases:
+; Applicative order evaluation:
+; The operator evaluates to the body of the abstraction 'test'
+; The first operand evaluates to 0
+; Whereas the second operand (p) cannot be evaluated as it yields an infinite recursion.
+
+; Normal order evaluation:
+; The operator evaluates to the body of the abstraction 'test'
+; The operands are not eval'ed until necessary
+; The body of the abstraction 'test' is a special form ("if")
+; As per the rules for the special form, the predicate (= x 0) is eval'ed first
+; Since the predicate is true, the consequent is eval'ed and yielded as the value of the expression, i.e. 0
+; The alternative to the if is never eval'ed in this case, so all is well.
+
+(define (p) (p))
+
+;Value: p
+
+(define (test x y)
+ (if (= x 0)
+ 0
+ y))
+
+;Value: test
+
+(test 0 (p))

0 comments on commit 0ded16f

Please sign in to comment.