How do I require a library in PLT scheme (pre-racket)? - racket

I need to use a PLT-scheme library that only runs on a very old version of DrScheme (DrScheme v103p1), which I think is from at least 15 years ago.
The problem I am having is that I can't figure out how to use the library in my code because aparently the "require" function from modern racket didn't exist back then. What do I have to do to use a library then? All I know for now is that the file containing the definitions I want resides on "C:\Program Files\PLT\collects\mzlib\spidey.ss".
I tried using (require "spidey.ss") and (require "spidey") but both failed with a reference to unidentified identifier: required error.

You can use require-library to load things from the standard library:
http://download.plt-scheme.org/doc/103p1/html/mzscheme/node157.htm
(require-library "spidey.ss")
You can also use the support facilities (load and friends) to load single files. You need to use absolute paths though or else its going to search relative to your current working directory.
http://download.plt-scheme.org/doc/103p1/html/mzscheme/node149.htm
(load "C:/Program Files/PLT/collects/mzlib/spidey.ss")
Since MrSpidey is integrated into DrScheme, open DrScheme and then follow these instructions:
http://download.plt-scheme.org/doc/103p1/html/mrspidey/node4.htm

Related

How to use require to import a file whose filename is not in English in Racket?

I am trying to use Racket to "require" a file whose name is in Chinese. The compiler gives an error that test.rkt:2:3: require: bad module-path string at: "./lib/算数.rkt" in: (require "./lib/算数.rkt") #(13 14), but it does work when I rename 算数.rkt into math.rkt. So I am sure that the error occurs because the filename is not in English. Any method I can use to solve the problem?
I was able to replicate this, and while I didn't dig deep into the documentation to see if there's some listed restriction about what codepoints can be in a module path, I did find a workaround:
(require (file "./lib/算数.rkt"))
From the documentation for (file ...):
Similar to the plain rel-string case, but string is a path—possibly absolute—using the current platform’s path conventions and expand-user-path.
Other documentation says
This form is not portable, and it should not be used when a plain, portable rel-string suffices.
Since the normal way doesn't suffice, I guess this is one of the situations where it's okay to use.

How and where to set the default load location for sbcl / slime / emacs on Ubuntu 18

I have emacs / sbcl / slime working.
I'm going through a tutorial and hit the following example:
CL-USER> (load "hello.lisp")
; Loading /home/peter/my-lisp-programs/hello.lisp
The author doesn't specify how or where he set things up to default the load location to his example.
I've tried creating the EMACSLOADPATH environment variable and have tried a setq for load-path all with no positive results.
If I load a .lisp file using the entire path as in /home/bill/lisp/hello.lisp, it load and I can run it. Id like to know how and where to set the default to "~/lisp" so I can avoid an absolute path reference.
Before the answer, EMACSLOADPATH (or load-path) isn't related to common-lisp or sbcl (which is a common-lisp implementation). It is an emacs related variable. The root of the confusion is understandable, as emacs is a tool build on its own variant of lisp, elisp, and you write elisp code to extend or configure emacs. Emacs tool has its own elisp engine that runs those elips commands. And then you start using emacs as an editor to write common-lisp code, using plugins like slime to make it easier to interact with sbcl interpreter (or any other slime compatible common-lisp interpreter, for that matter).
For the default load location of common-lisp, load function of sbcl uses *default-pathname-defaults* to form the pathname from the argument, and it is generally set to the current directory (by slime at least - check swank:set-default-directory).
However, if you want an approach similar to python's import, where the function uses a list of directories to search for, I believe there are two options to start with.
Use quicklisp or asdf, as they are equivalent to python-import, but that probably means you define a 'system' (python: 'module') and use asdf:load-system. Here is an answer for this solution: example of using external libs in common-lisp
Write a load function yourself, and search for a predefined list of directories for the file name, and form an absolute path then call cl:load with the absolute path you decide.

Do we need to add (require 'package_name.el) in init file if we installed packages with package manager?

As titled , I'm confused whether we should add (require 'package_name) in init file if we have already used package manager rather than installed packages manually.
Some packages, like yasnippet or auto-complete, even if I don't add (require 'yasnippet) it still works fine, but with package like alpha (a package in Marmalade), if I don't add (require 'alpha), then Emacs cannot recognize the hot key C->, which is used to increase the transparency, and I get the error message C-> is undefined, so what is the require command actually doing and when should we add it?
require loads the library in question, unless it had already been loaded.
Loading libraries takes time, so autoloading is often preferred, as this defers the load until it is needed.
package.el facilitates autoloading for ELPA packages in general, so most of the time you do not need to require these -- however it is ultimately dependent on (a) the package author, and (b) exactly how you are trying to use it.
What you describe sounds simply like a packaging bug: i.e. that alpha package from Marmalade is poorly packaged. (require '<foo>) should not change key bindings, for example, since <foo> might be loaded "by accident" for all kinds of reasons.
Instead it should probably define an alpha-mode minor mode which is autoloaded, so instead of (require 'alpha) you'd use (alpha-mode 1) (or the equivalent from Customize).

Structuring large Lisp applications

I am currently trying to wrap my head around packages, systems & co.
I now have read Packages, systems, modules, libraries - WTF? a few times, and I think I'm still having difficulties to get it right.
If I simply want to split a Lisp source file into two files, where one shall "use" the other - how do I do that? Do I need to build a system for this? Should I use a module? …? I'm coming from a Node.js background, and there you can simply say
var foo = require('./foo');
to get a reference to what's exported in file foo.js. What is the closest equivalent to this in Lisp?
I understand that ASDF is for systems, and that it is bundled as part of Quicklisp, at least according to its documentation:
ASDF comes bundled with all recent releases of active Common Lisp implementations as well as with quicklisp […]
Okay, Quicklisp is for libraries, but what is their relationship? Is Quicklisp something such as a "package manager" in other languages? And if so, then what exactly does ASDF provide?
Sorry for these many questions, but I think it just shows the trouble I have to understand how to structure Lisp applications. Any help would be greatly appreciated :-)
System
For structuring large system use a system management tool. A 'free' one is ASDF.
You would need a system declaration, which lists the parts of you library or application. Typically it goes into an own file. Then you load a system or compile a system. There should be tutorials how to do that.
A simple Lisp system might have the following files:
a system file describing the system, its parts and whatever other stuff is needed (other systems)
a package file which describes the namespaces used
a basic tools file (for examples functions used by the macro)
a macro file which lists the macros (used so that they get compiled/loaded before the rest of the software)
one or more other files with functionality.
Quicklisp is independent of that. It's a software distribution tool.
Quick hack to compile and load files
But you can also compile and load files the old fashioned way without a system tool:
(defparameter *files*
'("/yourdir/foo.lisp" "/yourdir/bar.lisp"))
(defun compile-foobar ()
(mapc #'compile-file *files*))
(defun load-foobar ()
(mapc #'load *files*))
(defun compile-and-load ()
(mapc (lambda (file)
(load (compile-file file)))
*files*))
In reality there might be more to it, but often it's enough. It should be easy to write your own building tool. A typical system tool will provide many more features for building more complex software in a structured way. Many of the ideas for these tools reach back at least 35 years. See for example the Lisp Machine manual, here the edition from 1984, chapter Maintaining Large Systems.
The role of files
Note that in plain Common Lisp the role of files and its semantics are not very complex.
A file is not a namespace, it is not associated with a class/subclass or an object, it is not a module. You mix Lisp constructs in a file like you want. Files can be arbitrary large (for example one complex library has a version where it is delivered as single source file with 30000 lines). The only real place in the standard semantics where a file plays a role is when compiling a file. What side effects has compiling a file? What optimizations can a compiler do?
Other than that it is assumed that the development environment provides services like load and compiling groups of files aka systems, provide overviews of compilation errors, record source locations of definitions, can locate definitions and more. A tool like ASDF handles the system part.
There is a require function in Common Lisp, but it deprecated. If you simply want to split your code in one or more pieces, to use it interactively in the REPL, you can put the code in different files and then load each of them. If instead you want to write a full lisp project, I have found very useful the quickproject package, that provides a simple starting point for the creation of new packages.

Where are the files for a package?

Following this clisp basic website tutorial it asks me to define a package to persist the code:
(defpackage :retro-games
(:use :cl :cl-who :hunchentoot :parenscript))
However, I cannot work out where my code is being persisted. Where are the files?
NB: I have only one day's experience with lisp!
Where are the files? They are where you want them.
Create a new file anywhere you want.
Put the definitions into the file.
Done
Lisp can LOAD files and you can use COMPILE-FILE.
In Common Lisp there no connections between files, the location of a file and a package. The package definition just defines a namespace to symbols. But this is not necessarily connected to a file. Some tools help maintain something called a system, which is a collection of files, which can be compiled and loaded together.
To get a basic understanding how to use Common Lisp I recommend reading Practical Common Lisp by Peter Seibel.