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.
Related
I would like to use DrRacket in the same way that it works for some of the ‘legacy languages’. In particular, I would like to go through a file as if it were a sequence of commands issued to the interpreter, and not as a module.
Essentially I want to run at least one file in load-mode, but I’m not sure if it’s possible to do it using DrRacket.
Ideally, I could:
*Specify a file that sets the language and maybe loads some modules, which runs by default at startup.
*Then load a file that is not a module (and has no #lang specification) and run it.
It’d also be nice (since I want to use Scheme) if it would allow redefinitions, just as the legacy languages do.
Yes you can, and in fact, the 'legacy languages' (and 'teaching languages') are actually just implemented as DrRacket Plugins. You can remove them from your copy of DrRacket and even add new ones.
There are various ways to do this depending on if you are okay with a #lang (or #reader) saved in the file. If you're not, its still doable, you just need to use drracket:get/extend:extend-unit-frame to add your tool to DrRacket, and possibly drracket:get/extend:extend-definitions-text to easily extend the definitions window.
I won't go into the details of making a generic DrRacket plugin here, that belongs in a different question...also the DrRacket Plugins Manual has the information you need.1 I will, however, point you in the direction of how you can use DrRacket in load mode out of the box.
Check out the racket/load language. It is designed to run each expression in the top level as if you were at a REPL typing it. I find it very useful for testing the differences between Racket module and top level interactions.
Of course, if you don't make a DrRacket plugin, you will still need to put:
#lang racket/load
at the top of your file, but you otherwise get a 'legacy mode' out of the box.
1If it doesn't please continue to ask questions, and of course we always love help from anyone who is willing to contribute. <3
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.
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'm trying to use drracket to work thru the exercises in "How To
Design Programs 2nd Ed".
A number of the exercises in this build up on the answers to previous
questions, so I would like to include the source files from the
answered questions so that I don't have to copy and paste the the body
of the old answer each time.
My main question is: How do I do this?
I have looked thru the documentation and found a method called
include that seems to do what I want, but I cant work out how to use
it correctly.
eg - I have two files:
test.rkt - this compiles and runs fine and contains one function:
(define (test) 1)
(test)
newtest.rkt - I would like this file to to be able to use the function defined in test.rkt.
(require racket/include)
(include "test.rkt")
(define (newtest) (* test 2))
When I try to compile this I get the following error:
module: this function is not defined
(Not very informative, but that's all the information I'm given...)
How do I get this first file to include without getting this error? Is include
even the right function for this, or is my approach completely wrong?
The include form isn't working because when the language is set to "Beginning Student" or one of the other teaching languages, DrRacket actually wraps your program in a module. You can see this if you open "test.rkt" in a regular text editor. The #reader.... bit is what generates the module. But when that gets included into the other file, it doesn't make sense. Thus the error complaining about module.
Unfortunately, as far as I can tell, the HtDP languages still don't have provide, which is what you need to make this work right.
If you really want to get this working, here's a way to hack around it:
Create a new file called "provide.rkt" in the same directory as your other files. While you're editing this file (and only this file), set the Language in DrRacket to "Determine language from source". Put the following two lines in "provide.rkt":
#lang racket
(provide provide)
(That declares a module using the full Racket language that provides only the built-in special form provide.)
Add the following lines to your "test.rkt" program. (Make sure DrRacket's Language is set back to "Beginning Student" or whichever teaching language you're using for this.)
(require "provide.rkt")
(provide test)
Now "test.rkt" is a module that exports your test function. (It was always a module, it just didn't have any exports before, so it wasn't very useful.)
Add the following lines to your "newtest.rkt" program:
(require "test.rkt")
That imports everything provided by "test.rkt": currently just test, but you can add other things to, you just have to provide them.