I'm beginner in SICP. I'm reading section 2.2.4, A Picture Language. I'm stuck with it's example. I didn't understand, How to run these examples?
As given in book, wave is supposed to be a primitive procedure. But When I tried to execute it, REPL throw an error as given below
1 ]=> wave
;Unbound variable: wave
Similarly when I tried to execute first example of this section, REPL throw an error
1 ]=> (define wave2 (beside wave (flip-vert wave)))
;Unbound variable: wave
I didn't understand, what's going wrong? Any help would be appreciated. Thanks.
In DrRacket IDE there is a SICP compatibility language. Using DrRacket helps format code, identify errors and it has a debugger. It also supports standard Scheme as well as it's own dialect, racket, which is very similar but has lots of libraries like most modern languages.
To install (after you have installed DrRacket)
From terminal run:
raco pkg install sicp
You get lots of output and perhaps some WARNINGS. Restart DrRacket and replace #lang racket with #lang sicp. To get the picture language add (#%require sicp-pict), thus this example should work nicely:
#lang sicp
(#%require sicp-pict)
(paint-hires (below (beside diagonal-shading
(rotate90 diagonal-shading))
(beside (rotate270 diagonal-shading)
(rotate180 diagonal-shading))))
This is a short version of an answer to Which lang packet is proper for SICP in Dr.Racket?
Related
I was trying to follow how to do scheme and sicp from Which lang packet is proper for SICP in Dr.Racket?
but when I run code in the accepted answer
#lang sicp
(paint-highres (below (beside diagonal-shading
(rotate90 diagonal-shading))
(beside (rotate270 diagonal-shading)
(rotate180 diagonal-shading))))
I get error
paint-hires: unbound identifier in: paint-hires
I have installed the sicp package.
Anyone know what the problem is?
The paint-hires function is a left-over form the original MIT Scheme implementation. Back then it the "high resolution" was too slow to use, while experimenting - so paint-hires was used to get a "final" image.
When the original MIT Scheme implementation of the SICP Picture Language was ported to PLT Scheme paint-hires was kept.
Recently (within a year or two) the SICP Picture Language was reimplemented on modern Racket. This gives you the ability to use the Picture language with a resolution of your choice, colors! (the original MIT Scheme was used on monochrome displays) and more.
Make a copy of: "main.rkt" and einstein2.jpg" and save them in the same folder.
Open "main.rkt" in DrRacket and run it.
Look at the bottom for examples.
Add your own program at the bottom of "main.rkt".
Look through the files for how to use colors etc.
Both files are here:
https://github.com/sicp-lang/sicp/tree/master/sicp-pict
#sorawee-porncharoenwase thank you for the docs link. #soegaard thanks for the context for the recent changes to DrRacket.
What finally worked for me was this
#lang sicp
(#%require sicp-pict)
(paint (below (beside diagonal-shading
(rotate90 diagonal-shading))
(beside (rotate270 diagonal-shading)
(rotate180 diagonal-shading))))
I think the docs incorrectly say to use paint-hires.
I'm starting to read the Little Schemer and now instead of PLT Scheme we have Racket. I would like to know if Racket is suitable for doing the exercises in the book or do I need to get another true Scheme compiler. Before I forgot to tell you, my OS is Windows x64.
The book, language and paradigm is complex enough, I would love to avoid struggling with a compiler.
DrRacket is the (r)evolution of DrScheme; DrRacket will work perfectly for the exercises in "The Little Schemer". Just don't forget to:
In the Language dialog, choose "Use the language declared in the source"
Write #lang racket at the top of each file you create
Implement the atom? predicate in each file as explained at the very beginning of the book
If you're going to re-implement an existing procedure, do so in a separate tab or window, because trying to rewrite a procedure in the edit window will result in a duplicate definition for identifier error. If necessary, use several files for saving the procedure definitions
You really just need the atom? function. What’s described in the book's preface is essentially:
(define (atom? x)
(and (not (pair? x)) (not (null? x))))
As mentioned, it should satisfy this test:
(atom? '()) ;=> #f
Note that there is also a definition in Racklog that will not satisfy that test.
A more detailed discussion on atom? is here.
Despite the book's suggestion to implement add1 and sub1, Racket does already provide them.
BTW, I use Vim for editing Racket, but a few other editors are capable.
I have had the author of the book as a professor. He now uses Racket himself, but he uses Emacs as a text editor.
Racket/Scheme are interchangeable. You should be able to answer any exercise with it. Good luck.
Also, I recommend downloading Dr. Racket as your interpreter.
(define read-eval-print (sllgen:make-rep-loop "--> "
(lambda (pgm) (eval-program pgm))
(sllgen:make-stream-parser
the-lexical-spec
the-grammar)))
Is anyone familiar with this type of error?
Help please.
You need to use the right language. According to the documentation, it looks like you need to start your source file with #lang eopl:
#lang eopl
(define read-eval-print
(sllgen:make-rep-loop
"--> "
(lambda (pgm)
(eval-program pgm))
(sllgen:make-stream-parser
the-lexical-spec
the-grammar)))
Once you do this, of course, you'll start getting some other errors, e.g., that eval-program isn't defined. If you define a dummy one, e.g.,
(define (eval-program pgm)
(values))
You'll then get an error that
sllgen:make-stream-parser: bad scanner specification in: the-lexical-spec
You'll need to replace that lexical specification with an actual specification, and similarly (I expect, though I didn't explore this far) with the-grammar. The documentation linked above might (or might not) help you out with those. It looks like you can get the code for the book that this is based on, Essentials of Programming Languages, from the book's website.
What really looks the most promising, though, are results like
Environment-Passing Interpreters, Programming Language Essentials, 2nd edition, Appendix: The SLLGEN Parsing System (PDF)
SLLGEN tutorial (PDF)
sllgen source (sllgen.scm) (a somewhat older version)
other sources (I found the above just by searching with Google for sslgen)
I'm starting to read the Little Schemer and now instead of PLT Scheme we have Racket. I would like to know if Racket is suitable for doing the exercises in the book or do I need to get another true Scheme compiler. Before I forgot to tell you, my OS is Windows x64.
The book, language and paradigm is complex enough, I would love to avoid struggling with a compiler.
DrRacket is the (r)evolution of DrScheme; DrRacket will work perfectly for the exercises in "The Little Schemer". Just don't forget to:
In the Language dialog, choose "Use the language declared in the source"
Write #lang racket at the top of each file you create
Implement the atom? predicate in each file as explained at the very beginning of the book
If you're going to re-implement an existing procedure, do so in a separate tab or window, because trying to rewrite a procedure in the edit window will result in a duplicate definition for identifier error. If necessary, use several files for saving the procedure definitions
You really just need the atom? function. What’s described in the book's preface is essentially:
(define (atom? x)
(and (not (pair? x)) (not (null? x))))
As mentioned, it should satisfy this test:
(atom? '()) ;=> #f
Note that there is also a definition in Racklog that will not satisfy that test.
A more detailed discussion on atom? is here.
Despite the book's suggestion to implement add1 and sub1, Racket does already provide them.
BTW, I use Vim for editing Racket, but a few other editors are capable.
I have had the author of the book as a professor. He now uses Racket himself, but he uses Emacs as a text editor.
Racket/Scheme are interchangeable. You should be able to answer any exercise with it. Good luck.
Also, I recommend downloading Dr. Racket as your interpreter.
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