How to load an executable in Scheme using the command line - scheme

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.

Related

How to resolve `file missing` error when autoloading in Emacs?

I'm running into this error when trying to load an auto generated autoload.el file using loaddefs-generate function. The following is how I did
;;; core-autoload.el -*- lexical-binding: t; -*-
(defvar generated-autoload-file nil
"This is neccessary, otherwise raise error.
`Defining as dynamic an already lexical var`.")
(defvar chidori-autoload-file
(expand-file-name "autoload.el" chidori-cache-dir)
"Autoload file.")
(defun chidori-autoload/generate-define (loaddef &rest DIRS)
"LOADDEF DIRS."
(let ((generated-autoload-file loaddef))
(when (or (not (file-exists-p generated-autoload-file))
noninteractive)
(loaddefs-generate DIRS chidori-autoload-file))))
(defun chidori-autoload/reload ()
"Generate autoload file from `core/autoload'."
(interactive)
(when (file-exists-p chidori-autoload-file)
(delete-file chidori-autoload-file t)
(message "delete old autoload file: %s" chidori-autoload-file))
(chidori-autoload/generate-define chidori-autoload-file chidori-autoload-dir)
(load chidori-autoload-file nil 'nomessage)
(message "generate autoload file: %s done." chidori-autoload-file))
(unless (file-exists-p chidori-autoload-file)
(chidori-autoload/reload))
(load chidori-autoload-file nil 'nomessage)
(provide 'core-autoload)
;; core-autoload.el ends here
And this is part of generated autoload file
;;; autoload.el --- automatically extracted autoloads (do not edit) -*- lexical-binding: t -*-
;; Generated by the `loaddefs-generate' function.
;; This file is part of GNU Emacs.
;;; Code:
;;; Generated autoloads from ../../autoload/package.el
(autoload 'package! "../../autoload/package")
;;; and many other autoloads
However, when I start Emacs, the error occurred saying it (file-missing "Cannot open load file" "No such file or directory" "../../autoload/package"
If I manually edit to absolute path, the issue is gone. But I don't think it's right way to do.
Can anyone help me out?
My draft for further reference:
https://git.sr.ht/~bangedorrunt/chidori-emacs
The following is my system information:
OS: macOS 12.6
Emacs versions: emacs-plus#29
Terminal: Wezterm
Shell: fish
I finally figured out the issue.
In my config, I got
(defvar chidori-etc-dir (expand-file-name "etc/" user-emacs-directory))
(defvar chidori-cache-dir (expand-file-name "cache/" chidori-etc-dir))
The issue is fixed if I replace chidori-etc-dir with the following
(defvar chidori-cache-dir (expand-file-name "cache/" user-emacs-directory))
From my understanding, we couldn't 2 level nested dir for autoload file. This also happen if I write autoload file to user-emacs-directory.

How to debug? emacs lisp program behavior does not achieve the desired but the executing the same as command does?

In a test buffer in emacs with Spacemacs, I have the following (the problem is also described in the buffer content):
* Top Heading
** A subheading created by executing commands
M-x org-insert-heading
M-x org-do-demote
Below I tried to achieve the same with an elisp function:
*** A subheading created by executing an interactive elisp function still works
It also works. Here is the program:
(defun org-insert-subheading-relative ()
"Replacement of org-insert-subheading, as it requires to provide an argument,
which is not convenient to program."
(interactive)
(org-insert-heading)
(org-do-demote)
)
However, having the code segment below,
the (org-do-demote) would not work, in
the following code segment:
(progn
(goto-end-of-code-block)
(insert "\n")
(org-insert-heading)
(org-do-demote)
(insert block)
(hide-subtree)
)
What could be the cause of the problem?
Here is the related code not defined by standard emacs:
(defun goto-end-of-code-block ()
(re-search-forward "#\\+END_SRC.*$" nil t 1) ; no raising error
)

How to import module in scheme?

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

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

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