the-little-schemer

Scheme: Is it possible to convert a list of S-expressions into a list of atoms?

*爱你&永不变心* 提交于 2021-01-28 11:26:14
问题 I am trying to convert a list of S-expressions to a plain list of atoms similar to a problem in the book The Little Schemer . My code is (as typed in Dr.Racket): > (define lat '((coffee) cup ((tea) cup) (and (hick)) cup)) > (define f (lambda (lat) (cond ((null? lat) (quote ())) ((atom? (car lat)) (cons (car lat) (f (cdr lat)))) (else (cons (f (car lat)) (f (cdr lat))))))) > (f lat) '((coffee) cup ((tea) cup) (and (hick)) cup) The above code is returning back the list the same as input list. I

The Little Schemer evens-only*&co

筅森魡賤 提交于 2020-01-20 19:14:10
问题 I'm having difficulty understanding what's going on with The Little Schemer's evens-only*&co example on page 145. Here's the code: (define evens-only*&co (lambda (l col) (cond ((null? l) (col '() 1 0)) ((atom? (car l)) (cond ((even? (car l)) (evens-only*&co (cdr l) (lambda (newl product sum) (col (cons (car l) newl) (opx (car l) product) sum)))) (else (evens-only*&co (cdr l) (lambda (newl product sum) (col newl product (op+ (car l) sum))))))) (else (evens-only*&co (car l) (lambda (newl

The mechanism of anonymous function to call itself in Scheme?

不羁的心 提交于 2020-01-03 14:07:26
问题 I'm reading The Little Schemer and feel confused about the following code: ((lambda (len) (lambda (l) (cond ((null? l) 0) (else (+ 1 (len (cdr l))))))) eternity) (define eternity (lambda (x) (eternity x))) The code is to determine the empty list, otherwise it never stops. Why is the " len " not recursion? 回答1: Although it can be dangerous to apply textual substitution to Lisp forms (since there are dangers of multiple evaluation, etc.), in this case it may help to look at this form and see

The mechanism of anonymous function to call itself in Scheme?

南楼画角 提交于 2020-01-03 14:07:09
问题 I'm reading The Little Schemer and feel confused about the following code: ((lambda (len) (lambda (l) (cond ((null? l) 0) (else (+ 1 (len (cdr l))))))) eternity) (define eternity (lambda (x) (eternity x))) The code is to determine the empty list, otherwise it never stops. Why is the " len " not recursion? 回答1: Although it can be dangerous to apply textual substitution to Lisp forms (since there are dangers of multiple evaluation, etc.), in this case it may help to look at this form and see

Flatten a list using only the forms in “The Little Schemer”

谁说我不能喝 提交于 2019-12-27 12:27:12
问题 I'm going through The LIttle Schemer to learn Scheme (as an old C programmer) and as an exercise I tried to write a procedure to flatten a list using only the forms in The Little Schemer; I.e., define , lambda , cond , car , cdr , and , or , etc., but not append . I thought it would be easy but I haven't been able to come up with a solution. How can I do this ? 回答1: I have a version that uses only "first-principles" operations and is efficient (does not require more than one pass through any

Little Schemer: write function that only supports lists of length ≤ 2

扶醉桌前 提交于 2019-12-22 10:46:39
问题 In the book The little schemer , we find this function that only supports lists with length smaller than or equal to 1 : (((lambda (mk-length) ; A. (mk-length mk-length)) (lambda (mk-length) (lambda (l) (cond ((null? l ) 0) (else (add1 ((mk-length eternity ) (cdr l)))))))) '(1)) I want to study step by step, and want to write the similar function that supports only lists of length smaller than or equal to 2 . Please, don't answer this question by offering code like: (((lambda (mk-length) ; B.

Why all the lambdas in The Little Schemer?

让人想犯罪 __ 提交于 2019-12-20 09:54:16
问题 After learning a bit of Scheme from SICP, I started reading The Little Schemer (which I find quite entertaining) and am about one fourth done. I noticed that I can write many (most? all?) solutions without using lambda whereas The Little Schemer always uses them. For example, the very first definition is (define atom? (lambda (x) (and (not (pair? x)) (not (null? x))))) which, unless I am mistaken, can be written more simply as (define (atom? x) (and (not (pair? x)) (not (null? x)))) Am I

The Little Schemer 4th Edition: rember function discussion

我的未来我决定 提交于 2019-12-10 15:52:31
问题 On page 41 after simplification of the rember function, there is a question-respond that I don't understand very well. Q: So why don't we simplify right away? R: Because then a function's structure does not coincide with its argument's structure. I have tried to figure it out for a couple of days, but I don't understand what exactly means that question-respond. Could anyone explain me what Friedman want to show with that question-respond? Thanks in advance 回答1: Up until this point in the

What is the definition of “natural recursion”?

我的梦境 提交于 2019-12-10 12:43:24
问题 The Third Commandment of The Little Schemer states: When building a list, describe the first typical element, and then cons it onto the natural recursion. What is the exact definition of "natural recursion"? The reason why I am asking is because I am taking a class on programming language principles by Daniel Friedman and the following code is not considered "naturally recursive": (define (plus x y) (if (zero? y) x (plus (add1 x) (sub1 y)))) However, the following code is considered

Little Schemer: why wrap (mk-length mk-length) into a function?

Deadly 提交于 2019-12-10 10:47:55
问题 In The Little Schemer book, in Chapter 9, while building a length function for arbitrary long input, the following is suggested (on pages 170-171), that in the following code snippet (from page 168 itself): ((lambda (mk-length) (mk-length mk-length)) (lambda (mk-length) ((lambda (length) (lambda (l) (cond ((null? l) 0) (else (add1 (length (cdr l))))))) (mk-length mk-length)))) the part (mk-length mk-length) , will never return and will be infinitely applying itself to itself: Because we just