Geiser and Racket variable have been defined previously and cannot be redefined - emacs

I'm trying to use geiser-mode in emacs to run racket code.
I've been able to install geiser-mode and launched racket.
Yet when I run a definition twice I got the following error.
this name was defined previously and cannot be re-defined
here is simple example
(define a (* 1 4))
a
run twice
In the debugger
#a: this name was defined previously and cannot be re-defined
#in: a

racket appears to behave differently in a file and the REPL. This file will throw an error:
#lang racket
(define a 5)
(define a 6)
And this REPL session will not:
> (define a 5)
> a
5
> (define a 6)
> a
6
The behavior is because of the way modules work. When working in a file, there is an implicit module. Once the symbol a has been defined in that module another symbol with the same name cannot be defined within that module. The REPL simply expands forms without all the ceremony of modules.

Related

How do I make a modified version of a Racket #lang? (adding / removing a few definitions)

I'd like to take an existing language, say, htdp/isl, and add a few definitions that aren't included by default. I can (require blah) at the top of every file, but I want to just be able to write #lang my-modified-isl at the top, and get those definitions along with the rest of the language. It's okay if this only works for languages that are "close" to Racket (i.e., have a boring reader).
In Racket, a #lang is basically just any module that provides #%module-begin (and optionally a reader/parser). As an example, you can check out my SML package (no relation to Standard ML), which is almost vanilla Racket, but a few custom tweaks to make it really good for describing data (like YAML).
Let's say you want to make a version of vanilla Racket, but that includes the function standard-fish, and lacks a divide (/) function. You could make your file:
#lang racket ; custom-racket.rkt
(require pict)
(provide (except-out (all-from-out racket) /)
standard-fish)
And now you can use s-exp to put your new language in the #lang line:
#lang s-exp "custom-racket.rkt"
(standard-fish) ; A fish
(/ 10 2) ; Error, `/` undefined
(Note that if you want to get rid of S-Expressions entirely and replace the reader, you would use #lang reader instead.)
Finally, you can package your file into a custom Racket package to use directly. Rename custom-racket.rkt from above to custom-racket/main.rkt, add an info.rkt file, and install the package:
$ mkdir custom-racket
$ cd custom-racket
$ vim main.rkt
#lang racket ; custom-racket.rkt
(require pict)
(provide (except-out (all-from-out racket) /)
standard-fish)
(module reader syntax/module-reader
custom-lang)
$ vim info.rkt
#lang info
(define collection "custom-racket")
$ raco pkg install
And now you can use custom-racket directly in the #lang line:
#lang custom-racket
(standard-fish) ; A fish
(/ 10 2) ; Error, `/` undefined

read-syntax function for postfix interpreter in racket

I write a read-syntax function but ı get this error. my read-syntax function is this:
(define (read-syntax path port)
(for([line (port->lines port)])
(parse-line line)))
The error is this:
Module Language: only a module expression is allowed, either
#lang <language-name>
or
(module <name> <language> ...)
Each racket file is a module.
The file must start with defining the module.
The easy starting way is to have
#lang racket
as the first line of your module.
Description of this is in the Racket Guide https://docs.racket-lang.org/guide/intro.html

display executing name of the current running script

I have tried a few methods, one of them being:
(define (program) (find-system-path 'pref-file))
I have read from the documentation (after attempting the above code) and have noticed it is not what i'd need to use, obviously :) Any ideas?
Would also like to save this information to a variables. VBS example:
script = WScript.ScriptFullName
#lang racket
(display "Program name: ")
(displayln (find-system-path 'run-file))
Output (if saved as "test.rkt" and run with the command racket test.rkt):
Program name: test.rkt
From the racket console REPL, (find-system-path 'run-file) will return #<path:racket>. I didn't try it in DrRacket.

Pass Expression as arguments to Clojure REPL

I have been learning Clojure and lately I have been using the REPL as a comand line calculator, my 'workflow' would be greatly improved if it were possible to pass arguments to the Clojure REPL and get the output, does anyone know how to do that?
Clarification: For example I would like to execute lein "(+ 2 2)" and have it return 4
~  lein "(+ 2 2)"
'(+ 2 2)' is not a task. See 'lein help'.
grenchman creates a repl, and each command line invocation gets a result from that repl, this is likely what you want.
lein (Leiningen) is the wrong tool for this, other than starting up a REPL. If you really want a command line interface to some Clojure program, that's possible too, but requires you to compile it to a jar and execute it, cf. this article on building CLI clojure apps.
Anything you def is available at the REPL.
=> (def ten 10)
...
=> (defn fact [n] (apply * (range 1 (inc n))))
...
=> (fact ten)
3628800
=>
# as bash variable
{ echo "$clj-expressions"; cat - ; } | lein repl
# as file
{ cat ./script.clj; cat - ; } | lein repl
Lucky for us, lein repl is just a plain-old unix process
The idea here is to send your commands to the repl's stdin but ensure that the current terminal's stdin is connected afterwards.
Thanks to Jonathan Leffler for this one. His answer here solved this.
To collect output, you can always spit something out as part of the script you run.
This is exactly how REPL works - you write some expression and press Enter, got expression result back.
→ lein repl
nREPL server started on port 59650 on host 127.0.0.1
REPL-y 0.3.0
Clojure 1.5.1
Docs: (doc function-name-here)
(find-doc "part-of-name-here")
Source: (source function-name-here)
Javadoc: (javadoc java-object-or-class-here)
Exit: Control+D or (exit) or (quit)
Results: Stored in vars *1, *2, *3, an exception in *e
user=> (+ 42 42)
84
user=>

Racket: execute file and stay in interactive mode

Is there a way from a command line to run Racket file and stay in the interactive mode afterwards?
E.g. same in Python it would be:
python -i <file.py>
Assuming a foo.rkt that's this:
#lang racket
(provide x)
(define x 42)
(define y 4242)
Then you can use -i to specify interactive mode (= REPL), together with -t to require the file:
$ racket -it foo.rkt
Welcome to Racket vX.X.X.
> x
42
> y
y: undefined; ...
> (exit)
Note that y is not bound since it's in the module and not provided out. More likely you want a REPL that is "inside" the foo module, which can be done using enter! to go into the module's namespace, either in the REPL:
$ racket
> (enter! "foo.rkt")
> x
42
> y
4242
> (exit)
or on the command-line, using -e (and also -i to request a REPL):
$ racket -i -e '(enter! "foo.rkt")'
Welcome to Racket vX.X.X.
> x
42
> (+ x 12)
54
> (exit)
xrepl
If you do this a lot, you'll probably like xrepl. In your ~/.racketrc simply add:
(require xrepl)
Now the example becomes:
$ racket
Welcome to Racket vX.X.X.
-> ,en foo.rkt
42
"foo.rkt"> x
42
"foo.rkt"> (+ x 12)
54
"foo.rkt"> ,ex
Aside from ,en, XREPL has a bunch of goodness -- like the prompt indication of the module you're currently in, as well as a bunch of other useful commands:
$ racket
Welcome to Racket vX.X.X.
-> ,h
; Available commands:
; help (h ?): display available commands
; exit (quit ex): exit racket
; cd: change the current directory
; pwd: display the current directory
; shell (sh ls cp mv rm md rd git svn): run a shell command
; edit (e): edit files in your $EDITOR
; drracket (dr drr): edit files in DrRacket
; apropos (ap): look for a binding
; describe (desc id): describe a (bound) identifier
; doc: browse the racket documentation
; require (req r): require a module
; require-reloadable (reqr rr): require a module, make it reloadable
; enter (en): require a module and go into its namespace
; toplevel (top): go back to the toplevel
; load (ld): load a file
; backtrace (bt): see a backtrace of the last exception
; time: time an expression
; trace (tr): trace a function
; untrace (untr): untrace a function
; errortrace (errt inst): errortrace instrumentation control
; profile (prof): profiler control
; execution-counts: execution counts
; coverage (cover): coverage information via a sandbox
; switch-namespace (switch): switch to a different repl namespace
; syntax (stx st): set syntax object to inspect, and control it
; check-requires (ckreq): check the `require's of a module
; log: control log output
; install!: install xrepl in your Racket init file
Emacs
However if you're an Emacs user you might prefer using something like:
Geiser
Quack minor mode for scheme-mode
racket-mode (shameless self-promotion)
If you are using Visual Studio Code as an editor, you may want to use the "Code Runner extension"
make sure it's installed from the vs code marketplace
then enter Preferences: Open Settings (JSON) and past the following:
"code-runner.executorMap": {
"racket": "(exit); racket -i -e '(enter! \"$fileName\")'",
},
You will be able to run directly your file by clicking the Run Code icon or by pressing Ctrl+Alt+N
NB: the same manouvre goes for "scheme" since it's interpreted by racket as well, however putting #lang racket in the top of your file is necessary
It can be done with
racket -if <file.rkt>
However it will not work as expected if the file starts with
#lang racket