Category: Scheme
-
Sicp Exercise 2.22
Exercise 2.22. Louis Reasoner tries to rewrite the first square-list procedure of exercise 2.21 so that it evolves an iterative process: (define (square-list items) (define (iter things answer) (if (null? things) answer (iter (cdr things) (cons (square (car things)) answer)))) (iter items nil)) Unfortunately, defining square-list this way produces the answer list in the reverse order of the one desired. Why? Louis then tries to fix his bug by interchanging the…
-
Sicp Exercise 2.21
Exercise 2.21. The procedure square-list takes a list of numbers as argument and returns a list of the squares of those numbers. (square-list (list 1 2 3 4)) (1 4 9 16) Here are two different definitions of square-list. Complete both of them by filling in the missing expressions: (define (square-list items) (if (null? items) nil (cons <??> <??>))) (define (square-list items) (map <??> <??>)) This file contains hidden or bidirectional Unicode text that…
-
Sicp Exercise 2.20
Exercise 2.20. The procedures +, *, and list take arbitrary numbers of arguments. One way to define such procedures is to use define with dotted-tail notation. In a procedure definition, a parameter list that has a dot before the last parameter name indicates that, when the procedure is called, the initial parameters (if any) will have…
-
Sicp Exercise 2.19
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…
-
Sicp Exercise 2.18
Exercise 2.18. Define a procedure reverse that takes a list as argument and returns a list of the same elements in reverse order: (reverse (list 1 4 9 16 25)) (25 16 9 4 1) 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…
-
Sicp Exercise 2.17
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) 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.…
-
Sicp Exercise 2.06
Exercise 2.6. In case representing pairs as procedures wasn’t mind-boggling enough, consider that, in a language that can manipulate procedures, we can get by without numbers (at least insofar as nonnegative integers are concerned) by implementing 0 and the operation of adding 1 as (define zero (lambda (f) (lambda (x) x))) (define (add-1 n) (lambda (f) (lambda (x) (f ((n f) x))))) This representation is known as Church numerals, after its…
-
Sicp Exercise 2.05
Exercise 2.5. Show that we can represent pairs of nonnegative integers using only numbers and arithmetic operations if we represent the pair a and b as the integer that is the product 2a 3b. Give the corresponding definitions of the procedures cons, car, and cdr. This file contains hidden or bidirectional Unicode text that may…
-
Sicp Exercise 2.04
Exercise 2.4. Here is an alternative procedural representation of pairs. For this representation, verify that (car (cons x y)) yields x for any objects x and y. (define (cons x y) (lambda (m) (m x y))) (define (car z) (z (lambda (p q) p))) What is the corresponding definition of cdr? (Hint: To verify that this works, make use of the substitution model of section 1.1.5.) This file contains…
-
Sicp Exercise 2.03
Exercise 2.3. Implement a representation for rectangles in a plane. (Hint: You may want to make use of exercise 2.2.) In terms of your constructors and selectors, create procedures that compute the perimeter and the area of a given rectangle. Now implement a different representation for rectangles. Can you design your system with suitable abstraction barriers, so…