scheme

returns the first n of list

半腔热情 提交于 2019-12-10 17:10:05
问题 How to return the first n elements of a list? Here's what I have: (define returns(lambda (list n) (cond ((null? list) '()) (((!= (0) n) (- n 1)) (car list) (cons (car list) (returns (cdr list) n))) (else '())))) Examples: (returns '(5 4 5 2 1) 2) (5 4) (returns '(5 4 5 2 1) 3) (5 4 5) 回答1: You're asking for the take procedure: (define returns take) (returns '(5 4 5 2 1) 2) => (5 4) (returns '(5 4 5 2 1) 3) => (5 4 5) This looks like homework, so I guess you have to implement it from scratch.

local in racket

一笑奈何 提交于 2019-12-10 16:59:10
问题 I am reading about local definitions in the book, and I came across this example- (local ((define (f x) (+ x 5)) (define (g alon) (cond [(empty? alon) empty] [else (cons (f (first alon)) (g (rest alon)))]))) (g (list 1 2 3))) what exactly does local do here? 回答1: local is documented either in here as part of one of the HtDP languages or in here as part of the local module. Let's see each one in turn. First the one in HtDP: (local [definition ...] expression) Groups related definitions for use

What is the difference between let and let* in Scheme?

纵饮孤独 提交于 2019-12-10 16:13:23
问题 I am writting a script for GIMP and using let* as it was in a sample I took. But it seems to be just a lambda sugar exactly like let . Why are they different? What is the difference between them? 回答1: They are different in the order in which variables are bound. Consider this for example: > (let ((a 1)(b (+ a 2))) b) This code will FAIL because b requires a , which has not been defined before. It is defined, in the same let , but Scheme will take all your let definitions as only one statement

Racket accumulator list function

早过忘川 提交于 2019-12-10 16:11:36
问题 I'm working on a specific step in creating the 2048 game which you may have played. It's on a bunch of websites online. Basically all this function does is: 1) All blank spaces move to the back and 2) if the first two numbers are equal then it doubles and checks every two numbers These are the instructions for the step I'm stuck on: Design a slide-left function so that it runs a single pass over the given row using an APS (accumulator passing style) helper. You will need to maintain two

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

How do I evaluate a symbol returned from a function in Scheme?

南笙酒味 提交于 2019-12-10 15:47:15
问题 I'm refamiliarizing myself with Scheme and I've hit a problem that is probably reflecting a fundamental misunderstanding on my part. Say I do the following in Scheme (using Guile in this case but it's the same in Chicken): > (define x 5) > x 5 > (string->symbol "x") x > (+ 5 (string->symbol "x")) <unnamed port>:45:0: In procedure #<procedure 1b84960 at <current input>:45:0 ()>: <unnamed port>:45:0: In procedure +: Wrong type: x > (symbol? (string->symbol "x")) #t > (+ 5 x) ; here x is

Static analyzer for functional programming languages, e.g.Scheme

我只是一个虾纸丫 提交于 2019-12-10 15:43:12
问题 I seldom see static analyzer for functional programming languages, like Racket/Scheme, I even doubt that whether there are any. I would like to write a static analyzer for functional languages, say Scheme/Racket. How should I go about it? 回答1: First read this paper by Shivers, explaining why there is no static control flow graph available in Scheme. Might implemented k-CFA in Scheme. Matt Might's site and blog is a good starting point for exploring static analysis of higher-order languages. I

Curly brackets {} to replace 'begin' in Racket

拈花ヽ惹草 提交于 2019-12-10 15:23:24
问题 Is it possible to have a macro to use curly brackets {} to indicate a block of statments, so as to replace the 'begin' keyword. Hence, instead of: (if (condition) (begin (statement1) (statement2) (statement3) (statement4)) (else-statement)) we may use: (if (condition) { (statement1) (statement2) (statement3) (statement4) } (else-statement)) How can this be accomplished? Thanks for your answers. 回答1: This is completely possible, and there are several ways to do it. (Quick note before I start,

Writing an auto-memoizer in Scheme. Help with macro and a wrapper

落花浮王杯 提交于 2019-12-10 14:50:00
问题 I am facing a couple of problems while writing an auto-memoizer in Scheme. I have a working memoizer function, which creats a hash table and checks if the value is already computed. If it has been computed before then it returns the value else it calls the function. (define (memoizer fun) (let ((a-table (make-hash))) (λ(n) (define false-if-fail (λ() #f)) (let ((return-val (hash-ref a-table n false-if-fail))) (if return-val return-val (begin (hash-set! a-table n (fun n)) (hash-ref a-table n)))

For-each and map in Scheme

不想你离开。 提交于 2019-12-10 14:38:06
问题 Are there any difference between these 2 functions in scheme?I am using Dr Racket R5RS language to make a simulator game and I could not decide which one is better. 回答1: for-each evaluates the given function on the list elements left-to-right, and discards the return value of the function. It's ideal for doing side-effecting things to each element of the list. map evaluates the given function on the list elements in no specific order (though most implementations will use either right-to-left