Category: Scheme
-
Sicp Exercise 2.32
Exercise 2.32. We can represent a set as a list of distinct elements, and we can represent the set of all subsets of the set as a list of lists. For example, if the set is (1 2 3), then the set of all subsets is (() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3)). Complete the following definition…
-
Sicp Exercise 2.31
Exercise 2.31. Abstract your answer to exercise 2.30 to produce a procedure tree-map with the property that square-tree could be defined as (define (square-tree tree) (tree-map square tree)) 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…
-
Sicp Exercise 2.30
Exercise 2.30. Define a procedure square-tree analogous to the square-list procedure of exercise 2.21. That is, square-list should behave as follows: (square-tree (list 1 (list 2 (list 3 4) 5) (list 6 7))) (1 (4 (9 16) 25) (36 49)) Define square-tree both directly (i.e., without using any higher-order procedures) and also by using map and recursion. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled…
-
Sicp Exercise 2.28
Exercise 2.28. Write a procedure fringe that takes as argument a tree (represented as a list) and returns a list whose elements are all the leaves of the tree arranged in left-to-right order. For example, (define x (list (list 1 2) (list 3 4))) (fringe x) (1 2 3 4) (fringe (list x x)) (1 2 3 4 1 2 3 4) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently…
-
Sicp Exercise 2.29
Exercise 2.29. A binary mobile consists of two branches, a left branch and a right branch. Each branch is a rod of a certain length, from which hangs either a weight or another binary mobile. We can represent a binary mobile using compound data by constructing it from two branches (for example, using list): (define (make-mobile left right) (list left right))…
-
Sicp Exercise 2.27
Exercise 2.27. Modify your reverse procedure of exercise 2.18 to produce a deep-reverse procedure that takes a list as argument and returns as its value the list with its elements reversed and with all sublists deep-reversed as well. For example, (define x (list (list 1 2) (list 3 4))) x ((1 2) (3 4)) (reverse x) ((3 4) (1 2)) (deep-reverse x) ((4 3) (2 1)) This file contains hidden or bidirectional Unicode text that may…
-
Sicp Exercise 2.26
Exercise 2.26. Suppose we define x and y to be two lists: (define x (list 1 2 3)) (define y (list 4 5 6)) What result is printed by the interpreter in response to evaluating each of the following expressions: (append x y) (cons x y) (list x y) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the…
-
Sicp Exercise 2.25
Exercise 2.25. Give combinations of cars and cdrs that will pick 7 from each of the following lists: (1 3 (5 7) 9) ((7)) (1 (2 (3 (4 (5 (6 7)))))) 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…
-
Sicp Exercise 2.24
Exercise 2.24. Suppose we evaluate the expression (list 1 (list 2 (list 3 4))). Give the result printed by the interpreter, the corresponding box-and-pointer structure, and the interpretation of this as a tree (as in figure 2.6). This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To…
-
Sicp Exercise 2.23
Exercise 2.23. The procedure for-each is similar to map. It takes as arguments a procedure and a list of elements. However, rather than forming a list of the results, for-each just applies the procedure to each of the elements in turn, from left to right. The values returned by applying the procedure to the elements are…