Dan's Thoughts Thinking somewhat carefully

12Aug/090

Exercise 2.27 of SICP

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))
(define (deep-reverse x)
  (cond ((null? x) '())
        ((not (pair? x))
         (list x))
        ((not (pair? (car x)))
         (append (deep-reverse (cdr x)) (list (car x))))
        (else
          (append (deep-reverse (cdr x)) 
                  (list (deep-reverse (car x)))))))

> (define x (list (list 1 2) (list 3 4)))
> (deep-reverse x)
((4 3) (2 1))
> (define y (list (list (list 1 2)) (list 3 4)))
> y
(((1 2)) (3 4))
> (deep-reverse y)
((4 3) ((2 1)))

Filed under: SICP, lisp No Comments
11Aug/090

Exercise 2.26 of SICP

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)
(1 2 3 4 5 6)
(cons x y)
((1 2 3) 4 5 6)
(list x y)
((1 2 3) (4 5 6))
Filed under: SICP, lisp No Comments
10Aug/090

Exercise 2.25 of SICP

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))))))

> (define a (list 1 3 (list 5 7) 9))
> (car (cdr (car (cdr (cdr a)))))
7

> (define a (list (list 7)))
> (car (car a))
7

> (define a (list 1 (list 2 (list 3 (list 4 (list 5 (list 6 7)))))))
> (car (cdr (car (cdr (car (cdr (car (cdr (car (cdr (car (cdr a))))))))))))
7

Filed under: SICP, lisp No Comments
9Aug/090

Exercise 2.24 of SICP

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).

(1 (2 (3 4)))
1
2
3
4
5
[1 *]->[* /]
       |
       [2 *]->[* /]
              |
              [3 *]->[4 /]
Filed under: SICP, lisp No Comments
8Aug/090

Exercise 2.23 of SICP

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 not used at all -- for-each is used with procedures that perform an action, such as printing. For example,

(for-each (lambda (x) (newline) (display x))
          (list 57 321 88))
57
321
88

The value returned by the call to for-each (not illustrated above) can be something arbitrary, such as true. Give an implementation of for-each.

(define (for-each proc terms)
  (cond ((not (null? terms))
         (proc (car terms))
         (for-each proc (cdr terms)))
        (else
          (newline))))
Filed under: SICP, lisp No Comments
7Aug/090

Exercise 2.22 of SICP

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?

The reason the above works in reverse is because cons step of iter, cons puts new elements to the left of list answer.
(list 1 2 3) end up as (list 1), (list 2 1) and finally (list 3 2 1).

Louis then tries to fix his bug by interchanging the arguments to cons:

(define (square-list items)
  (define (iter things answer)
    (if (null? things)
        answer
        (iter (cdr things)
              (cons answer
                    (square (car things))))))
  (iter items nil))

This doesn't work either. Explain.

Because (square (car things)) is not a list, cons creates a dotted pair out of answer which is an empty list and a number.
The result looks like this:
> (square-list (list 1 2 3 4))
((((() . 1) . 4) . 9) . 16)

Filed under: SICP, lisp No Comments
6Aug/090

Exercise 2.21 of SICP

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 <??> <??>))
(define (square-list items)
  (if (null? items)
    '()
    (cons 
      ((lambda (x) (* x x))
       (car items))
      (square-list (cdr items)))))
 
(define (square-list2 items)
  (map (lambda (x) (* x x)) items))

> (square-list (list 1 2 3 4 5))
(1 4 9 16 25)
> (square-list2 (list 1 2 3 4 5))
(1 4 9 16 25)

Filed under: SICP, lisp No Comments
5Aug/090

Exercise 2.20 of SICP

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 as values the initial arguments, as usual, but the final parameter's value will be a list of any remaining arguments. For instance, given the definition

(define (f x y . z) )

the procedure f can be called with two or more arguments. If we evaluate

(f 1 2 3 4 5 6)

then in the body of f, x will be 1, y will be 2, and z will be the list (3 4 5 6). Given the definition

(define (g . w) )

the procedure g can be called with zero or more arguments. If we evaluate

(g 1 2 3 4 5 6)

then in the body of g, w will be the list (1 2 3 4 5 6).11

Use this notation to write a procedure same-parity that takes one or more integers and returns a list of all the arguments that have the same even-odd parity as the first argument. For example,

(same-parity 1 2 3 4 5 6 7)
(1 3 5 7)

(same-parity 2 3 4 5 6 7)
(2 4 6)
(define (same-parity a . rest)
  (define (filter rest)
    (cond ((null? rest) '())
          ((= (remainder a 2) (remainder (car rest) 2))
           (cons (car rest) (filter (cdr rest))))
          (else
            (filter (cdr rest)))))
  (filter (cons a rest)))

> (same-parity 2 3 4 5 6 7)
(2 4 6)
> (same-parity 1 2 3 4 5 6 7)
(1 3 5 7)

Filed under: SICP, lisp No Comments
4Aug/090

Exercise 2.19 of 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 rather than an integer specifying which coins to use. We could then have lists that defined each kind of currency:

(define us-coins (list 50 25 10 5 1))
(define uk-coins (list 100 50 20 10 5 2 1 0.5))

We could then call cc as follows:

(cc 100 us-coins)
292

To do this will require changing the program cc somewhat. It will still have the same form, but it will access its second argument differently, as follows:

(define (cc amount coin-values)
  (cond ((= amount 0) 1)
        ((or (&lt; amount 0) (no-more? coin-values)) 0)
        (else
         (+ (cc amount
                (except-first-denomination coin-values))
            (cc (- amount
                   (first-denomination coin-values))
                coin-values)))))

Define the procedures first-denomination, except-first-denomination, and no-more? in terms of primitive operations on list structures. Does the order of the list coin-values affect the answer produced by cc? Why or why not?

I wanted to use lexical scope so the whole program is bellow:

(define (cc amount coin-values)
  (define (no-more? coin-values)
    (null? coin-values))
  (define (first-denomination coin-values)
    (car coin-values))
  (define (except-first-denomination coin-values)
    (cdr coin-values))
  (cond ((= amount 0) 1)
        ((or (< amount 0) (no-more? coin-values)) 0)
        (else
         (+ (cc amount
                (except-first-denomination coin-values))
            (cc (- amount
                   (first-denomination coin-values))
                coin-values)))))
(define us-coins (list 50 25 10 5 1))
(define uk-coins (list 100 50 20 10 5 2 1 0.5))

> (cc 100 us-coins)
292
> (cc 100 uk-coins)
104561
> (cc 100 (list 50 5 25 1 10))
292
> (cc 100 (list 50 2 10 5 100 1 0.5 20))
104561

The order is irrelevant as long as all the coin values are in the list. This is true by looking and what the two recursion branches do:

Branch 1:

(cc amount (except-first-denomination coin-values))

will always bottom out. As the matter of fact, this branch doesn't even care if coin-values are numbers let alone what order they are in! As long as the (length coin-values) is the right number (in the case of us-coins that number is 5) it will execute without complaining.

Branch 2:

(cc (- amount (first-denomination coin-values)) coin-values)

this branch will subtract every term in coin-values until the amount less that or equal to 0 or coin-values is empty. The reason order is irrelevant is because coupled with the first branch, this second branch makes sure that all sublists of coin-values is evaluated.

While the order doesn't matter in the final result of computation it does matter in the "shape" of the computational process.
For instance (cc 51 (list 1 50)) has twice the amount of nodes as (cc 51 (list 50 1))

Filed under: SICP, lisp No Comments
3Aug/090

Exercise 2.18 of SICP

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)
(define (reverse-rec ls)
  (if (null? ls) 
    '()
    (append (reverse-rec (cdr ls)) (list (car ls)))))
(define (reverse ls)
  (define (reverse-iter ol nl)
    (if (null? ol)
      nl
      (reverse-iter (cdr ol) (cons (car ol) nl))))
  (reverse-iter ls '()))

> (reverse-rec (list 1 2 3 4))
(4 3 2 1)
> (reverse (list 1 2 3 4))
(4 3 2 1)

Filed under: SICP, lisp No Comments