Skip to content

Instantly share code, notes, and snippets.

;; SICP Exercise 2.73
;; Section 2.3.2 described a program that performs symbolic differentiation:
;;
;; (define (deriv exp var)
;; (cond ((number? exp) 0)
;; ((variable? exp) (if (same-variable? exp var) 1 0))
;; ((sum? exp)
;; (make-sum (deriv (addend exp) var)
;; (deriv (augend exp) var)))
;; SICP Excercise 2.56
;; Show how to extend the basic differentiator to handle more kinds
;; of expressions. For instance, implement the differentiation rule
;; d(u^n) /du\
;; ------ = nu^(n-1) | -- |
;; dx \dx/
;; by adding a new clause to the deriv program and defining appropriate pro-
;; cedures exponentiation?, base, exponent, and make-exponentiation. (You
;; may use the symbol ** to denote exponentiation.) Build in the rules that any-
;; SICP
;; Exercise 2.54
;;
;; Two lists are said to be equal? if they contain equal elements
;;arranged in the same order. For example,
(equal? '(this is a list) '(this is a list))
;; is true, but
;; SICP
;; Exercise 2.41.
;;
;; Write a procedure to find all ordered triples of distinct positive integers i, j , and k less than
;; or equal to a given integer n that sum to a given integer s.
;;
(define (unique-triplets n)
(flatmap
(lambda (i)
;; SICP
;; Exercise 2.40.
;;
;; Define a procedure unique-pairs that, given an integer n, generates the sequence of pairs
;; (i, j ) with 1 ≤ j ≤ i ≤ n. Use unique-pairs to simplify the definition of prime-sum-pairs given above.
(define (unique-pairs n)
(flatmap
(lambda (i)
(map (lambda (j) (list i j))
;; SICP
;; Exercise 2.33.
;;
;; Fill in the missing expressions to complete the following definitions of some basic list-
;; manipulation operations as accumulations:
;;
;; (define (map p sequence)
;; (accumulate (lambda (x y) <??>) nil sequence))
;; (define (append seq1 seq2)
;; (accumulate cons <??> <??>))
;; SICP
;; Exercise 2.19.
;;
;; Consider the change-counting program of section 1.2.2. It would be nice to be able to easily
;; change the currency used by the program, so that we could compute the number of ways to change a British
;; pound, for example. As the program is written, the knowledge of the currency is distributed partly into the
;; procedure first-denomination and partly into the procedure count-change (which knows that there are
;; five kinds of U.S. coins). It would be nicer to be able to supply a list of coins to be used for making change.
;;
;; We want to rewrite the procedure cc so that its second argument is a list of the values of the coins to use
@jimbreen
jimbreen / gist:222541
Created October 30, 2009 17:15
SICP Excercise 2.17
;; SICP
;; Exercise 2.17.
;;
;; Define a procedure last-pair that returns the list that contains only the last element of a
;; given (nonempty) list:
;; (last-pair (list 23 72 149 34))
;; (34)
;;
(define (last-pair items)
@jimbreen
jimbreen / gist:211906
Created October 16, 2009 17:37
SICP Excercise 1.42
;; SICP
;; Exercise 1.42
;; Let f and g be two one-argument functions. The composition f
;; after g is defined to be the function x |-> f (g (x )). Define a procedure compose
;; that implements composition. For example, if inc is a procedure that adds 1
;; to its argument,
;; ((compose square inc) 6)
;; 49
(define (inc n)
;; SICP 1.3
;;
;; Exercise 1.3. Define a procedure that takes three numbers as arguments and
;; returns the sum of the squares of the two larger numbers.
;;
;; This solution is based on discussion (primarily by Jim Weirich) during the
;; Alpha study group's weekly meeting
(define (square x) (* x x))
(define (sum-of-squares x y) (+ (square x) (square y)))