Name: sicp 2008-08-31 11:25
Yes I am reading sicp.
(define ex1.2 (/ (+ 5 4 (- 2 (- 3 (+ 6 (/ 4 5)))))
(* 3 (- 6 2)(- 2 7)))
)
;sumofsquaresoflargest: # # # -> #
;takes three #'s & returns sum of the squares of the two largest
(define (sumofsquaresoflargest a b c)
(+ (* (largest1 a b c)(largest1 a b c))
(* (largest2 a b c)(largest2 a b c))))
;largest1: # # # -> #
;takes three numbers and returns the largest
(define (largest1 a b c)
(cond [(and (> a b)(> a c)) a]
[(and (> b a)(> b c)) b]
[(and (> c a)(> c b)) c]
[else a]))
;largest2: # # # -> #
;takes three numbers and returns the 2nd largest
(define (largest2 a b c)
(cond [(and (> a b)(< a c)) a]
[(and (> b a)(< b c)) b]
[(and (> c a)(< c b)) c]
[else a]))
;In normal order it would just loop infinitely trying to substitute (p) for (p) looking for a primitive operation to perform. Applicative would evaluate it as 0 by just evaluating (= x 0) as 0 and return 0.
(define ex1.2 (/ (+ 5 4 (- 2 (- 3 (+ 6 (/ 4 5)))))
(* 3 (- 6 2)(- 2 7)))
)
;sumofsquaresoflargest: # # # -> #
;takes three #'s & returns sum of the squares of the two largest
(define (sumofsquaresoflargest a b c)
(+ (* (largest1 a b c)(largest1 a b c))
(* (largest2 a b c)(largest2 a b c))))
;largest1: # # # -> #
;takes three numbers and returns the largest
(define (largest1 a b c)
(cond [(and (> a b)(> a c)) a]
[(and (> b a)(> b c)) b]
[(and (> c a)(> c b)) c]
[else a]))
;largest2: # # # -> #
;takes three numbers and returns the 2nd largest
(define (largest2 a b c)
(cond [(and (> a b)(< a c)) a]
[(and (> b a)(< b c)) b]
[(and (> c a)(< c b)) c]
[else a]))
;In normal order it would just loop infinitely trying to substitute (p) for (p) looking for a primitive operation to perform. Applicative would evaluate it as 0 by just evaluating (= x 0) as 0 and return 0.