Background: I am new to scheme, and am using DrScheme to write my programs.
The following program outputs 12345 when I run the program as r5rs:
12345
However the following program outputs nothing (it's an r6rs program):
#!r6rs
(import (rnrs))
12345
That being said, I can get it to output 12345 by doing this:
#!r6rs
(import (rnrs))
(display 1235)
Is that something new with r6rs, where output only occurs when specifically specified using display? Or am I just doing something else wrong
This is a subtle issue that you're seeing here. In PLT, the preferred mode of operation is to write code in a module, where each module has a specification of the language it is written it. Usually, the default language is #lang scheme (and #! is short for #lang). In this language, the behavior is for all toplevel non-definition expressions to display their values (unless they're void -- as in the result of most side-effects). But the #lang r5rs and #lang r6rs don't do the same -- so these toplevel expressions are evaluated but never displayed.
The reason you did see some output with the R5RS language is that you didn't use it as a "module" (as in #lang r5rs), but instead used the specific R5RS "language level". This language level is more compatible to the R5RS, but for various subtle reasons this is not a good idea in general. Using #lang is therefore generally better, and if you want to save yourself some additional redundant headaches, it'll be easier if you stick with #lang scheme for now, and worry about standards later. (But YMMV, of course.)
Related
I am using Drracket for my project. I am using the language SICP(#lang sicp) for my project. For one of my implementations i require the use of scheme package called (require (planet dyoo/simply-scheme)). Scheme is not recognising require keyword. But if i change my language to #lang racket "require" is recognised as an keyword. My project makes use of set-car! and set-cdr! which is available in sicp language and not in racket. Is there any work around.
Below is some excerpts of my code:
#lang sicp
(require (planet dyoo/simply-scheme))
(define crosscuts 0)
and
(define (move-north steps_to_move)
(set-cdr! current_position ( + (y_coord current_position) steps_to_move)))
First of all, here are things that I want to note:
It's generally better to use #lang racket because #lang sicp is very limited. You can still access set-car! (though it's named set-mcar!). See the documentation at https://docs.racket-lang.org/reference/mpairs.html.
In case you decide to use #lang racket, do you actually need to use mutable data structure? Avoiding mutation is a defining trait of functional programming. And if you really do need mutable data structure, can you use struct with #:mutable instead? It's more Racket-y. See the documentation at https://docs.racket-lang.org/reference/define-struct.html.
This might not be possible, but generally I would avoid using Planet package. Planet is an old package system that is no longer recommended. It looks like there's no simply-scheme in the new package system, however, so you might really need to use Planet here.
If you really want to use #lang sicp, note the margin note at https://docs.racket-lang.org/sicp-manual/SICP_Language.html
R5RS has no require to avoid breaking programs that use the name require. #%require is therefore used instead.
#%require is a much more primitive operation, however. Depending on your require spec, it might or might not be easy to switch from require to #%require.
So first, try to replace require with #%require and see if it works. If it is, great, you are done.
But if it errors, then it means your require spec can't be processed by #%require. One possible solution is to use #%require to get require from Racket, so that you can use require to do more complex things.
(#%require (only racket require))
(require ....)
In Common Lisp, when I want to use different pieces of code depending on Common Lisp implementations, I can use *features* and the provided notation of #+ and #- to check the availability of a given feature and proceed accordingly. So for example (taken from Peter Seibel's PCL):
(defun foo ()
#+allegro (do-one-thing)
#+sbcl (do-another-thing)
#+clisp (something-else)
#+cmu (yet-another-version)
#-(or allegro sbcl clisp cmu) (error "Not implemented"))
Is anyone aware of a similar mechanism for Scheme? There are sometimes subtle differences between different implementations of Scheme, which, when you're trying to be portable, would be nice to abstract away. One such case that comes to my mind is Racket not providing mutable pairs by default. Instead of writing e.g. (set-cdr! lst '(1 2 3)) you would have to use set-mcdr! and only after you ran (require racket/mpair). Of course, such things could be abstracted by functions and/or macros, but I think the Common Lisp approach is neat in this aspect.
The closest thing there is, is cond-expand (aka SRFI 0), which is available on some Schemes but not others (Racket, for example, doesn't have it, and your code won't compile if you try to use it). For those Schemes that do have it, it looks like a cond form, except you test for booleans that tell you things about the compiler/interpreter. On some Schemes you can detect which Scheme you're running on, while on others you can only check for SRFIs:
(cond-expand (chicken
'bok-bok-bok!)
((and guile srfi-3432)
'this-guile-is-full-of-SRFI!)
(else
'(might be MIT Scheme, whose cond-expand only tests for SRFIs)))
I am new to scheme and I am trying to trace a function. I have to load the "trace" function. According to the racket documentation, I have to execute a:
(require racket/trace)
But the response I am getting back is
require: undefined;
cannot reference undefined identifier
I am baffled. I am using language "R5RS" if that makes a difference. can't find anything online or on stack overflow on this.
Try this:
(#%require racket/trace)
Chris Jester-Young's comment is correct: don't use R5RS. In the standard R5RS language, there's no such thing as a module. Go look at http://www.schemers.org/Documents/Standards/R5RS/; not a word about a module, right?
Racket takes the standard more seriously than you'd expect: if you tell it to work in R5RS mode, it will turn off language features that the standard does not describe.
If you're using the Racket toolchain, don't use the R5RS language unless you really want to work in a restrictive language. Use standard #lang racket instead. See: http://docs.racket-lang.org/guide/intro.html which shows how to use it in that mode.
Anyone know what is included in the racket/base language. I am looking to compare the racket/base namespace definitions with the R7RS draft to get a first hand idea of how divergent Racket is from Scheme.
The difference is going to be huge, just like any other practical Scheme implementation. To give you a rough idea, I see more than 1300 identifiers from racket/base, and racket goes up to over 2000. Also, note that comparing the names is not going to be the whole story -- there are additional differences, like the fact that Racket uses immutable pairs by default, and like the fact that you get guaranteed left-to-right evaluation order.
A quick way to get the lists yourself for the former would be to make sure that XREPL is installed (have your ~/.racketrc file contain (require xrepl)), then start Racket with racket/base as the REPL language, and use the ,ap (apropos) command with no arguments so it shows you all of the bound names:
$ racket -I racket/base
Welcome to Racket v5.2.1.
-> ,ap
To read about the names, you can use the ,doc command, or just use the search box in the docs.
According to The Racket Reference:
Unless otherwise noted, the bindings defined in this manual are exported by the racket/base and racket languages.
Unfortunately that is not terribly helpful since it is a large reference manual. But, since you are comparing R7RS to Racket, it may be useful to just browse through the whole reference to get an idea of what is available.
I've got a bunch of "legacy" Guile Scheme code that I want to get running in the Racket Scheme IDE. There appear to be enough differences to make this a non-trivial exercise. (My level of Scheme knowledge is the level to complete the The Little Schemer).
My question is:
What are the differences between Guile Scheme and Standard Scheme (in the Racket IDE)?
In light of these differences, in general, what are the steps I'll need to take to convert some Guile Scheme Code to standard Scheme?
Additional: (happy with divergence between Racket Scheme and R5RS/R6RS) - what I want is to get 'something' to run in the Racket IDE - rather than the Racket language.
If by "Standard Scheme (in the Racket IDE)," you mean the Racket language, i.e., what you get when you prefix your code with #lang racket, then the top four differences to look out for are:
a different module system
a different macro system (depending on how old your code is)
immutable cons-cells (unless you import mutable ones)
no one-armed ifs in Racket (use when)
To port code from Guile to Racket, find out which files are "at the bottom" of your dependencies, i.e., find the files that do not depend on other files.
Open such a file in Racket, add the line #lang racket at the top, and try to run it.
You will most likely encounter some "unbound identifier" errors.
If you are lucky, the function is present in Racket, but not included in the "racket" language. Search for the name in the Racket documentation, and if you find it, then use (require ...) to import the name into your program.
Then run the program again to find the next error.
Some function are named differently in Guile and Racket, so look up the name in the Guile documentation and see what it does. Then open the Racket documentation on the same subject, and see what it is called in Racket.
In some cases you may have to make bigger changes. If you can't find some piece of functionality in the Racket documentation, then try asking the mailing list. It could be that it simply has a different name, or that somebody implemented it and put it on PLaneT (thus it will no appear in the documentation until you have installed the package).
Example of importing srfi/1 into the R5RS language.
#lang r5rs
(#%require srfi/1)
(xcons 1 2)
Differences from R4RS code to modern Scheme?
One thing to look out for is that in R4RS the empty list '() counted as false, not it is interpreted as true.
See this question for more things to look out for:
Running SICP Pattern Matching Rule Based Substitution Code
See also this list of changes from the R5RS standard:
List of changes from R4RS to R5RS