Google Answers Logo
View Question
Q: Help with Checking Excercise Soultions ( Answered 5 out of 5 stars,   0 Comments )
Subject: Help with Checking Excercise Soultions
Category: Computers > Programming
Asked by: mott85-ga
List Price: $10.00
Posted: 05 Oct 2004 21:02 PDT
Expires: 04 Nov 2004 20:02 PST
Question ID: 410901
I am trying to teach myself scheme so I have the book Structure and
interpretation of computer programs Second edition by Harold Abelson
and Gerald Jay Sussman. I have done some sample exercises however this
book provides no answers to check myself. Is there somewhere I can get
the answers to exercises: 
Exercise 2.2
Exercise 2.30
Exercise 2.36 
Exercise 2.37 ? I would like to see if I
am on the right track. Thanks

Request for Question Clarification by googleexpert-ga on 06 Oct 2004 06:47 PDT
Hi mott85-ga,

I am familiar with the book "Structure and Interpretation of Computer
programs" and know
how difficult most of the exercises are.
However, there are some exercises that are somewhat easy given any
prerequisite knowledge.

Is there an exercise you are sure of the answer to, just so I know
this is not a homework problem?



Clarification of Question by mott85-ga on 06 Oct 2004 22:31 PDT
Thank you for checking to see why i need this. I do understand basic
concepts. For exemple excercise 2.2 i pretty sure i solved correctly
here si the code:
(define (same-parity x . r)
  (define (helper proc lst result)
    (if (null? lst)
	(helper proc (cdr lst) 
		(if (proc (car lst))
		    (append result (list (car lst)))
  (if (even? x)
      (helper even? r (list x))
      (helper odd? r (list x))))
I was also able to solve 2.17 amd 2.23 i was just wnat to check my
thoughts on the ones i listed i would apprecite if some can show me
where a proper soultion is for those.
Subject: Re: Help with Checking Excercise Soultions
Answered By: googleexpert-ga on 07 Oct 2004 22:39 PDT
Rated:5 out of 5 stars
Hi again mott85-ga,
Here are the solutions to exercises ( 2.2, 2.30, 2.36, 2.37 ):

Exercise 2.2:

(define (make-segment sp ep)
  (cons sp ep))

(define (start-segment segment)
  (car segment))

(define (end-segment segment)
  (cdr segment))

(define (make-point x y)
  (cons x y))

(define (x-point point)
  (car point))

(define (y-point point)
  (cdr point))

;;; calc midpoint
;;; midpoint x - (x1 + x2)/2 the same with y
(define (midpoint-segment line)
  (make-point (/ (+ (x-point (start-segment line))
		    (x-point (end-segment line))) 2)
	      (/ (+ (y-point (start-segment line))
		    (y-point (end-segment line))) 2)))

(define (print-point p)
  (display ")")
  (display (x-point p))
  (display ",")
  (display (y-point p))
  (display ")"))

Exercise 2.30

(define (square x) (* x x))

;; Direct implementation
;; Takes a tree of numbers represented as a list
;; returns the tree of numbers where each number
;; in the original tree has been squared
(define (square-tree tree)
  (cond ((null? tree) ())
        ((not (pair? (car tree)))
         (cons (square (car tree))
               (square-tree (cdr tree))))
         (cons (square-tree (car tree))
               (square-tree (cdr tree))))))

;; Map implementation
;; Takes a tree of numbers represented as a list
;; returns the tree of numbers where each number
;; in the original tree has been squared
(define (map-square-tree tree)
  (map (lambda (sub-tree)
         (if (pair? sub-tree)
             (map-square-tree sub-tree)
             (square sub-tree)))

Exercise 2.36

(define (accumulate-n op init seqs)
  (if (null? (car seqs))
      (cons (accumulate op init (map car seqs))
            (accumulate-n op init (map cdr seqs)))))

Exercise 2.37

(define v1 '(1 2 3))
(define v2 '(4 5 6))
(define m2 '((1 2 3) (4 5 6) (7 8 9)))
(define v3 '(2 3 4))
(define v4 '(10 13 16))
(define a '((1 -2 2) (1 0 2)))
(define b '((1 0 1) (3 -1 5) (4 2 0)))
;(matrix-*-matrix a b)
;=((3 6 -9) (9 4 1)) 
(define e1 '((1 0 -2) (0 3 -1)))
(define e2 '((0 3) (-2 -1) (0 4)))
;(matrix-*-matrix e1 e2)
;=((0 -5) (-6 -7))

; dot-product takes two vectors and returns the
; sum of their pairwise multiplication
(define (dot-product v w)
  (accumulate + 0 (map * v w)))

; takes a matrix and a vector and returns a
; vector where each ti in the vector is the
; sum of mij*vj (for all j)
(define (matrix-*-vector m v)
  (map (lambda (w)
         (dot-product v w))

; takes a matrix and returns its transpose
; the transpose of a matrix, mat, is another 
; matrix trans-mat where trans-mat(i,j)=mat(j,i)
(define (transpose mat)
  (accumulate-n cons () mat))

; takes two matrices and multiplies them together
; it returns the matrix result where
; retult(i,j)=sum of k or m(i,k)*n(k,j)
(define (matrix-*-matrix m n)
  (let ((cols (transpose n)))
    (map (lambda (v1) 
           (map (lambda (c1)
                  (dot-product c1 v1))

[Search Strategy]
Used keywords from Textbook exercises and scheme keywords.

Please let me know if you have anymore questions.

Thanks again.


Clarification of Answer by googleexpert-ga on 09 Oct 2004 18:45 PDT
Thank you very much for 5-Star rating! 

I appreciate it .:)
mott85-ga rated this answer:5 out of 5 stars

There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  

Google Home - Answers FAQ - Terms of Service - Privacy Policy