scheme

Little Schemer: length0 and mk-length

最后都变了- 提交于 2019-12-07 17:42:29
问题 The little schemer gives the following on page 165 as still the function length 0 . But how does this work? It looks like the length lambda is being passed to the mk-length lambda , which evaluates the length lambda with the length lambda itself passed as an argument. So then, when (length (cdr l)) at the bottom is evaluated length is just the length lambda itself. But the length lambda takes two parameters curried: length and l . So how can (length (cdr l)) make sense then? ((lambda (mk

How to install sicp package module in racket?

两盒软妹~` 提交于 2019-12-07 14:04:36
问题 I'm newbie in programming world. I'm using ubuntu OS. I have started my journey with sicp book. I'm working with scheme repl . But suddenly I get stuck with section 2.2.4 I'm not able to execute it's example with scheme repl . I tried to run given example of section, I got an error as given below 1 ]=> (define wave2 (beside wave (flip-vert wave))) ;Unbound variable: wave Even In book, painter is given as primitive procedure. when I ran it, it thrown an error too 1 ]=> painter ;Unbound

recursive function accepts list in scheme

我的梦境 提交于 2019-12-07 13:53:04
问题 I'm new to Scheme and this is my very first Functional language. Implementing almost everything recursively seems to be awkward for me. Nevertheless, was able to implement functions of Factorial and Fibonacci problems having a single integer input. However, what about when your function has an input of a list? Suppose this exercise: FUNCTION: ret10 - extracts and returns as a list all the numbers greater than 10 that are found in a given list, guile> (ret10 ‘(x e (h n) 1 23 12 o)) OUTPUT: (23

Why does Scheme allow mutation to closed environment in a closure?

左心房为你撑大大i 提交于 2019-12-07 12:35:08
问题 The following Scheme code (let ((x 1)) (define (f y) (+ x y)) (set! x 2) (f 3) ) which evaluates to 5 instead of 4. It is surprising considering Scheme promotes static scoping. Allowing subsequent mutation to affect bindings in the closed environment in a closure seems to revert to kinda dynamic scoping. Any specific reason that it is allowed? EDIT: I realized the code above is less obvious to reveal the problem I am concerned. I put another code fragment below: (define x 1) (define (f y) (+

Does Chicken Scheme support complex numbers? If so, why am I getting this error?

一世执手 提交于 2019-12-07 10:40:42
问题 I just started learning a little Scheme, and I'm using Dorai Sitaram's Teach Yourself Scheme in Fixnum Days . In said work it is stated: Scheme numbers can be integers (eg, 42) ... or complex ( 2+3i ). Emphasis mine. Note the form. Using the principles I had been taught so far I tried writing a few different programs that dealt with the different kinds of numbers. I ended up writing this extremely simple snippet to test complex numbers: (begin (display 3+4i) (newline) ) Testing this on

How to Solve N-Queens in Scheme?

倾然丶 夕夏残阳落幕 提交于 2019-12-07 10:36:35
问题 I'm stuck on the extended exercise 28.2 of How to Design Programs. I used a vector of true or false values to represent the board instead of using a list. This is what I've got which doesn't work: #lang Scheme (define-struct posn (i j)) ;takes in a position in i, j form and a board and ; returns a natural number that represents the position in index form ;example for board xxx ; xxx ; xxx ;(0, 1) -> 1 ;(2, 1) -> 7 (define (board-ref a-posn a-board) (+ (* (sqrt (vector-length a-board)) (posn-i

Scheme language: merge two numbers

99封情书 提交于 2019-12-07 09:44:23
问题 How can I merge two integers from a list into one? (in Scheme) Example: '(11 223) -> 11223 回答1: Assuming that the list has exactly two elements, and that both are numbers: (define (merge-numbers lst) (let ((1st (number->string (first lst))) (2nd (number->string (second lst)))) (string->number (string-append 1st 2nd)))) It works as expected: (merge-numbers '(11 223)) > 11223 Alternatively, without using a let : (define (merge-numbers lst) (string->number (string-append (number->string (first

fixed point combinator in lisp

纵饮孤独 提交于 2019-12-07 09:10:57
问题 ;; compute the max of a list of integers (define Y (lambda (w) ((lambda (f) (f f)) (lambda (f) (w (lambda (x) ((f f) x))))))) ((Y (lambda (max) (lambda (l) (cond ((null? l) -1) ((> (car l) (max (cdr l))) (car l)) (else (max (cdr l))))))) '(1 2 3 4 5)) I wish to understand this construction. Can somebody give a clear and simple explanation for this code? For example, supposing that I forget the formula of Y. How can I remember it , and reproduce it long after I work with it ? 回答1: Here's some

scheme/racket: canvas manipulation

心已入冬 提交于 2019-12-07 08:20:32
问题 1) As title says, the objects i draw disappear when i resize the window, but the rectangle stays as is. 2) The origin starts from the top left, but i wish for it to be at the bottom left. 3) I couldn't find any zoom functions, other than in the plot library so if i wish to implement such a thing, one option would to be "zooming" in by drawing bigger objects and refreshing the canvas instead? (define top-frame (new frame% [label "KR"] [width 500] [height 500])) ;Make a frame by instantiating

Y Combinator implementation Scheme

和自甴很熟 提交于 2019-12-07 07:58:33
问题 I am really new to scheme functional programming. I recently came across Y-combinator function in lambda calculus, something like this Y ≡ (λy.(λx.y(xx))(λx.y(xx))) . I wanted to implement it in scheme, i searched alot but i didn't find any implementation which exactly matches the above given structure. Some of them i found are given below: (define Y (lambda (X) ((lambda (procedure) (X (lambda (arg) ((procedure procedure) arg)))) (lambda (procedure) (X (lambda (arg) ((procedure procedure) arg