Racket language change & module problems - scheme

I'm trying to implement some of SICP graphic programs in Racket, but there are 2 problems:
When I need to use 'let' I can't use beginner language.
When I try to change language, or open new file while using "advanced" language, I get this error:
module: identifier already imported from a different source
error when I try to load image module by (require 2htdp/image).
What's going on? Also, are there better ways to train with images in Scheme?

It's not clear why you want to use 2htdp/image in the first place. A much more useful package to use would be Neil Van Dyke's SICP Support page, it provides a language with support for the book and includes the graphical language. That should be enough to solve both of your problems.

As Óscar mentions, you are better off with using #lang planet neil/sicp, However, if you want to import somethng that exports identical symbols you can prefix them:
(require (prefix-in hi: 2htdp/image))
Then all exported from that have prefix hi:, eg. (hi:circle 30 "outline" "red"). The colon isn't anything special. The prefix could have been xxx and it would be xxxcircle.
Also, you can only import the symbols you want:
; you only want circle and eclipse
(require (only-in 2htdp/image circle ellipse))
Or you can import everything except some symbols:
; everything except circle and ellipse
(require (except-in 2htdp/image circle))
And there is no reason not using racket or racket/base as language when you know this.

Related

Racket | CLI with specific language

I'm trying to use the racket CLI to run a file but I need a specific language (pretty-big) features to be available and I'm struggling with that.
As far as I know, if I run $ racket <file_name> then it requires the file as a module. In order to do that, I need to add #lang <some_lang> at the top of my file, but I just can't find any equiv to #lang pretty-big. Any idea how to ask for pretty-big this way?
The other option, is to run the code in load-mode (from the racket CLI manual) but on this case I'm getting no results back which obviously isn't good.
Any suggestions?
I know the solution to this will be super easy, just can't find it.
A search of the Racket docs for "pretty big" turned up this result, which says that the module lang/plt-pretty-big corresponds to DrRacket's "Pretty Big" legacy language.
Unfortunately, putting
#lang lang/plt-pretty-big
at the top of your file won't work, because that module doesn't specify a reader. But you can tell it to use the usual S-expression reader by using the s-exp "meta-language" as follows:
#lang s-exp lang/plt-pretty-big
If you want Racket to print out the results of top-level expressions in your file, it looks like the lang/plt-pretty-big language still won't do it, and I don't think there's an easy fix. Consider porting the program to #lang racket, which has that behavior. Or add println around the things you want printed.

How to use an image in R5RS

I'm looking for a way to import a .jpg file into R5RS. I would like to use it as background for a game that I'm making.
Thanks!
Many Scheme implementations have support for image files. The R5RS standard, as written, does not. I suggest using Racket, where you can use, e.g.,
(require 2htdp/image)
(bitmap/file "/path/to/foo.jpg")
In fact, there are many parts of building a game that are not going to fit well under the R5RS umbrella. I'm afraid you're almost certainly going to have to pick an implementation, and use features that lie outside of the standard.
I use the graphics/graphics library.
Put it in your file by using this (in racket):
(require graphics/graphics)
This is the usage:
((draw-pixmap viewport) file p [color]) → void?
viewport : viewport?
file : path-string?
p : posn?
color : (or/c (integer-in 0 299)
string?
rgb?)
You can find more help in the Racket documentation

racket/base namespace

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.

Cannot load File in Scheme, (using Simply Scheme Book and PLT Scheme)

I am using PLT Scheme (DrScheme). I want to load a file that I got from here. To load the file, I go into PLT Scheme and in the interactions window (or the bottom window), I type (load "simply.scm") and then press enter. It gives me this error:
simply.scm:20:12: set!: cannot mutate module-required identifier in: number->string
I have no clue how to fix that, please assist...
Extra Info: I am learning out of the book Simply Scheme Introducing Computer Science by Brian Harvey and Matthew Wright
Also, the link takes a little long to load, but it does work, i think they have the files on a really old server, so that may be why.
Open DrScheme (or DrRacket as the newer version of the software is now called); from the Language menu select "Choose Language..." and make sure "Use the language declared in the source" is checked.
Then at the top of your file, put the following two header lines followed by whatever code you want from the book (I've chosen an example from the first chapter):
#lang racket
(require (planet dyoo/simply-scheme))
(define (pigl wd)
(if (member? (first wd) 'aeiou)
(word wd 'ay)
(pigl (word (butfirst wd) (first wd)))))
Then click run. This should allow you to also type expressions in the Interactions pane to evaluate them.
Alternatively, you can replace the two lines above with one:
#lang planet dyoo/simply-scheme
But then the Simply Scheme language is not enabled in the Interactions pane.
You can find the documentation for this DrScheme/Racket simply-scheme library by clicking on the 'docs' link at the URL provided by Chris.
You should use the Racket Simply Scheme module. The file you have linked to is not compatible with Racket.
More specifically, in Racket, you're not allowed to use set! to overwrite existing function bindings, which is what that file does. (Technically, it can potentially break other Scheme implementations also, so this isn't a "Racket quirk" or anything.)

What Scheme Does Ghuloum Use?

I'm trying to work my way through Compilers: Backend to Frontend (and Back to Front Again) by Abdulaziz Ghuloum. It seems abbreviated from what one would expect in a full course/seminar, so I'm trying to fill in the pieces myself.
For instance, I have tried to use his testing framework in the R5RS flavor of DrScheme, but it doesn't seem to like the macro stuff:
src/ghuloum/tests/tests-driver.scm:6:4: read: illegal use of open square bracket
I've read his intro paper on the course, An Incremental Approach to Compiler Construction, which gives a great overview of the techniques used, and mentions a couple of Schemes with features one might want to implement for 'extra credit', but he doesn't mention the Scheme he uses in the course.
Update
I'm still digging into the original question (investigating options such as Petit Scheme suggested by Eli below), but found an interesting link relating to Gholoum's work, so I am including it here.
[Ikarus Scheme](http://en.wikipedia.org/wiki/Ikarus_(Scheme_implementation)) is the actual implementation of Ghuloum's ideas, and appears to have been part of his Ph.D. work. It's supposed to be one of the first implementations of R6RS. I'm trying to install Ikarus now, but the configure script doesn't want to recognize my system's install of libgmp.so, so my problems are still unresolved.
Example
The following example seems to work in PLT 2.4.2 running in DrEd using the Pretty Big
(require lang/plt-pretty-big)
(load "/Users/donaldwakefield/ghuloum/tests/tests-driver.scm")
(load "/Users/donaldwakefield/ghuloum/tests/tests-1.1-req.scm")
(define (emit-program x)
(unless (integer? x) (error "---"))
(emit " .text")
(emit " .globl scheme_entry")
(emit " .type scheme_entry, #function")
(emit "scheme_entry:")
(emit " movl $~s, %eax" x)
(emit " ret")
)
Attempting to replace the require directive with #lang scheme results in the error message
foo.scm:7:3: expand: unbound identifier in module in: emit
which appears to be due to a failure to load tests-driver.scm. Attempting to use #lang r6rs disables the REPL, which I'd really like to use, so I'm going to try to continue with Pretty Big.
My thanks to Eli Barzilay for his patient help.
The language he uses is most likely Chez Scheme. Regardless, the R5RS language in PLT is a pretty strict version of R5RS, with extensions like square brackets throwing errors -- and you may get more mileage using the default #lang scheme language. (Or, if that fails, try and see if you can work with Petit -- the free version of Chez.)
You can see setup instructions for running it here on Ubuntu x86.
The installation download for Petite Scheme are here.

Resources