问题
Whenever I try to use srfi/1 functions like fold and reduce in drracket r5rs language I get an null-list? error. After some research I found that it is due to the fact that the function requires and immutable list but gets a mutable one. How do I create immutable list in r5rs or is this srfi/1 not designed with r5rs in mind?
回答1:
This is basically reiterating what John said, but it definitely does merit a full answer status. (John, I'll be happy to delete this if you copy this answer verbatim...)
The thing about Racket's R5RS language is that it sticks very strictly to just the R5RS language, and therefore comes with nothing beyond that. (SRFIs could be implemented for the R5RS mutable pairs too, but nobody ever came up with the adapted code.)
In any case, the bottom line here is that R5RS is basically useful only in those rare cases where you want to inflict on yourself the pain of writing using just the basic r5rs language -- which means that you basically can't do much useful things. So especially if you want to get a feel for the language, you'll likely want to play with all kinds of things that go beyond the narrow r5rs world -- and for that you should use #lang racket
.
It's also better in terms of "just grokking the language", and you can even apply the same lessons to other Scheme implementations baring in mind that they all come with their own extensions. If you use the limited r5rs language, you'll likely to experience the frustration that comes with such a limited language and this can easily overwhelm any positive experience you might have with it otherwise.
回答2:
Since the language "racket" uses immutable cons cells, the srfi/1 implementation that comes with the system also uses immutable cons cells.
Since cons cell in R5RS uses mutable cons cells, you can't use the builtin srfi/1 with the R5RS language in DrRacket.
That's is however a small problem. Download http://srfi.schemers.org/srfi-1/srfi-1-reference.scm and save it in the directory where you have your code.
Open it and insert at the first line:
(define (check-arg . more) #t)
Then in your own code, add the line:
(load "srfi-1-reference.scm")
Now srfi/1 is redefines map (and a few others). Redefined builtin operations is normally done only on accident, so the default settings in DrRacket is to throw an error. Therefore click at the R5RS language at the bottom left in DrRacket. Choose "Choose language...". Then click the button "Show Details". The remove the tick by "Disallow redefinition of initial bindings".
Now the following program runs:
(load "srfi-1-reference.scm")
(fold + 0 '(1 2 3))
and produces 6 as expected.
来源:https://stackoverflow.com/questions/11334697/dr-racket-r5rs-and-srfi