mit-scheme

Value returned by a define expression in Scheme

风流意气都作罢 提交于 2019-12-12 13:43:12
问题 I ran this in MIT/GNU Scheme: (define x (+ 2 3)) The interpreter then prints: ;Value: x But according to my textbook, the value returned by a define expression should be undefined. Why did the interpreter print ";Value: x" then? 回答1: If the standard report does not specify a return or mentions that it is undefined an implementation is literally free to choose the value returned and it would be according to the standard. That also means you cannot depend on one implementations behaviour would

TypeError, the object is not applicable

こ雲淡風輕ζ 提交于 2019-12-11 13:06:43
问题 I wrote a relatively simple bank account function, however when I try to run it I get an TypeError and I'm not sure to why? It's straight out of SICP so the solution is readily available, I just want to understand why my answer produces that error. Any thoughts? Here is my code: (define (make-password-account balance password) (define (withdraw amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient Funds")) (define (deposit amount) (set! balance (+

Transpose list of tuples filling with empty lists

蓝咒 提交于 2019-12-11 12:34:00
问题 I'm new to Scheme and I'm trying to write a procedure which combines n list into a list of n -tuples. If the lists are of different size, the tuples should contain the empty list () when the corresponding list ran out of elements. My current implementation is the following: (define (comb list1 list2) (cond [(empty? list1) empty] [(empty? list2) empty] [else (cons (list (first list1) (first list2)) (comb (rest list1) (rest list2)))])) However, this program doesn't produce another tuple when

How to implement iteration of lambda calculus using scheme lisp?

我的未来我决定 提交于 2019-12-11 08:04:38
问题 I'm trying to learn lambda calculus and Scheme Lisp. The tutorial on lambda calculus can be found here http://www.inf.fu-berlin.de/lehre/WS03/alpi/lambda.pdf. The problem I'm facing is I don't know how to properly implement iteration. (define (Y y) (((lambda (x) (y (x x))) (lambda (x) (y (x x)))))) (define (sum r n) ((is_zero n) n0 (n succ (r (pred n))))) (display ((Y sum) n5)) I always get this error: Aborting!: maximum recursion depth exceeded I know the problem is about the evaluation

How do you find where an error has occurred in MIT scheme?

。_饼干妹妹 提交于 2019-12-11 06:16:21
问题 When you get an error in MIT scheme it doesn't tell you where the error occurred. For example, it just prints something like this: ;Unbound variable: top-left ;To continue, call RESTART with an option number: ; (RESTART 3) => Specify a value to use instead of top-left. ; (RESTART 2) => Define top-left to a given value. ; (RESTART 1) => Return to read-eval-print level 1. How do I find where this error occurred in my code? 回答1: In mit-scheme, if you are using the REPL from a shell, you can call

decent way of nested definition in scheme

≡放荡痞女 提交于 2019-12-10 18:53:53
问题 I want to define a constant foo using an auxiliary function, say, bar . And I want to hide bar inside the definition of foo , so I come with this code: (define foo (define (bar n) (+ n n)) (bar 1)) However, this definition causes syntax errors in many scheme implementations(mit-scheme, racket, guile, etc.). I have three workarounds but none of them seems satisfactory: (define foo1 ((lambda () (define (bar n) (+ n n)) (bar 1)))) (define foo2 (let ((bar (lambda (n) (+ n n)))) (bar 1))) (define

How to use (read) correctly in mit-scheme?

青春壹個敷衍的年華 提交于 2019-12-10 17:21:13
问题 I read in the documentation and rosetta code that (read) is used to get input from the console. So I wrote this code to check this: (display (+ (read) 1)) But mit-scheme never asks for user input and the program just terminates. Why is this the case? 回答1: In the REPL, (display (+ (read) 1)) works as expected. When (display (+ (read) 1)) is placed in a source file, and the file is run as a script using mit-scheme --quiet < program.scm (reference), mit-scheme never asks for user input and the

How to obtain my function definition in MIT Scheme?

こ雲淡風輕ζ 提交于 2019-12-10 14:09:32
问题 In JavaScript, I can retrieve the "source code" definition of a function, for example: ​function alert_Hi() { alert("Hi"); } alert(alert_Hi); will return exactly what I typed. http://jsfiddle.net/DuCqJ/ How can I do this in MIT Scheme? I remember seeing something that returns #compound-procedure or something, but what I really want is the "source code". 回答1: You might try pp (define (display-hi) (display "Hi")) (pp display-hi) => (named-lambda (display-hi) (display "Hi")) MIT-Scheme debugging

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

帅比萌擦擦* 提交于 2019-12-07 20:28:38
问题 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

How do I define a sub environment in scheme?

被刻印的时光 ゝ 提交于 2019-12-06 09:52:16
问题 I am just hacking around with Scheme (mit-scheme) and I have just figured out how you change the environment, so that '+' becomes a symbol for the equivalent procedure of the '-' operator. Example (environment-define user-initial-environment '+ -) (eval (+ 3 2) user-initial-environment) => 1 I was just wondering if there were a simple way to deal with environments as variables so when I input an environment into eval, like so (eval <exp> user-initial-environment) I don't have to use 'user