scheme

EVAL in SCHEME

若如初见. 提交于 2019-12-06 03:44:25
问题 Peter Norvig in PAIP says: " in modern lisps...eval is used less often (in fact, in Scheme there is no eval at all)" " if you find yourself using eval, you are probably doing the wrong thing". What are some of the ways to circumvent using eval in scheme? Arent there case where eval is absolutely necessary? 回答1: There are cases where eval is necessary, but they always involve advanced programs that do things like dynamically loading some code (eg, a servlet in a web server). As for a way to

List to string conversion in Racket

浪尽此生 提交于 2019-12-06 03:31:02
问题 How do I convert a list into a string in DrRacket? For example, how do I convert '(red yellow blue green) into "red yellow blue green"? I tried using list->string but that seems to work only for characters. 回答1: The trick here is mapping over the list of symbols received as input, converting each one in turn to a string, taking care of adding a white space in-between each one except the last. Something like this: (define (slist->string slst) (cond ((empty? slst) "") ((empty? (rest slst))

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

强颜欢笑 提交于 2019-12-06 02:49:55
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. (mk-length mk-length)) (lambda (mk-length) (lambda (l) (cond ((null? l) 0 ) (else (add1((mk-length mk

Implicit currying in Scheme with syntax-rules?

前提是你 提交于 2019-12-06 02:29:54
问题 Jeffrey Meunier has an implicit Curry macro here, which uses defmacro. I was wondering if someone has ever written this with syntax-rules? 回答1: There are a number of curry implementations for Scheme -- none can be as elegant as Haskell, since there functions are always unary functions, so everything can be curried. (But this can of course be implemented in a sufficiently powerful Scheme like Racket.) As for the macro that you've dug up -- it's a pretty bad one: not only does it use an

Execute command line from Scheme (Guile)

六眼飞鱼酱① 提交于 2019-12-06 01:51:04
问题 The question is described on the title, basically I'd like to execute a command line from scheme, let's say 'ls' and obtaining the output. So my questions are: Is it possible? How? Thanks a lot in advance! By the way I use Guille. 回答1: You need one of these system and system* . Example: (system "ls") From the documentation: Guile Reference — Scheme Procedure: system [cmd] — C Function: scm_system (cmd) Execute cmd using the operating system's “command processor”. Under Unix this is usually

Little Schemer: length0 and mk-length

筅森魡賤 提交于 2019-12-06 01:49:32
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-length) (mk-length mk-length)) (lambda (length) (lambda (l) (cond ((null? l) 0) (else (add1 (length (cdr l)

In R6RS Scheme, is there a way to get the current environment for use with eval?

a 夏天 提交于 2019-12-06 01:27:25
问题 Is there any way in R6RS Scheme to obtain the current environment and then pass it as the second argument to eval ? For example, what should the question marks be for the following expression to return 9? (let ((x 4) (y 5)) (eval '(+ x y) ???)) 回答1: No, there is no such thing in R6RS. Some rare implementations might support something like that, but in the overwhelming majority (including eval in other languages!) this cannot be done. The reason for that is simple: it breaks compilation, since

Scheme let statement

落爺英雄遲暮 提交于 2019-12-06 01:09:03
问题 In scheme which is a functional programming language, there is no assignment statement. But in a let statement (let ((x 2)) (+ x 3)) You are assigning 2 to x , so why doesn't this violate the principle that there is no assignment statements in functional programming? 回答1: The statement "Scheme which is a functional programming language" is incorrect. In Scheme, a functional-programming style is encouraged, but not forced. In fact, you can use set! (an assignment statement!) for modifying the

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

我只是一个虾纸丫 提交于 2019-12-06 01:04:33
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) (+ x y)) (set! x 2) (f 3) ; evaluates to 5 instead of 4 Allowing such mutation is excellent . It allows

curry in scheme

被刻印的时光 ゝ 提交于 2019-12-06 01:04:16
问题 I have this curry function: (define curry (lambda (f) (lambda (a) (lambda (b) (f a b))))) I think it's like (define curry (f a b)) . my assignment is to write a function consElem2All using curry ,which should work like (((consElem2All cons) 'b) '((1) (2 3) (4))) >((b 1) (b 2 3) (b 4)) I have wrote this function in a regular way: (define (consElem2All0 x lst) (map (lambda (elem) (cons x elem)) lst)) but still don't know how to transform it with curry . Can anyone help me? thanks in advance