scheme

Seasoned Schemer's get-first, get-next, and waddle functions

你。 提交于 2020-01-02 05:06:05
问题 (define get-first (lambda (l) (call-with-current-continuation (lambda (here) (set! leave here) (waddle l) (leave (quote ())))))) (define get-first (lambda (l) (call-with-current-continuation (lambda (here) (set! leave here) (leave (waddle l)))))) For anybody not familiar with the book "The Seasoned Schemer", get-first , get-next , and waddle (last two not defined here) are procedures to apparently model coroutines to iterate through a tree passed to waddle that yields leaves only. Just prior

Using trace to display a procedure in racket

北城以北 提交于 2020-01-02 04:46:06
问题 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

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

僤鯓⒐⒋嵵緔 提交于 2020-01-02 03:53:06
问题 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

Variable in a function

故事扮演 提交于 2020-01-02 03:30:11
问题 I have see the following code... The first call of (next-num) returns 1 , and the second returns 2 . (define next-num (let ((num 0)) (lambda () (set! num (+ num 1)) num))) (next-num) ; 1 (next-num) ; 2 What I can not understand is... num is created by let inside next-num , it is kind of a local variable... How does scheme know that each time next-num is called, the value of num is not erased by let ((num 0)) ; How does scheme know that it is always the same num that we modify whenever next

Using “do” in Scheme

隐身守侯 提交于 2020-01-01 11:56:07
问题 What is the difference between CODE SNIPPET 1 and CODE SNIPPET 2? ;CODE SNIPPET 1 (define i 0) (do () ((= i 5)) ; Two sets of parentheses (display i) (set! i (+ i 1))) ;CODE SNIPPET 2 (define i 0) (do () (= i 5) ; One set of parentheses (display i) (set! i (+ i 1))) The first code snippet produces 01234 and the second produces 5. What is going on? What does the extra set of parentheses do? Also, I have seen [(= i 50)] used instead of ((= i 5)) . Is there a distinction? Thanks! 回答1: The

Anonymous lambdas directly referring to themselves

≯℡__Kan透↙ 提交于 2020-01-01 10:37:21
问题 Does Scheme or do any dialects of scheme have a kind of "self" operator so that anonymous lambdas can recur on themselves without doing something like a Y-combinator or being named in a letrec etc. Something like: (lambda (n) (cond ((= n 0) 1) (else (* n (self (- n 1))))))) 回答1: No. The trouble with the "current lambda" approach is that Scheme has many hidden lambdas. For example: All the let forms (including let* , letrec , and named let ) do (which expands to a named let ) delay , lazy ,

Anonymous lambdas directly referring to themselves

倾然丶 夕夏残阳落幕 提交于 2020-01-01 10:36:50
问题 Does Scheme or do any dialects of scheme have a kind of "self" operator so that anonymous lambdas can recur on themselves without doing something like a Y-combinator or being named in a letrec etc. Something like: (lambda (n) (cond ((= n 0) 1) (else (* n (self (- n 1))))))) 回答1: No. The trouble with the "current lambda" approach is that Scheme has many hidden lambdas. For example: All the let forms (including let* , letrec , and named let ) do (which expands to a named let ) delay , lazy ,

(define (average …)) in Lisp

妖精的绣舞 提交于 2020-01-01 09:36:13
问题 I'm just playing around with scheme/lisp and was thinking about how I would right my own definition of average . I'm not sure how to do some things that I think are required though. define a procedure that takes an arbitrary number of arguments count those arguments pass the argument list to (+) to sum them together Does someone have an example of defining average ? I don't seem to know enough about LISP to form a web search that gets back the results I'm looking for. 回答1: The definition

using lambda instead of let in scheme

旧时模样 提交于 2020-01-01 09:28:11
问题 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. 回答1: 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

How can you rewrite “begin” in Scheme?

有些话、适合烂在心里 提交于 2020-01-01 08:18:52
问题 As the Wikipedia article explains, begin in Scheme is a library form that can be rewritten using more fundamental forms like lambda . But how do you rewrite a begin , especially considering the following? x ===> error: undefined identifier: x (begin (define x 28) x) ===> 28 x ===> 28 回答1: You cannot. The thing is that begin has two roles: one is sequence a bunch of side-effectful expressions, and the other is that it is used to "splice" macro results. The fact that you can use begin with a