sicp

Small SICP/Scheme question (local state)

I'm actually reading the book for fun but it might be considered homework. At any event, I don't feel comfortable with local state variables at all with this language... Take for example this code: (define flip (let ((count 0)) (lambda () (if (= 0 count) (begin (set! count 1) count) (begin (set! count 0) ...

What's the explanation for Exercise 1.4 in SICP?

I'm just beginning to work through SICP (on my own; this isn't for a class), and I've been struggling with Exercise 1.4 for a couple of days and I just can't seem to figure it out. This is the one where Alyssa re-defines if in terms of cond, like so: (define (new-if predicate then-clause else-clause) (cond (predicate then-clause) ...

How do I include files in DrScheme?

I'm using DrScheme to work through SICP, and I've noticed that certain procedures (for example, square) get used over and over. I'd like to put these in a separate file so that I can include them in other programs without having to rewrite them every time, but I can't seem to figure out how to do this. I've tried: (load filename) (loa...

Seemingly unnecessary case in the unification algorithm in SICP

Hi guys, I'm trying to understand the unification algorithm described in SICP here In particular, in the procedure "extend-if-possible", there's a check (the first place marked with asterix "*") which is checking to see if the right hand "expression" is a variable that is already bound to something in the current frame: (define (extend...

I can't find my error in this Scheme program for calculate PI

I am doing a Monte Carlo experiment to calculate an approximation of PI. From SICP: The Monte Carlo method consists of choosing sample experiments at random from a large set and then making deductions on the basis of the probabilities estimated from tabulating the results of those experiments. For example, we can approx...

Is IronScheme suitable for working through SICP?

Will there be any incompatibilities with the code in SICP if I use IronScheme? ...

Quotation in Scheme

Following is a exercise from SICP. I couldn't figure it out on my own. Can some why help me understand? Type following code into interpreator: (car ''abracadabra) And it print out 'quote'. Why? ...

SICP making change

So; I'm a hobbiest who's trying to work through SICP (it's free!) and there is an example procedure in the first chapter that is meant to count the possible ways to make change with american coins; (change-maker 100) => 292. It's implemented something like: (define (change-maker amount) (define (coin-value n) (cond ((= n 1) 1) ...

Should one just read SICP and not solve problems?

Hi, I am enjoying reading Structure and Interpretation of Computer Programs. But I find exercises to be a blocker. They take lot of time to do, especially in chapter 1-2.Should I just read SICP in one go, and not solve the problems? Did anyone of you faced same problem? My feeling is that doing exercises helps to understand concepts bett...

SICP exercise 1.16, where is my bug, because it looks right to me

I've just started working through this book for fun; I wish it were homework, but I could never afford to attend MIT, and there are tons of people smarter than me anyway. :p fast-exp is supposed to find b^n, i.e. 4^2 = 16, 3^3 = 27 (define (fast-exp b n) (define (fast-exp-iter n-prime a) (cond ((= n-prime 1) a) ((= (rem...

SICP 1.31: Approximating Pi

Hello- I'm working through SICP on my own, so I don't have an instructor to ask about this. This code is supposed to approximate pi but always returns zero instead. (define (approx-pi acc) (define (factors a) (define basic-num (if (= (mod a 2) 0) (/ a 2) (/ (- a 1) 2))) (if (= (mod basic-num ...

Pros and cons of MIT Scheme and DrScheme to study SICP?

All, In your mind, what are the pros and cons of using MIT Scheme versus DrScheme, in the context of trying to go through SICP (presumably simultaneously to watching some / all the MIT 6.001 videos)? Please feel free to edit below: MIT Scheme pros: - Specifically built for SICP and MIT 6.001. MIT Scheme cons: DrScheme pros: - Wid...

the difference between if and cond?

i'm learning sicp now and do the ex2.23 i have wrirten the following code: (define (for-each proc items) (if (null? items) #t ((proc (car items)) (for-each proc (cdr items))))) but when running, cause error: procedure application: expected procedure, given: #; arguments were: () i think i know the reason: I c...

SICP exercise 1.19

It's a procedure to genearate the fibonacci numbers, here is the reference: http://sicp.org.ua/sicp/Exercise1-19 it's said that we can consider the procedure as "a <- bq + aq + ap and b <- bp + aq".My question is how the auther(or someone else) think out this good idea?Has it to be this form? ...

Clojure- why doesn't this piece of code work in clojure, is there some lazy evaluation gotcha I am missing ?

Am new to clojure and learning it by working through SICP. I cannot get this piece of code from SCIP 1.3.1 to work. What am I missing ? (defn sum [term a next b] (if (> a b) 0 (+ (term a) (sum term (next a) next b)))) (defn sum-cubes-new [a b] ((sum cube a inc b))) HERE is the error message: java.lang.Integer can...

Is there an equivalent to Lisp's "runtime" primitive in Scheme?

According to SICP section 1.2.6, exercise 1.22: Most Lisp implementations include a primitive called runtime that returns an integer that specifies the amount of time the system has been running (measured, for example, in microseconds). I'm using DrScheme, where runtime doesn't seem to be available, so I'm looking for a good substi...

No idea how to solve this SICP exercise

This is not homework. Exercise 1.11: A function f is defined by the rule that f(n) = n if n<3 and f(n) = f(n - 1) + 2f(n - 2) + 3f(n - 3) if n> 3. Write a procedure that computes f by means of a recursive process. Write a procedure that computes f by means of an iterative process. Implementing it recursively is simple enough. Bu...

Create a polynomial object from a number using change-class

I have written a polynomial class along the lines described in SICP 2.5.3 (except using defclass). I would like to be able to seamlessly add and multiply polynomials and regular numbers but I can't make change-class accept a number. I tried to simplify the problem by changing class from an integer to a float: (change-class 4 'float) ...

Scheme procedure problem

I defined the Scheme procedure to return another procedure with 2 parameters : (define (smooth f) (λ(x dx)(/ (+ (f (- x dx)) (f x) (f (+ x dx))) 3.0))) if i run this procedure with sin procedure with 2 arguments 10 and 0.0001 then it is ok ((smooth sin) 10 0.0001) ==> -0.544021109075966 if i ...

Problem with circular definition in Scheme

I am currently working through SICP using Guile as my primary language for the exercises. I have found a strange behavior while implementing the exercises in chapter 3.5. I have reproduced this behavior using Guile 1.4, Guile 1.8.6 and Guile 1.8.7 on a variety of platforms and am certain it is not specific to my setup. This code works f...