r5rs

ANTLR grammar for scheme R5RS

跟風遠走 提交于 2019-12-10 11:25:17
问题 I'm beginner in ANTLR and I'm learning it by an example. I use C as my target language. The example is a Scheme R5RS grammar file taken from this question, with a little modification(rename the grammar name and add some options with the grammar specification untouched). antlr generated the lexer and parser, and I compile it with a test main() in which I just do some initialization and simply call the parser. When runing the test program with a piece of scheme code, the parser detect some

Scheme, SICP, R5RS, why is delay not a special form?

三世轮回 提交于 2019-12-09 10:29:03
问题 This is concerning chapter 3.5 from SICP, in which streams are being discussed. The idea is that: (cons-stream 1 (display 'hey)) Should not evaluate the second part of the cons-stream, so it should not print “hey”. This does happen, I get the following output: hey(1 . #< promise >) So my conclusion is that delay is not implemented as a special form? Or am I doing something wrong? I use the following implementation: (define (cons-stream a b) (cons a (delay b))) With delay being the default

How can I convert a string into exact number in Scheme Lisp?

那年仲夏 提交于 2019-12-08 06:44:48
问题 For example, I have this string: "6119726089.12814713" If I do (string->number "6119726089.12814713") - using the SISC implementation the result is 6.119726089128147e9 - and in Guile implementation is 6119726089.128147 but I would like an exact number, like: 611972608912814713/100000000 without loss precision. I'd like a function like (string->exact) or something like this. NOTE: please fix my non-native English and remove this message. Thanks. 回答1: Use (string->number "#e6119726089.12814713"

Multiple lines comments in Scheme (RnRS)

独自空忆成欢 提交于 2019-12-06 05:31:45
问题 I created this solution: ; use like this: ; (/* content ... */ <default-return>) ; or ; (/* content ... */) => #f (define-syntax /* (syntax-rules (*/) ((/* body ... */) #f) ((/* body ... */ r) r))) But is it really the best or easiest way? 回答1: You can't do it this way -- it won't work for a number of contexts. Here are some examples that won't work: (+ (/* foo */) 1 2) (define (foo a (/* b */) c) ...) (/* foo; bar */) (/*x*/) (let ((x (/* 1 */) 2)) ...) (let ((/* (x 1) */) (x 2)) ...) (car '

ANTLR grammar for scheme R5RS

你说的曾经没有我的故事 提交于 2019-12-06 05:09:31
I'm beginner in ANTLR and I'm learning it by an example. I use C as my target language. The example is a Scheme R5RS grammar file taken from this question , with a little modification(rename the grammar name and add some options with the grammar specification untouched). antlr generated the lexer and parser, and I compile it with a test main() in which I just do some initialization and simply call the parser. When runing the test program with a piece of scheme code, the parser detect some syntax error(which should not happen!) main function in test.c #include <stdio.h> #include "r5rsLexer.h"

Extensible macro definitions

…衆ロ難τιáo~ 提交于 2019-12-05 21:06:17
问题 Inspired by a comment thread on a related question regarding functions instead of macros. Is there any way to extend a Scheme syntax definition so that it can use the previous definition of the syntax in the new definition? Furthermore, this must be extensible, that is, it must be possible to chain the technique together several times. For example, say we want to extend lambda so that every time a function defined with lambda is called, it prints "foo" before executing the function body. We

Is it possible to “extend” a function / lambda / macro in Scheme?

风格不统一 提交于 2019-12-05 11:34:11
For example: if I want the function equal? recognize my own type or record, can I add a new behavior of equal? ? without erasing or overwriting the old one? Or for example if I want to make the function "+" accept also string? Rather than using import , a better solution is to keep track of the original function by let -binding it. It's also better to check that the type of the argument is a string, rather than that it is not a number. Using both of these approaches means that it's possible to compose the technique. (define + (let ((old+ +)) (lambda args (if (string? (car args)) (apply string

Extensible macro definitions

↘锁芯ラ 提交于 2019-12-04 03:26:48
Inspired by a comment thread on a related question regarding functions instead of macros. Is there any way to extend a Scheme syntax definition so that it can use the previous definition of the syntax in the new definition? Furthermore, this must be extensible, that is, it must be possible to chain the technique together several times. For example, say we want to extend lambda so that every time a function defined with lambda is called, it prints "foo" before executing the function body. We can do this in the following way: (define-syntax old-lambda lambda) (define-syntax lambda (syntax-rules

Scheme, SICP, R5RS, why is delay not a special form?

我与影子孤独终老i 提交于 2019-12-03 12:55:35
This is concerning chapter 3.5 from SICP, in which streams are being discussed. The idea is that: (cons-stream 1 (display 'hey)) Should not evaluate the second part of the cons-stream, so it should not print “hey”. This does happen, I get the following output: hey(1 . #< promise >) So my conclusion is that delay is not implemented as a special form? Or am I doing something wrong? I use the following implementation: (define (cons-stream a b) (cons a (delay b))) With delay being the default R5RS implementation. Is this a fault in the implementation, or am I not doing or understanding it right?

Scheme number to list

孤街浪徒 提交于 2019-12-02 01:52:27
I need a subroutine for my program written in scheme that takes an integer, say 34109, and puts it into a list with elements 3, 4, 1, 0, 9. The integer can be any length. Does anyone have a trick for this? I've thought about using modulo for every place, but I don't think it should be that complicated. The simplest way I can think of, is by using arithmetic operations and a named let for implementing a tail-recursion: (define (number->list num) (let loop ((num num) (acc '())) (if (< num 10) (cons num acc) (loop (quotient num 10) (cons (remainder num 10) acc))))) Alternatively, you can solve