Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Have you read your SICP today?

Name: Anonymous 2008-12-04 9:20

Well, have you?

Name: Anonymous 2008-12-04 9:22

I have read my XKCD today.

Name: Anonymous 2008-12-04 9:40

I've read my Hardy and Wright today. Does that count?

Name: Anonymous 2008-12-04 11:29

>>1
I watched one of the lectures instead

Name: Anonymous 2008-12-05 12:18

Name: Anonymous 2010-06-16 4:05

I read my SICP today!

#lang scheme
(define (cuberoot number)
  (cuberooter number 1.0))

(define (cuberooter number guess)
  (if (good-enough guess number)
      guess
      (cuberooter number (improve-guess number guess))))

(define (improve-guess x y)
  (/ (+ (/ x (square y)) (* y 2)) 3))

(define (square x)
  (* x x))

(define (cube x)
  (* x (square x)))

(define (good-enough guess x)
  (< (abs (- (cube guess) x)) 0.000000000001))

(define (abs x)
  (cond ((> x 0) x)
        ((= x 0) x)
        ((< x 0) (- x))))

(cuberoot 1000000)
(cube (cuberoot 1000000))

Name: Anonymous 2010-06-16 4:30

SIGS

Name: Anonymous 2010-06-16 5:17

Structural Interpretation of Gay Sex>>7

Name: Anonymous 2010-06-16 6:21

Name: Anonymous 2010-06-16 23:25

>>8
Sodomistic Injection of

Name: Anonymous 2010-06-18 4:27

I read my SICP today!

Exercise 1.9

[bold]Recursive[/bold]

(define (+ a b)
  (if (= a 0)
      b
      (inc (+ (dec a) b))))
(define (inc a)
  (+ a 1))
(define (dec a)
  (- a 1))


a=4 b=5
(inc (func+ (- 4 1) 5))
(inc (func+ 3 5))
(inc (inc (func+ (- 3 1) 5)))
(inc (inc (func+ 2 5)))
(inc (inc (inc (func+ (- 2 1) 5))))
(inc (inc (inc (func+ 1 5))))
(inc (inc (inc (inc (func+ (- 1 1) 5)))))
(inc (inc (inc (inc (func+ 0 5)))))
(inc (inc (inc (inc ((if (= a 0) then b))))))
(inc (inc (inc (inc (b)))))
(inc (inc (inc (inc (5)))))
(inc (inc (inc (6))))
(inc (inc (7)))
(inc (8))
(9)


[bold]iterative[/bold]

(define (+ a b)
  (if (= a 0)
      b
      (+ (dec a) (inc b))))
(define (inc a)
  (+ a 1))
(define (dec a)
  (- a 1))
(+ 4 5)

a=4 b=5
(+ (- 4 1) (+ 5 1))
(+ 3  6)
(+ (- 3 1) (+ 6 1))
(+ 2 7)
(+ (- 2 1) (+ 7 1))
(+ 1 8)
(+ (- 1 1) (+ 8 1))
(+ 0 9)
(if (= a 0) b)
(b)
9

(don't know why the iterative program won't evaluate - maybe my interpreter is bugged)

Name: Anonymous 2010-06-18 6:25

Bitch, that's not how you do it.
This is what happens when you do (+ 1 1) using your "iterative" definition (which is actually mutually recursive with inc); assuming you haven't fucked up (- a b) yet…

(+ 1 1)
(+ (dec 1) (inc 1))
(+ 0 (+ 1 1))
(+ 0 (+ (dec 1) (inc 1)))
(+ 0 (+ 0 (+ 1 1)))
(+ 0 (+ 0 (+ (dec 1) (inc 1))))
(+ 0 (+ 0 (+ 0 (+ 1 1))))
(+ 0 (+ 0 (+ 0 (+ (dec 1) (inc 1)))))
(+ 0 (+ 0 (+ 0 (+ 0 (+ 1 1)))))
.
.
.


And this is what happens when you do (+ 1 1) with your recursive definition.


(+ 1 1)
(inc (+ (dec 1) 1))
(inc (+ 0 1))
(inc 1)
(+ 1 1)
(inc (+ (dec 1) 1))
(inc (+ 0 1))
(inc 1)
(+ 1 1)
(inc (+ (dec 1) 1))
(inc (+ 0 1))
(inc 1)
(+ 1 1)
.
.
.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List