Skip to content

Instantly share code, notes, and snippets.

@sherbondy
Created September 6, 2012 04:28
Show Gist options
  • Save sherbondy/3651239 to your computer and use it in GitHub Desktop.
Save sherbondy/3651239 to your computer and use it in GitHub Desktop.
First assignment for SICM, Excercises 8.1 and 8.2
(define (f x y)
(* (square x) (cube y)))
(define (g x y)
(up (f x y) 'y))
; just for giggles
(define (helix t)
(up (cos t) (sin t) t))
(print-expression ((D helix) 't))
;>> (up (* -1 (sin t)) (cos t) 1)
(define (h x y)
(f (f x y) y))
; typing partial is no fun
(define (pf f n)
(((partial n) f) 'x 'y))
;;; 8.1 a
(pf f 0)
#|
(* 2 x (expt y 3))
|#
(pf f 1)
#|
(* 3 (expt x 2) (expt y 2))
|#
;;; 8.1 b
(((partial 0) f) (f 'x 'y) 'y)
#|
(* 2 (expt x 2) (expt y 6))
|#
;; NOT the same as:
(pf h 0)
#|
(* 4 (expt x 3) (expt y 9))
|#
(((partial 1) f) (f 'x 'y) 'y)
#|
(* 3 (expt x 4) (expt y 8))
|#
;;; 8.1 c
(pf g 0)
#|
(up (* 2 x (expt y 3)) 0)
|#
(pf g 1)
#|
(up (* 3 (expt x 2) (expt y 2)) 0)
|#
::; 8.1 d
((D f) 'a 'b)
#|
(down (* 2 a (expt b 3)) (* 3 (expt a 2) (expt b 2)))
|#
((D g) 3 5)
#|
(down (up 750 0) (up 675 0))
|#
((D h) (* 3 (square 'a))
(* 5 (cube 'b)))
#|
(down (* 210937500 (expt a 6) (expt b 27)) (* 284765625 (expt a 8) (expt b 24)))
|#
;;; 8.2
; and now for the vector argument formulation
(define (f* v)
(let ((x (ref v 0))
(y (ref v 1)))
(* (square x) (cube y))))
; this one looks like a profanity
(f* (up 'x 'y))
; I really need to examine macros in scheme
; these let statements feel like code smell.
(define (g* v)
(let ((x (ref v 0))
(y (ref v 1)))
(up (f* v) y)))
(g* (up 'x 'y))
; ah, a breath of fresh air
(define h* (compose f* g*))
(h* (up 4 2))
((D h*) (up (* 3 (square 'a)) (* 5 (cube 'b))))
#|
(down (* 210937500 (expt a 6) (expt b 27)) (* 284765625 (expt a 8) (expt b 24)))
|#
; hooray, it's the same
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment