sicp

In SICP exercise 2.26 using DrScheme, why does cons return a list, instead of a pair of lists?

我们两清 提交于 2019-11-30 20:50:17
In SICP exercise 2.26, this Scheme code is given: (define x (list 1 2 3)) (define y (list 4 5 6)) Then this cons call is given: (cons x y) I expected a pair of lists would result, ((1 2 3) (4 5 6)) but the interpreter gives, ((1 2 3) 4 5 6) ...a list with 4 elements, the first being a list. Why is y treated differently? I've tried looking up other SICP answers for an explanation, but couldn't find something satisfactory. So could any Scheme/Lisp experts please shed some light on this aspect of cons? Thanks in advance for any insight. '((1 2 3) 4 5 6) is actually a pair of lists. Here's another

how is the sicp cons-stream implemented?

ぃ、小莉子 提交于 2019-11-30 15:09:42
I'm working through the streams section of the scip and am stuck on how to define a stream. The following is my code: (define (memo-func function) (let ((already-run? false) (result false)) (lambda () (if (not already-run?) (begin (set! result (function)) (set! already-run? true) result) result)))) (define (delay exp) (memo-func (lambda () exp))) (define (force function) (function)) (define the-empty-stream '()) (define (stream-null? stream) (null? stream)) (define (stream-car stream) (car stream)) (define (stream-cdr stream) (force (cdr stream))) (define (cons-stream a b) (cons a (memo-func

Managing state - chapter 3 of SICP

安稳与你 提交于 2019-11-30 14:54:33
问题 I've been working through in Structure and Interpretation of Computer Programs and completing the exercises in Haskell. The first two chapters were fine (code at github) but Chapter 3 is making me think harder. It starts by talking about managing state, with the example of a bank account. They define a function make-withdraw by (define (make-withdraw balance) (lambda (amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds"))) so that you can

Pros and cons of MIT Scheme and DrScheme for studying SICP [closed]

倾然丶 夕夏残阳落幕 提交于 2019-11-30 14:01:05
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . What are the pros and cons of using MIT Scheme versus using DrScheme, in the context of trying to go through SICP (presumably

mcons in dr racket

喜夏-厌秋 提交于 2019-11-30 12:44:47
I'm having trouble reading output from dr racket. By default it displays lists using mcons. For example, sicp exercise 2.32 produces: > (subsets (list 1 2 3)) (mcons (mcons '() (mcons (mcons 3 '()) (mcons (mcons 2 '()) (mcons (mcons 2 (mcons 3 '())) (mcons (mcons 1 '()) (mcons (mcons 1 (mcons 3 '())) (mcons (mcons 1 (mcons 2 '())) (mcons (mcons 1 (mcons 2 (mcons 3 '()))) '())))))))) '()) I'm having trouble reading this. Is there a way to make the output look like: (() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3)) Thanks! Do you know what language are you using in your #lang line? The rest of the

SICP Exercise 1.3 request for comments

偶尔善良 提交于 2019-11-30 12:35:25
问题 I'm trying to learn scheme via SICP. Exercise 1.3 reads as follow: Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers. Please comment on how I can improve my solution. (define (big x y) (if (> x y) x y)) (define (p a b c) (cond ((> a b) (+ (square a) (square (big b c)))) (else (+ (square b) (square (big a c)))))) 回答1: Looks ok to me, is there anything specific you want to improve on? You could do something like: (define (max2

Managing state - chapter 3 of SICP

孤街醉人 提交于 2019-11-30 12:24:28
I've been working through in Structure and Interpretation of Computer Programs and completing the exercises in Haskell. The first two chapters were fine (code at github ) but Chapter 3 is making me think harder. It starts by talking about managing state, with the example of a bank account. They define a function make-withdraw by (define (make-withdraw balance) (lambda (amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds"))) so that you can execute the following code: (define w1 (make-withdraw 100)) (define w2 (make-withdraw 100)) (w1 50) 50 (w2

Running SICP Pattern Matching Rule Based Substitution Code

烂漫一生 提交于 2019-11-30 05:16:34
I have found the code from this lesson online (http://groups.csail.mit.edu/mac/ftpdir/6.001-fall91/ps4/matcher-from-lecture.scm), and I am having a heck of a time trying to debug it. The code looks pretty comparable to what Sussman has written: ;;; Scheme code from the Pattern Matcher lecture ;; Pattern Matching and Simplification (define (match pattern expression dictionary) (cond ((eq? dictionary 'failed) 'failed) ((atom? pattern) (if (atom? expression) (if (eq? pattern expression) dictionary 'failed) 'failed)) ((arbitrary-constant? pattern) (if (constant? expression) (extend-dictionary

In SICP exercise 2.26 using DrScheme, why does cons return a list, instead of a pair of lists?

北城以北 提交于 2019-11-30 05:16:16
问题 In SICP exercise 2.26, this Scheme code is given: (define x (list 1 2 3)) (define y (list 4 5 6)) Then this cons call is given: (cons x y) I expected a pair of lists would result, ((1 2 3) (4 5 6)) but the interpreter gives, ((1 2 3) 4 5 6) ...a list with 4 elements, the first being a list. Why is y treated differently? I've tried looking up other SICP answers for an explanation, but couldn't find something satisfactory. So could any Scheme/Lisp experts please shed some light on this aspect

how is the sicp cons-stream implemented?

99封情书 提交于 2019-11-29 21:55:20
问题 I'm working through the streams section of the scip and am stuck on how to define a stream. The following is my code: (define (memo-func function) (let ((already-run? false) (result false)) (lambda () (if (not already-run?) (begin (set! result (function)) (set! already-run? true) result) result)))) (define (delay exp) (memo-func (lambda () exp))) (define (force function) (function)) (define the-empty-stream '()) (define (stream-null? stream) (null? stream)) (define (stream-car stream) (car