Tag Archives: scheme

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 (count-leaves t) (accumulate + 0 (map (lambda (x) (if (pair? x) (count-leaves x) … Continue reading

Posted in SICP | Tagged , | Leave a comment

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 (horner-eval x coefficient-sequence) (accumulate (lambda (this-coeff higher-terms) (+ this-coeff (* x higher-terms))) 0 coefficient-sequence)) ;——————– tests … Continue reading

Posted in SICP | Tagged , | Leave a comment

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

Posted in SICP | Tagged , | Leave a comment

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

Posted in SICP | Tagged , | Leave a comment

SICP solution exercise 2.31

Solution for exercise 2.31 of SICP: (define (square x) (* x x)) (define (tree-map function tree) (map (lambda (subtree) (if (pair? subtree) (tree-map function subtree) (function subtree))) tree)) (define (square-tree tree) (tree-map square tree)) (square-tree (list 1 (list 2 (list … Continue reading

Posted in SICP | Tagged , | Leave a comment

SICP solution exercise 2.30

Solution for exercise 2.30 of SICP: ;Define square-tree directly (i.e., without using any ;higher-order procedures) (define nil ‘()) (define (square-tree-direct tree) (cond ((null? tree) nil) ((not (pair? tree)) (* tree tree)) (else (cons (square-tree-direct (car tree)) (square-tree-direct (cdr tree)))))) (square-tree-direct … Continue reading

Posted in SICP | Tagged , | Leave a comment

SICP solution exercise 2.29

Solution for exercise 2.29 of SICP: (define (make-mobile left right) (list left right)) (define (make-branch length structure) (list length structure)) ;a. Write the corresponding selectors ; * left-branch and ; * right-branch, ; which return the branches of a mobile, … Continue reading

Posted in SICP | Tagged , | Leave a comment

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 ;left-to-right order. ;For example, (define … Continue reading

Posted in SICP | Tagged , | Leave a comment

SICP solution exercise 2.27

Solution for exercise 2.27 of SICP: ;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 … Continue reading

Posted in SICP | Tagged , | Leave a comment

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

Posted in SICP | Tagged , | Leave a comment