I'm new to Racket (and Lisp's in general) and I'm wondering if there's a canonical way to detect if a script was run from the command line?
For example, in Python the standard way to do this would be with if __name__ == __main__: as so:
def foo():
"foo!"
if __name__ == "__main__":
foo()
Now, suppose I having the following Racket code, and I'd like respond to be invoked only when this is run as a script.
#lang racket
(require racket/cmdline)
(define hello? (make-parameter #f))
(define goodbye? (make-parameter #f))
(command-line #:program "cmdtest"
#:once-each
[("-H" "--hello") "Add Hello Message" (hello? #t)]
[("-G" "--goodbye") "Add goodbye Message" (goodbye? #t)])
(define (respond)
(printf "~a\n"
(apply string-append
(cond
[(and (hello?) (goodbye?)) '("Hello" " and goodbye.")]
[(and (hello?) (not (goodbye?))) '("Hello." "")]
[(and (not (hello?)) (goodbye?)) '("" "Goodbye.")]
[else '("" "")]))))
Is there an easy/standard way to achieve what I want?
Racket has the concept of main submodules. You can read about them in the Racket Guide section entitled Main and Test Submodules. They do precisely what you want—when a file is run directly using racket or DrRacket, the main submodule is executed. If a file is used by another file using require, the main submodule is not run.
The Racket equivalent of your Python program would be the following:
#lang racket
(define (foo)
"foo!")
(module+ main
(foo))
Related
Im new to scheme. I am trying to import module "sorting" in scheme. I tried everything from (load sorting) to (open sorting), (import sorting). I was able to use
,open sorting
when I am in scheme bash. However I want to import the module to a scheme file. I am using scheme48
You need to use the module language.
More details can be found here: http://community.schemewiki.org/?scheme48-module-system
Basically, instead of writing just a normal scheme file, foo.scm:
;; foo.scm
(define (hello) (display "Hello World!"))
You need to use the module language
;; foo2.scm
;; this is not scheme, it's the module language
(define-structure hello (export hello)
(open scheme)
;; or others here
(begin
;; this is Scheme
(define (hello) (display "Hello World!"))))
You can learn more about the module language here: http://s48.org/1.8/manual/manual-Z-H-5.html
I am using DrRacket and produced a file, hello.scm in emacs with the following content:
#! /usr/bin/env racket
;The first program
(begin
(display "Hello, World!")
(newline))
I then tried to compile the file at the terminal by using le$ racket hello.scm, and received this result:
Le-MacBook-Pro:~le$ racket hello.scm
default-load-handler: expected a `module' declaration, but found
something else
file: /Users/le/hello.scm
context...:
default-load-handler
standard-module-name-resolver
module-path-index-resolve
[repeats 1 more time]
module-declared?
Moreover, when I copy and paste the content of the emacs file into DrRacket and click Run, I receive the following message:
Module Language: only a module expression is allowed, either
#lang <language-name>
or
(module <name> <language> ...)
in: (begin (display "Hello, World!") (newline))
Interactions disabled.
What exactly is the problem?
The problem was solved by adding #lang racket at the top of the emacs file.
Is it possible to debug Maxima CAS Lisp code in Emacs?
It's a pain to use so many print statements all the time.
I've used two approaches over the years.
Run slime using the Maxima core file. See this email for how to do it
http://article.gmane.org/gmane.comp.mathematics.maxima.general/36029
Run Maxima but add code in the initialisation file to create a swank server then connect to that with slime-connect.
http://article.gmane.org/gmane.comp.mathematics.maxima.general/44533
Someone (Leo Butler, maybe?) on the list then suggested a neater approach than what's in that email. Unfortunately, my searching-fu has failed me and I can't find the conversation so I'll just paste what's in my ~/.maxima/swank.lisp nowadays:
(eval-when (:compile-toplevel :load-toplevel :execute)
(defvar *swank-asd*
(car (directory #P"~/.emacs.d/elpa/slime*/swank.asd")))
(when *swank-asd*
(load *swank-asd*)
(require :swank)))
(when (find-package :swank)
(swank:create-server :port 56789 :dont-close t)
;; Hack to make "q" not kill Maxima outright. Only applies from console
(in-package :maxima)
(defvar *real-continue-function* (symbol-function 'continue))
(setf (symbol-function 'continue)
(lambda (&rest args)
(let ((swank::*sldb-quit-restart* 'maxima::macsyma-quit))
(apply *real-continue-function* args))))
(format t "Swank loaded successfully"))
It starts by trying to load up swank from my Emacs directory (I install slime using Elpa). On success, or if swank was loaded anyway for some reason, it creates a server and then does the nifty "make the q key not really annoying" hack described in the second email.
At the moment I am using the REPL-feature of Petite-Chez Scheme. This is working fine for small examples etc.
However, how can I store an entire program in a file ".scm", and then run (interpret) it from the command-line ? I am familiar with the (load "C:/..") command, however this only load definitions from a file into REPL.
How do I run programs using Scheme like programs in C/C++ where I compile and then execute the binary ".exe" ?
Thanks.
Briefly, you just write your program in a file, put #!/usr/bin/scheme --script as the first line of the program, mark it executable, and run it. Here's a sample script that emulates the Unix echo command:
#!/usr/bin/scheme --script
(let ([args (cdr (command-line))])
(unless (null? args)
(let-values ([(newline? args)
(if (equal? (car args) "-n")
(values #f (cdr args))
(values #t args))])
(do ([args args (cdr args)] [sep "" " "])
((null? args))
(printf "~a~a" sep (car args)))
(when newline? (newline)))))
See section 2.6 of Using Chez Scheme for details.
If you want an actual executable there are several implementations that supports compilation to native executable. Racket is one of them and it supports many different scheme versions and dialects (R5RS, R6RS, Racket, ...). There are many more. Chicken (R5RS + SRFIs), Gambit (R5RS + SRFIs) and Bigloo (R5RS, + SRFIs) to name a few.
I have been trying to launch a racket program from the commandline (via 'racket') but have not been having success. According to the documentation (here http://docs.racket-lang.org/reference/running-sa.html#%28part._mz-cmdline%29) passing -f followed by a file should evaluate that file. However, I can't seem to get this to work. As a test, I made the following file:
;test.rkt
#lang racket
(define a 1)
Then, running it in racket (supposedly loading the file) and attempting to recall the value of a:
racket -f test.rkt -i
Welcome to Racket v5.1.1.
> a
reference to undefined identifier: a
My end goal is to be able to launch a different program from a shell script using the --main option combined with loading the definitions with -f to start up execution, just have become a bit baffled since I can't seem to get this trivial bit working.
Removing the #lang line works, but it means that your code is no longer a module, which makes it a pretty bad idea. To start racket on a given module file, all you need is to just run racket on the file, nothing else is needed. For example, put this in test.rkt:
#lang racket/base
(printf "Hi\n")
and just run it with racket test.rkt. If you want to have command-line flags, you can use (current-command-line-arguments) to get a vector of additional command-line arguments, but there's also the racket/cmdline library that makes it much easier to have standard kinds of flag processing. Here's an example for that:
#lang racket/base
(require racket/cmdline)
(define excitedness "")
(define mode "Hi")
(command-line
#:multi
[("-e" "--excited") "add excitedness levels"
(set! excitedness (string-append excitedness "!"))]
#:once-each
[("-b" "--bye") "turn on \"bye\" mode"
(set! mode "Bye")])
(printf "~a~a\n" mode excitedness)
and you can now run it with racket test.rkt <flags>. See also the Racket Guide's section on scripts for making your test.rkt even easier to run.
Finally, there is the --main approach that you've seen -- to use that, your module needs to provide a main function that receives all the command-line flags as arguments. For example:
#lang racket/base
(require racket/string)
(provide main)
(define (main . xs)
(printf "You gave me ~s flags: ~a\n"
(length xs) (string-join xs ", ")))
and to run it:
racket -t /tmp/y -m -- foo bar baz
The flag breakdown is: -t requires your module, -m causes racket to run your main function, and -- means that the following flags are all passed to your program. You can combine the flags like so:
racket -tm- /tmp/y foo bar baz
and that would be something that you'd usually put in your script trampoline as described in that guide section.
And, of course, this is all described in great details in the reference manual.
Remove the #lang racket header from your file:
;test.rkt
(define a 1)