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)
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.
I'm learning Common Lisp (Clozure CL) on the Mac and installed quicklisp, with the help of a generous contributor on here. The 'ltk' library works when running (ltk::ltk-eyes) or (ltk:ltktest).
Running (ql:quickload "ltk") seems to work as it return the following:
Load 1 ASDF system:
ltk
; Loading "ltk"
I have a problem running the following code taken from the 'ltk' documentation. Here's the script:
(ql:quickload "ltk") ;my addition to the script
(defun hello-1()
(with-ltk ()
(let ((b (make-instance 'button
:master nil
:text "Press Me"
:command (lambda ()
(format t "Hello World!~&")))))
(pack b))))
Howver, when I run (hello-1) I get this:
Error: Class named BUTTON not found.
While executing: FIND-CLASS, in process Listener(4).
Type cmd-/ to continue, cmd-. to abort, cmd-\ for a list of available restarts.
If continued: Try finding the class again
Type :? for other options.
My guess is that the 'ltk' library is not properly accessed in the function definition? I tried to fix the problem by using ltk:with-ltk as it seems to be a ltk function.
(defun hello-1()
(ltk:with-ltk ()
(let ((b (make-instance 'button
:master nil
:text "Press Me"
:command (lambda ()
(format t "Hello World!~&")))))
(pack b))))
But that produced the following error. It seems that I'm getting closer at fixing it since the 2D canvas also appeared with the GUI alerting me of the error.
Thanks for your help.
Common Lisp manipulates symbols, which belong to packages. The Lisp reader is responsible for resolving an unqualified reference to a symbol to the actual, qualified symbol. That depends on the current package being bound to *PACKAGE* when reading code. As suggested in comments, you should read §21. Programming in the Large: Packages and Symbols from P. Seibel's Practical Common Lisp.
You can define your own package as follows:
(defpackage :test-ltk
(:use :cl :ltk))
The :use clause is the declarative equivalent of USE-PACKAGE. The above makes the test-ltk package inherit all external symbols from the Common Lisp and LTK packages. Generally, you cannot using too many packages together because you are more likely to have conflicts: two symbols belonging to different packages but having the same name cannot be accessed in an unqualified way. This is a bit like in C++, where you are discouraged from doing using namespace std.
In order to selectively import some symbols and not others, you use :import-from instead. For example, you could define the preceding package as follows:
(defpackage :test-ltk
(:use :cl)
(:import-from :ltk #:with-ltk #:button #:pack))
Here, you only list the 3 symbols you are actually accessing. The #:symbol notation represents uninterned symbols, i.e. symbols that belongs to no package and are used only for their names. You could have used strings (in uppercase) instead.
Then, you change the current package with IN-PACKAGE. The unqualified access to symbols are resolved according to the current package's definitions:
(in-package :test-ltk)
(defun hello-1 ()
(with-ltk ()
(pack
(make-instance 'button
:master nil
:text "Press Me"
:command (lambda () (format t "Hello World!~&"))))))
Solution (thanks #jkiiski for referring to the : operator) : After carefully reading the error messages in the console I was able to resolve the problem. The compiler was not able to access the 'ltk' functions (with-ltk ...) and (pack ...) as well as the button class.
The corrected code is below (using Quicklisp to use the ltk library):
(load #P"/Users/myDirectory/quicklisp/setup.lisp")
(ql:quickload "ltk")
(defun hello-1()
(ltk:with-ltk ()
(let ((b (make-instance 'ltk:button
:master nil
:text "Press Me"
:command (lambda ()
(format t "Hello World!~&")))))
(ltk:pack b))))
My next step is to see if I can find a similar method to 'using namespace' in C++ so that I don't need to keep using let: , and simplify the code.
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.
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))