How to run (interpret) a Scheme program stored in a file? - scheme

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.

Related

How to load an executable in Scheme using the command line

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.

Debugging Maxima CAS Lisp code on Emacs

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.

Detecting if script executed from command line in Racket?

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))

Receiving Flymake error in Windows & Ubuntu Emacs for PHP

I am receiving this error.
Error (flymake): Flymake: Failed to launch syntax check process 'php' with args (-f test_flymake.php -l): Searching for program: no such file or directory, php. Flymake will be switched OFF
I am on windows 7 with emacs 24 GNU Emacs 24.1.1 (i386-mingw-nt6.1.7601)
There is an article which highlights this error but it is referring to checking /etc in linux however I am on windows. http://sachachua.com
This is the currently relevant part of my .emacs, what can I do to get it working.
(add-to-list 'load-path "C:/Users/renshaw family/AppData/Roaming/.emacs.d/elpa/flymake-0.4.11")
(require 'flymake)
(global-set-key [f3] 'flymake-display-err-menu-for-current-line)
(global-set-key [f4] 'flymake-goto-next-error)
;;(require 'flymake-php)
(require 'zencoding-mode)
(add-hook 'sgml-mode-hook 'zencoding-mode) ;; Auto-start on any markup modes
(defun flymake-php-init ()
"Use php to check the syntax of the current file."
(let* ((temp (flymake-init-create-temp-buffer-copy 'flymake-create-temp-inplace))
(local (file-relative-name temp (file-name-directory buffer-file-name))))
(list "php" (list "-f" local "-l"))))
(add-to-list 'flymake-err-line-patterns
'("\\(Parse\\|Fatal\\) error: +\\(.*?\\) in \\(.*?\\) on line \\([0-9]+\\)$" 3 4 nil 2))
(add-to-list 'flymake-allowed-file-name-masks '("\\.php$" flymake-php-init))
;; Drupal-type extensions
(add-to-list 'flymake-allowed-file-name-masks '("\\.module$" flymake-php-init))
(add-to-list 'flymake-allowed-file-name-masks '("\\.install$" flymake-php-init))
(add-to-list 'flymake-allowed-file-name-masks '("\\.inc$" flymake-php-init))
(add-to-list 'flymake-allowed-file-name-masks '("\\.engine$" flymake-php-init))
(add-hook 'php-mode-hook (lambda () (flymake-mode 1)))
(define-key php-mode-map '[M-S-up] 'flymake-goto-prev-error)
(define-key php-mode-map '[M-S-down] 'flymake-goto-next-error)
Edit:
I have now tried this in ubuntu 12.04 as well and receive the same error.
It looks like flymake can't find the PHP interpreter. Try adding an explicit hint about where to find PHP.
One way you could do this is by adding the following defun to your .emacs:
(defun my-flymake-get-php-cmdline (source base-dir)
"Gets the command line invocation needed for running a flymake
session in a PHP buffer. This gets called by flymake itself."
(list "C:\Path\to\PHP\php.exe"
;; Or the path to the PHP CLI executable on the Ubuntu machine you mentioned.
(list "-f" source "-l")))
Then modifying the flymate-php-init you're using now into this:
(defun flymake-php-init ()
"Use php to check the syntax of the current file."
(let* (
(temp
(flymake-init-create-temp-buffer-copy 'flymake-create-temp-inplace))
(local
(file-relative-name temp (file-name-directory buffer-file-name))))
(get-cmdline-f 'my-flymake-get-php-cmdline)))
This advice is heavily based on this answer from someone else using flymake on Windows for PHP. You might also find that answer helpful, since their situation is similar to yours.

How do you load a file into racket via command line?

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)

Resources