How to run a compiled racket program from windows - racket

The program is run within drRacket by quoting the main funnction and passing any parameters. How do I do this from the command line or clicking on the .exe file? The main function is as follows:
(define (worm-main ct)
(big-bang (make-wormState (init-worm INIT-WORM-SEGS START-POSN) "r" (food-create (make-posn 99999 99999)))
[to-draw render]
[on-key navigate]
[on-tick move ct]
[stop-when end-chk? final-scene]))
I call worm-main like this :- (worm-main 0.2)

From what you wrote, it seems you created the executable from the “Create Executable” item in DrRacket’s “Racket” menu. You are calling the main definition from the interaction area by typing (worm-main 0.2).
Your executable isn't working because you haven't added the call to the main function in the definitions window. Indeed, the executable just has the file that's in the before creating the executable.
You must must add the call (worm-main 0.2) to the definitions window before creating the executable.

I don't think you can call a function within a file from terminal. Alternatively, in your program, you can add input statements and call the main function with these parameters.
You can run a racket program from terminal by changing the language to
#lang racket
For example:
#lang racket
(+ 3 4)
You can run that from command line using (name of file is test.rkt):
racket test.rkt
Also, you can create an executable from Dr.Racket by going to Racket, Create Executable...
Edit: Try copy pasting this and create an executable.
#lang racket/gui
(require racket/gui/base)
(define frame (new frame% [label "Example"]))
; Make a static text message in the frame
(define msg (new message% [parent frame]
[label "No events so far..."]))
; Make a button in the frame
(new button% [parent frame]
[label "Click Me"]
; Callback procedure for a button click:
[callback (lambda (button event)
(send msg set-label "Button click"))])
; Show the frame by calling its show method
(send frame show #t)
; source: https://docs.racket-lang.org/gui/windowing-overview.html#%28part._.Creating_.Windows%29
I believe to see the effects of an executable, your program must create a new window.
Edit2:
Try compiling the racket program from the command line as explained here: https://docs.racket-lang.org/guide/cmdline-tools.html
i.e.
raco make name-of-file.rkt
After that run:
cd compiled
racket name-of-file.zo

Related

Why does (require (for-syntax 'm)) execute expressions in the required module 3 times in DrRacket?

I don't know how to explain the following behavior in DrRacket's interactions window. I think the output should be only one "hello", since the module m should be instantiated once in this case. But actually "hello" is printed 3 times.
> (module m racket
(printf "hello\n"))
> (module n racket
(require (for-syntax 'm)))
hello
hello
hello
>
The same behavior also exists if I write this example in DrRacket's definitions window and run it.
#lang racket
(module m racket
(printf "hello\n"))
(module n racket
(require (for-syntax (submod ".." m))))
hello
hello
hello
>
This example is a simplified version of one from Racket's guide (https://docs.racket-lang.org/guide/macro-module.html) -- the first example in 16.3.2. The behavior shown in that document
is intuitive (printing the string one time), but when I execute that code in DrRacket's interactions window, it also prints the string 3 times.
I'm using DrRacket version 7.7.
See also https://github.com/greghendershott/racket-mode/issues/379 and https://github.com/racket/drracket/issues/278.
The latter in particular indicates that one of the extra ones is from errortrace instrumentation, which is on by default in DrRacket.

How to call a racket procedure from terminal by giving command line argumands

I have a racket file called foo.rkt Inside that file I have a procedure called textify and it takes 2 parameters. Now how can I call this procedure from terminal ? What I want to do is simply:
> racket foo.rkt myfirstarg mysecondarg
and then I want this call to activate (textify myfirstarg mysecondarg) procedure. Is this possible ?
Here is the content of foo.rkt:
#lang racket
(require wxme)
(provide
(contract-out
[textify (-> path-string? path-string? void?)]))
(define (textify in out)
(call-with-input-file in
(λ (in-port)
(call-with-output-file out
(λ (out-port)
(copy-port (wxme-port->text-port in-port) out-port))
#:exists 'truncate))))
You can simply do this as the last expression in your file:
(apply textify (vector->list (current-command-line-arguments)))
If you are making a more advanced program that has switches you can use command-line that does this for you.

Getting EOF after calling Racket function with-out-to-string

I can't figure out why the below code would first print "eof" (On the interactions window). I am using DrRacket.
(find-first-path (car
(regexp-split #px"\n"
(with-output-to-string
(λ () (system "find /usr/lib/ -name
libpython2.7.so"))))))
The system library function starts a shell process that takes its input
from (current-input-port). In DrRacket, this causes the creation of a (short-
lived) subwindow of the interactions window into which you can type input that
you want to be treated as input to the subprocess.
So, for instance, try running this program
#lang racket
(system "cat")
Each line that you type into the input window will wind up being piped to the
cat process. When you're finished, though, you need a way to send an EOF to
the subprocess. This is what the "EOF" button on the right does.
If you don't want to see this window, you can create an empty input stream
to your subprocess, e.g.:
(parameterize ([current-input-port (open-input-string "")])
(system "find /usr/lib/ -name libpython2.7.so"))

compile large project within emacs

A large project may have directory with several level depth. Emacs's default compile command is "make -k", if I modified a certain source code, then typed "M-x compile RET RET", it will execute "make -k" under the directory which the source code lies.
I think I can write a function to determine if the Makefile exist under current directory, if yes, keep searching under the parent directory until find the top level directory, then execute the building command, it would be right like my expectation.
However, I'm not very clearly how to start, could anyone give me some hints to start? Like the function or variable I may encounter. Thanks.
You can try to use something like:
(setq compile-command
'(let ((mf (locate-dominating-file default-directory "Makefile")))
(if mf (setq mf (file-name-directory mf)))
(concat (if (and mf (not (equal mf default-directory)))
(format "cd %s; "
(shell-quote-argument
(file-relative-name
(directory-file-name mf)))))
"make -k ")))
There is a smarter-compile in marmalade.
From the documentation....When you require it, you can specify a list of cons cells, each one like (TEST . COMMAND).
COMMAND is used for the compile-command when
the TEST succeeds.
TEST can be:
a string. In this case it is used as a regex,
and matched against the filename associated to the
buffer. The TEST succeeds when the regex matches.
a symbol, representing the major-mode. In this case
if the buffer uses that major mode, the TEST
succeeds.
a symbol, representing any function with a name not
ending in \"-mode\". In this case, the function is
called and if it returns non-nil, the TEST
succeeds.
a list of forms. In this case the forms are eval'd,
and if the return value is non-nil, the TEST
succeeds.
So you could produce a function that does the scan for makefile in parent directories,
and use that as your TEST.
According to the documentation, if the COMMAND is nil, then the package uses the result of the TEST as the compile command. Which means you would need only one function, returning a make command referencing the makefile in the appropriate directory.
Take a look at: http://emacswiki.org/emacs/CompileCommand
"C-h v compile-command" directly from emacs.
Here's a solution for people who prefer bash scripting over Emacs Lisp. In my .emacs I define a command which saves all buffers and runs a bash script which compiles the project.
(defun save-all-and-compile () (interactive)
(save-some-buffers 1)
(shell-command "make-and-run.sh &"))
(add-hook 'c-mode-common-hook (lambda ()
(local-set-key (kbd "<f5>") 'save-all-and-compile)))
For small projects the script could be as simple as
#!/bin/bash
make -j && ./<main>
where 'main' is the name of your executable. For larger projects one would first need to locate the root directory. Similarly, you could have different scripts (bound for different keys) for building and running the program. And then some more scripts for testing different parts of the project. But these are just details one can figure out for themselves.
Make sure the script is run asynchronously using '&'. This way the Async Shell Command buffer will open with the output from make and your project and will stay open.
EDIT
Based on the discussion below it appears I have initially overthought it and the solution is quite simple. Instead of passing the usual 'make' or 'make -k' to compile-command one could use a shell script which first navigates to the project's root directory and the builds.
(setq compile-command "script.sh")

Automatically Reload a File in LISP When a Command Is Entered

I'm learning LISP for a class. I have a basic workflow setup in Ubuntu with my LISP file in VIM and an interactive LISP prompt in a terminal that I'm using to test code as I write it. Is there a way to get LISP to load a specific file every time I type a command? It's getting a bit tiring having to constantly input (load 'initial-code.cl) (yes, even when I am using the terminal's history).
Can always try:
(let (fn)
(defun l (&optional filename)
(if filename
(setf fn filename))
(load fn)))
Works like this:
[2]> (l "x.lisp")
;; Loading file x.lisp ...
;; Loaded file x.lisp
T
[3]> (l)
;; Loading file x.lisp ...
;; Loaded file x.lisp
T
[4]>
Pretty simple.
You can also do something like:
(defun go ()
(load "project.lisp")
(yourfunc 'your 'parameters))
Then you just type (go) and it reloads your file and calls your main entry point.
Or even combine them:
(defun gogo (&rest args)
(l) ;; call (l "file.lisp") first to initialize it
(apply #'yourfunc args))
then you can change your parameters easily
(gogo 1 2)
(gogo 2 4)
Ya know, it's lisp. Don't like something, change it.
With more time, you can write a simple wrapper that can build these on the fly. But you get the idea.
Most Lisp programmers would encourage you to use SLIME.
If you like Eclipse, there is also a Lisp plugin.
I know this doesn't really answer your question, but at least you can be aware of some alternatives.
You can try slimv, it is like slime for vim.