
Recent Posts
Archives
Categories
Author Archives: Frank
SICP solution exercise 2.35
Solution for exercise 2.35 of SICP: (define nil ‘()) (define (accumulate op initial sequence) (if (null? sequence) initial (op (car sequence) (accumulate op initial (cdr sequence))))) (define (countleaves t) (accumulate + 0 (map (lambda (x) (if (pair? x) (countleaves x) … Continue reading
SICP solution exercise 2.34
Solution for exercise 2.34 of SICP: (define (accumulate op initial sequence) (if (null? sequence) initial (op (car sequence) (accumulate op initial (cdr sequence))))) (define (hornereval x coefficientsequence) (accumulate (lambda (thiscoeff higherterms) (+ thiscoeff (* x higherterms))) 0 coefficientsequence)) ;——————– tests … Continue reading
SICP solution exercise 2.33
Solution for exercise 2.33 of SICP: (define nil ‘()) (define (square x) (* x x)) (define (accumulate op initial sequence) (if (null? sequence) initial (op (car sequence) (accumulate op initial (cdr sequence))))) (define (map p sequence) (accumulate (lambda (x y) … Continue reading
SICP solution exercise 2.32
Solution for exercise 2.32 of SICP: (define nil ‘()) (define (subsets s) (if (null? s) (list nil) (let ((rest (subsets (cdr s)))) (append rest (map (lambda (x) (cons (car s) x)) rest))))) (define a (list )) (define b (list 1 … Continue reading
SICP solution exercise 2.31
Solution for exercise 2.31 of SICP: (define (square x) (* x x)) (define (treemap function tree) (map (lambda (subtree) (if (pair? subtree) (treemap function subtree) (function subtree))) tree)) (define (squaretree tree) (treemap square tree)) (squaretree (list 1 (list 2 (list … Continue reading
SICP solution exercise 2.30
Solution for exercise 2.30 of SICP: ;Define squaretree directly (i.e., without using any ;higherorder procedures) (define nil ‘()) (define (squaretreedirect tree) (cond ((null? tree) nil) ((not (pair? tree)) (* tree tree)) (else (cons (squaretreedirect (car tree)) (squaretreedirect (cdr tree)))))) (squaretreedirect … Continue reading
SICP solution exercise 2.29
Solution for exercise 2.29 of SICP: (define (makemobile left right) (list left right)) (define (makebranch length structure) (list length structure)) ;a. Write the corresponding selectors ; * leftbranch and ; * rightbranch, ; which return the branches of a mobile, … Continue reading
SICP solution exercise 2.28
Solution for exercise 2.28 of SICP: ;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 ;lefttoright order. ;For example, (define … Continue reading
SICP solution exercise 2.27
Solution for exercise 2.27 of SICP: ;Modify your reverse procedure of exercise 2.18 to produce ;a deepreverse procedure that takes a list as argument and returns as its value ;the list with its elements reversed and with all sublists deepreversed … Continue reading
SICP solution exercise 2.26
Solution for exercise 2.26 of SICP: ;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 … Continue reading