This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; 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))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; 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- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; 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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; 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 <??> <??>)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; 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))) |