Skip to content

Commit

Permalink
The Ackermann function is wild
Browse files Browse the repository at this point in the history
  • Loading branch information
dwinston committed Jun 21, 2014
1 parent 0e8172f commit 1017df2
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 6 deletions.
44 changes: 44 additions & 0 deletions ex1.10.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
(define (A x y)
(cond ((= y 0) 0)
((= x 0) (* 2 y))
((= y 1) 2)
(else (A (- x 1)
(A x (- y 1))))))

(define (pow base exponent)
(cond ((= base 0) 0)
((= exponent 0) 1)
(else (* base (pow base (- exponent 1))))))


(pow 2 10)

;Value: 1024

(pow 2 16)

;Value: 65536

(A 1 10)

;Value: 1024

(A 2 4)

;Value: 65536

(A 3 3)

;Value: 65536

(define (f n) (A 0 n))

; 2*n

(define (g n) (A 1 n))

; 2^n

(define (h n) (A 2 n))

; 2^2^2^2...^2, with (- n 1) 2's in total
15 changes: 11 additions & 4 deletions ex1.3.scm
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
;; Exercise 1.3

(define (sum-of-squares x y) (+ (square x) (square y)))
(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))
(= (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))
2 changes: 0 additions & 2 deletions ex1.6.scm
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
(cond (predicate then-clause)
(else else-clause)))

(new-if (= 2 3) 0 5)

(new-if (= 1 1) 0 5)

(define (new-sqrt-iter guess x)
Expand Down
21 changes: 21 additions & 0 deletions ex1.9.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
(define (inc a) (+ a 1))
(define (dec a) (- a 1))

;; The below procedure generates a recursive process because the process cannot
;; be summarized at every step by a set of state variable values -- there is
;; additional "hidden" information maintained by the interpreter that indicates
;; "where the process is" in negotiating a chain of deferred operations.

(define (plus-recur a b)
(if (= a 0)
b
(inc (plus-recur (dec a) b))))

;; The below procedure generates an iterative process because the state at any
;; stage in the evolution of the process is captured completely by two state
;; variables, a and b.

(define (plus-iter a b)
(if (= a 0)
b
(plus-iter (dec a) (inc b))))

0 comments on commit 1017df2

Please sign in to comment.