Hi again mott85-ga,
Here are the solutions to exercises ( 2.2, 2.30, 2.36, 2.37 ):
Exercise 2.2:
Source: http://t2.technion.ac.il/~seyal18/Chapter2/ex2-2.txt
(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)
(newline)
(display ")")
(display (x-point p))
(display ",")
(display (y-point p))
(display ")"))
Exercise 2.30
Source: http://www.cis.udel.edu/~mccoy/courses/cisc280-04s/Homeworks/hw3/hw3-answers.scm
(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))))
(else
(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)))
tree))
Exercise 2.36
Source:
http://www.cis.udel.edu/~mccoy/courses/cisc280-04s/Homeworks/hw4/hw4-answers.scm
(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
Source: http://www.cis.udel.edu/~mccoy/courses/cisc280-04s/Homeworks/hw4/hw4-answers.scm
(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))
m))
; 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))
cols))
m)))
[Search Strategy]
Used keywords from Textbook exercises and scheme keywords.
Please let me know if you have anymore questions.
Thanks again.
-googleexpert |