scheme

How do I get a definition's name as a symbol?

余生长醉 提交于 2021-02-19 08:18:09
问题 How do I get a definition's name as a symbol in Scheme and/or Racket? Suppose I have these definitions: (define bananas 123) (define multiply *) (define (pythagoras a b) (sqrt (+ (* a a) (* b b)))) How can I define def->symbol where: (def->symbol bananas) returns 'bananas (def->symbol multiply) returns 'multiply (def->symbol pythagoras) returns 'pythagoras Is this a case where I have no choice but to learn and use these advanced things called "macros"? 回答1: Just as you suspect, we need to use

Is there a way to see the body of a lambda in Racket?

谁说胖子不能爱 提交于 2021-02-19 04:46:14
问题 Say I have this code: #lang racket (define a ((λ (x) x) ((λ (y) y) (λ (z) ((λ (w) w) z))))) I know intuitively that this lambda expression is (extensionally) equal to (λ (z) z) My question is if there is a way to print out the body of a in case I want to see how much the function got simplified internally by Racket. More information: By default, if I type a into the interpreter, I get #<procedure:y> (This seems to give a hint as to how much evaluation happened). I can change the output style

DrRacket, R5RS and the error procedure

不想你离开。 提交于 2021-02-19 03:50:05
问题 I love DrRacket IDE, but currently I'm building a pet project where I would like to be independent from it, meaning i'm commited to use only R5RS standard procedures. The thing is, in DrRacket there's this procedure called "error" which i would like to continue using but I can't find it in the Standards. What i would like to know is if there's a way to emulate that "error" procedure using only the Standards procedures so that the code is portable between different implementations of Scheme. I

for/continue in scheme/lisp

旧街凉风 提交于 2021-02-19 01:16:57
问题 I'm writing a small interpreter for a C-like language in Scheme (R5RS) and trying to convert something like: for (i = 0; i < 100; i++) { if (isprime(i)) continue; else /* do something with i */ } to valid Scheme (the isprime function is just an example and not important). However, after trying for some time, I have not been able to find an efficient/simple way to add the equivalent of a continue statement to a do loop in Scheme. What would be even better would be a "for" macro which allows

for/continue in scheme/lisp

扶醉桌前 提交于 2021-02-19 01:14:03
问题 I'm writing a small interpreter for a C-like language in Scheme (R5RS) and trying to convert something like: for (i = 0; i < 100; i++) { if (isprime(i)) continue; else /* do something with i */ } to valid Scheme (the isprime function is just an example and not important). However, after trying for some time, I have not been able to find an efficient/simple way to add the equivalent of a continue statement to a do loop in Scheme. What would be even better would be a "for" macro which allows

How do I create a pipe between two processes in Guile?

蓝咒 提交于 2021-02-18 18:52:02
问题 I want to create two process in Guile and send the output (stdout) from one of them as input (stdin) to the other. Using the following example, how can this be done? echo "foo bar" | wc Output: 1 2 8 回答1: Yes, you can do this using open-output-pipe : (let ((p (open-output-pipe "wc"))) (display "The quick brown fox jumps over the lazy dog.\n" p) (close-pipe p)) This is equivalent to echo "The quick brown fox jumps over the the lazy dog." | wc (including echo 's implicit newline because I'm

How do I create a pipe between two processes in Guile?

倾然丶 夕夏残阳落幕 提交于 2021-02-18 18:51:09
问题 I want to create two process in Guile and send the output (stdout) from one of them as input (stdin) to the other. Using the following example, how can this be done? echo "foo bar" | wc Output: 1 2 8 回答1: Yes, you can do this using open-output-pipe : (let ((p (open-output-pipe "wc"))) (display "The quick brown fox jumps over the lazy dog.\n" p) (close-pipe p)) This is equivalent to echo "The quick brown fox jumps over the the lazy dog." | wc (including echo 's implicit newline because I'm

Scheme pass-by-reference

血红的双手。 提交于 2021-02-17 21:51:10
问题 How can I pass a variable by reference in scheme? An example of the functionality I want: (define foo (lambda (&x) (set! x 5))) (define y 2) (foo y) (display y) ;outputs: 5 Also, is there a way to return by reference? 回答1: See http://community.schemewiki.org/?scheme-faq-language question "Is there a way to emulate call-by-reference?". In general I think that fights against scheme's functional nature so probably there is a better way to structure the program to make it more scheme-like. 回答2:

Missing argument in syntax-rules Hygienic macro call from Scheme R5RS example

不羁岁月 提交于 2021-02-11 17:00:20
问题 I have one more questions about Hygienic macros in Scheme, consider example from R5RS (let-syntax ((when (syntax-rules () ((when test stmt1 stmt2 ...) (if test (begin stmt1 stmt2 ...)))))) (let ((if #t)) (when if (set! if 'now)) if)) Why it match if the pattern have 3 arguments and ellipsis that can match empty list? It's called with 2 arguments if and (set! if 'now) . What should be ... bind to, if stmt2 can be bind to empty list? This is kind of non Lispy if ... is just nothing. Is that

Missing argument in syntax-rules Hygienic macro call from Scheme R5RS example

此生再无相见时 提交于 2021-02-11 16:59:50
问题 I have one more questions about Hygienic macros in Scheme, consider example from R5RS (let-syntax ((when (syntax-rules () ((when test stmt1 stmt2 ...) (if test (begin stmt1 stmt2 ...)))))) (let ((if #t)) (when if (set! if 'now)) if)) Why it match if the pattern have 3 arguments and ellipsis that can match empty list? It's called with 2 arguments if and (set! if 'now) . What should be ... bind to, if stmt2 can be bind to empty list? This is kind of non Lispy if ... is just nothing. Is that