Category: Scheme

  • Sicp Exercise 3.23

    Exercise 3.23.  A deque (“double-ended queue”) is a sequence in which items can be inserted and deleted at either the front or the rear. Operations on deques are the constructor make-deque, the predicate empty-deque?, selectors front-deque and rear-deque, and mutators front-insert-deque!, rear-insert-deque!, front-delete-deque!, and rear-delete-deque!. Show how to represent deques using pairs, and give implementations of…

  • Sicp Exercise 3.50

    Exercise 3.50.  Complete the following definition, which generalizes stream-map to allow procedures that take multiple arguments, analogous to map in section 2.2.3, footnote 12. (define (stream-map proc . argstreams) (if (<??> (car argstreams)) the-empty-stream (<??> (apply proc (map <??> argstreams)) (apply stream-map (cons proc (map <??> argstreams))))))   This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an…

  • Sicp Exercise 3.35

    Exercise 3.35.  Ben Bitdiddle tells Louis that one way to avoid the trouble in exercise 3.34 is to define a squarer as a new primitive constraint. Fill in the missing portions in Ben’s outline for a procedure to implement such a constraint: (define (squarer a b) (define (process-new-value) (if (has-value? b) (if (< (get-value b) 0) (error “square less than 0 — SQUARER” (get-value b)) <alternative1>) <alternative2>)) (define (process-forget-value) <body1>) (define (me request) <body2>) <rest of definition> me)   This file contains…

  • Sicp Exercise 3.34

    Exercise 3.34.  Louis Reasoner wants to build a squarer, a constraint device with two terminals such that the value of connector b on the second terminal will always be the square of the value a on the first terminal. He proposes the following simple device made from a multiplier: (define (squarer a b) (multiplier a a b)) There is a serious flaw…

  • Sicp Exercise 3.33

    Exercise 3.33.  Using primitive multiplier, adder, and constant constraints, define a procedure averager that takes three connectors a, b, and c as inputs and establishes the constraint that the value of c is the average of the values of a and b. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled…

  • Sicp Exercise 3.29

    Exercise 3.29.  Another way to construct an or-gate is as a compound digital logic device, built from and-gates and inverters. Define a procedure or-gate that accomplishes this. What is the delay time of the or-gate in terms of and-gate-delay and inverter-delay? This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently…

  • Sicp Exercise 3.28

    Exercise 3.28.  Define an or-gate as a primitive function box. Your or-gate constructor should be similar to and-gate.   This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode…

  • Sicp Exercise 3.24

    Exercise 3.24.  In the table implementations above, the keys are tested for equality using equal? (called by assoc). This is not always the appropriate test. For instance, we might have a table with numeric keys in which we don’t need an exact match to the number we’re looking up, but only a number within some tolerance…

  • Sicp Exercise 3.22

    Exercise 3.22.  Instead of representing a queue as a pair of pointers, we can build a queue as a procedure with local state. The local state will consist of pointers to the beginning and the end of an ordinary list. Thus, the make-queue procedure will have the form (define (make-queue) (let ((front-ptr …) (rear-ptr …)) <definitions of internal procedures> (define (dispatch m) …) dispatch)) Complete the…

  • Sicp Exercise 3.19

    Exercise 3.18.  Write a procedure that examines a list and determines whether it contains a cycle, that is, whether a program that tried to find the end of the list by taking successive cdrs would go into an infinite loop. Exercise 3.13 constructed such lists. Exercise 3.19.  Redo exercise 3.18 using an algorithm that takes only a constant amount…