sicp

Why is it legal in a function definition to make self-call but illegal for a value?

吃可爱长大的小学妹 提交于 2019-12-06 09:37:05
问题 Structure and Interpretation of Computer Programs (SICP) 3.5.2 introduces infinite streams: (define ones (cons-stream 1 ones)) This code doesn't work in DrRacket, with the error: ones: undefined; cannot reference an identifier before its definition Other code like this: (define (integers-starting-from n) (cons-stream n (integers-starting-from (+ n 1)))) (define integers (integers-starting-from 1)) produce error: Interactions disabled (fall in infinite loop?) So far as I read(SICP), the key

Error setting load-noisily? and auto-exiting in MIT-Scheme

自作多情 提交于 2019-12-06 08:46:24
In order to debug MIT-Scheme scripts with Vim, I want to be able to run the script file currently being edited as conveniently as possible. Here is what I'm doing: sicp.scm (set! load-noisily? #t) (define (abs x) (cond ((> x 0) x) ((= x 0) 0) ((< x 0) (- x)) ) ) (abs 42) (abs -24) (exit) After executing :!mit-scheme --eval "(load \"sicp\")" when editing sicp.scm in Vim, I get: Image saved on Saturday May 17, 2014 at 2:39:25 AM Release 9.2 || Microcode 15.3 || Runtime 15.7 || SF 4.41 || LIAR/x86-64 4.118 Edwin 3.116 ;Loading "sicp.scm"... Kill Scheme (y or n)? There are two main issues: The

How do I include files in DrScheme?

旧城冷巷雨未停 提交于 2019-12-05 21:18:37
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) (load (filename)) (load ~/path-to-directory/filename) (require filename) (require ~/path-to-directory/filename) (require path-from-root/filename) None of these works. Obviously I'm grasping at straws -- any help is much appreciated. It's not clear from your question

Lexical vs dynamic scoping in terms of SICP's Environment Model of Evaluation

喜夏-厌秋 提交于 2019-12-05 15:23:00
问题 In Section 3.2.2 of SICP the execution of the following piece of code (define (square x) (* x x)) (define (sum-of-squares x y) (+ (square x) (square y))) (define (f a) (sum-of-squares (+ a 1) (* a 2))) (f 5) is explained in terms of this diagram. Each time a function is applied, a new frame is created (labeled by E1 through E4 ) which represents a set of bindings between symbols and values. When a symbol is not bound in a frame, that frame's enclosing environment is queried for a binding of

Using trace to display a procedure in racket

人盡茶涼 提交于 2019-12-05 10:54:35
I've been working through the last few exercises ch 1 of SICP, where several of the exercises uses higher-order functions. Currently I'm trying to debug a problem in my solution to 1.45, which is raising an arity mismatch. The function which is raising the error is the result of twice applying an averaging operation to a fixed-point function solver. It would make my debugging efforts a lot easier if I could just dump some sort of representation of procedures, given that the procedure has been run through several other procedures that alter it before it raises an error. I've looked at the

Is there any difference between closure in Scheme and usual closure in other languages?

橙三吉。 提交于 2019-12-05 06:51:29
I'm studying SICP right now. And I found the definition of closure in SICP is (maybe) different from closure definition in other languages. Here's what SICP says: The ability to create pairs whose elements are pairs is the essence of list structure's importance as a representational tool. We refer to this ability as the closure property of cons. In general, an operation for combining data objects satisfies the closure property if the results of combining things with that operation can themselves be combined using the same operation. Here closure is more close to closure in Mathematics I think,

Writing a Scheme interpreter with FPC: Recursive data structures

感情迁移 提交于 2019-12-05 03:15:57
问题 Essentially, this is a question about recursive data structures in Pascal (FPC). As I would like to implement a Scheme interpreter like it is shown in SICP chapter 4, this question may be relevant for Schemers as well. :) S-expressions shall be represented as tagged data. So far, I have constructed a variant record, which represents numbers and pairs. Hopefully the code is readable and self-explanatory: program scheme; type TTag = (ScmFixnum, ScmPair); PScmObject = ^TScmObject; TScmObject =

SICP, Continuation Passing Style and Clojure's trampoline

爷,独闯天下 提交于 2019-12-04 19:57:23
I am working with SICP and exercise 2.29-b gave me the opportunity to have fun with the Continuation Passing Style while traversing mobiles and branches. To make the story short, each mobile has left and right branch, which are composed by a length and either a numeric weight or another mobile. The question asks to find the total weight given a mobile. After the first mutually recursive solution, quite simple, I tried and successfully implemented a cps' one: (defn total-weight-cps [mobile] (letfn [(branch-weight-cps [branch kont] (let [structure (branch-structure branch)] (if (mobile? (branch

using lambda instead of let in scheme

烂漫一生 提交于 2019-12-04 04:19:35
In SICP 1.2.1 there is a function that makes a rational number, as follow: (define (make-rat n d) (let ((g (gcd n d))) (cons (/ n g) (/ d g)))) I'm just curious how you can implement the same thing using lambda instead of let, without calling GCD twice. I couldn't figure it out myself. Looking at SICP section 1.3.2 , (let ((<var1> <exp1>) (<var2> <exp2>) ... (<varn> <expn>)) <body>) is equivalent to ((lambda (<var1> ...<varn>) <body>) <exp1> ... <expn>) So your procedure, (define (make-rat n d) (let ((g (gcd n d))) (cons (/ n g) (/ d g)))) should be equivalent to (define (make-rat n d) (

Writing a Scheme interpreter with FPC: Recursive data structures

对着背影说爱祢 提交于 2019-12-03 17:33:53
Essentially, this is a question about recursive data structures in Pascal (FPC). As I would like to implement a Scheme interpreter like it is shown in SICP chapter 4, this question may be relevant for Schemers as well. :) S-expressions shall be represented as tagged data. So far, I have constructed a variant record, which represents numbers and pairs. Hopefully the code is readable and self-explanatory: program scheme; type TTag = (ScmFixnum, ScmPair); PScmObject = ^TScmObject; TScmObject = record case ScmObjectTag: TTag of ScmFixnum: (ScmObjectFixnum: integer); ScmPair: (ScmObjectCar,