Appendix 3: XLISP: An Object-oriented Lisp (original) (raw)
Previous Section | Next Section (Index) | Table of Contents | Title Page
Version 2.0
February 6, 1988
by
David Michael Betz
127 Taylor Road
Peterborough, NH 03458Copyright (c) 1988, by David Michael Betz
All Rights Reserved
Permission is granted for unrestricted non-commercial use
Introduction
XLISP is an experimental programming language combining some of the features of Common Lisp with an object-oriented extension capability. It was implemented to allow experimentation with object-oriented programming on small computers.
Implementations of XLISP run on virtually every operating system. XLISP is completely written in the programming language C and is easily extended with user written built-in functions and classes. It is available in source form to non-commercial users.
Many Common Lisp functions are built into XLISP. In addition, XLISP defines the objects Object and Class as primitives. Object is the only class that has no superclass and hence is the root of the class hierarchy tree. Class is the class of which all classes are instances (it is the only object that is an instance of itself).
This document is a brief description of XLISP. It assumes some knowledge of LISP and some understanding of the concepts of object-oriented programming.
I recommend the book Lisp by Winston and Horn and published by Addison Wesley for learning Lisp. The first edition of this book is based on MacLisp and the second edition is based on Common Lisp.
You will probably also need a copy of Common Lisp: The Language by Guy L. Steele, Jr., published by Digital Press to use as a reference for some of the Common Lisp functions that are described only briefly in this document.
A Note From The Author
If you have any problems with XLISP, feel free to contact me [me being David Betz - RBD] for help or advice. Please remember that since XLISP is available in source form in a high level language, many users [e.g. that Dannenberg fellow - RBD] have been making versions available on a variety of machines. If you call to report a problem with a specific version, I may not be able to help you if that version runs on a machine to which I don't have access. Please have the version number of the version that you are running readily accessible before calling me.
If you find a bug in XLISP, first try to fix the bug yourself using the source code provided. If you are successful in fixing the bug, send the bug report along with the fix to me. If you don't have access to a C compiler or are unable to fix a bug, please send the bug report to me and I'll try to fix it.
Any suggestions for improvements will be welcomed. Feel free to extend the language in whatever way suits your needs. However, PLEASE DO NOT RELEASE ENHANCED VERSIONS WITHOUT CHECKING WITH ME FIRST!! I would like to be the clearing house for new features added to XLISP. If you want to add features for your own personal use, go ahead. But, if you want to distribute your enhanced version, contact me first. Please remember that the goal of XLISP is to provide a language to learn and experiment with LISP and object-oriented programming on small computers. I don't want it to get so big that it requires megabytes of memory to run.
XLISP Command Loop
When XLISP is started, it first tries to load the workspacexlisp.wks
from the current directory. If that file doesn't exist, XLISP builds an initial workspace, empty except for the built-in functions and symbols.
Then XLISP attempts to load init.lsp
from the current directory. It then loads any files named as parameters on the command line (after appending .lsp
to their names).
XLISP then issues the following prompt:
>
This indicates that XLISP is waiting for an expression to be typed.
When a complete expression has been entered, XLISP attempts to evaluate that expression. If the expression evaluates successfully, XLISP prints the result and then returns to the initial prompt waiting for another expression to be typed.
Special Characters
When XLISP is running from a console, some control characters invoke operations:
- Backspace and Delete characters erase the previous character on the input line (if any).
- Control-U erases the entire input line.
- Control-C executes the TOP-LEVEL function.
- Control-G executes the CLEAN-UP function.
- Control-P executes the CONTINUE function.
- Control-B stops execution and enters the break command loop. Execution can be continued by typing Control-P or (CONTINUE).
- Control-E turns on character echoing (Linux and Mac OS X only).
- Control-F turns off character echoing (Linux and Mac OS X only).
- Control-T evaluates the INFO function.
Break Command Loop
When XLISP encounters an error while evaluating an expression, it attempts to handle the error in the following way:
If the symbol *breakenable*
is true, the message corresponding to the error is printed. If the error is correctable, the correction message is printed.
If the symbol *tracenable*
is true, a trace back is printed. The number of entries printed depends on the value of the symbol*tracelimit*
. If this symbol is set to something other than a number, the entire trace back stack is printed.
XLISP then enters a read/eval/print loop to allow the user to examine the state of the interpreter in the context of the error. This loop differs from the normal top-level read/eval/print loop in that if the user invokes the functioncontinue
, XLISP will continue from a correctable error. If the user invokes the function clean-up
, XLISP will abort the break loop and return to the top level or the next lower numbered break loop. When in a break loop, XLISP prefixes the break level to the normal prompt.
If the symbol *breakenable*
is nil
, XLISP looks for a surrounding errset function. If one is found, XLISP examines the value of the print flag. If this flag is true, the error message is printed. In any case, XLISP causes the errset function call to return nil
.
If there is no surrounding errset function, XLISP prints the error message and returns to the top level.
Data Types
There are several different data types available to XLISP programmers.
- lists - are linked lists of
cons
cells, where acons
cell is a pair of references called thecar
orhead
andcdr
ortail
. The empty list is the null reference denoted bynil
. (There is nocons
cell.) A list of 1 element is acons
cell whosecar
is the element and whosecdr
is a list of no elements, i.e.nil
. A list of 2 elements is acons
cell whosecar
is the first element and whosecdr
is a list containing the second element, and so on. - symbols - unique strings that serve as names of variables and names of functions. All symbols are in a global symbol table.
- strings - non-unique, immutable character strings.
- integers
- characters - note that characters are not strings.
- floats
- objects
- arrays
- streams
- subrs (built-in functions)
- fsubrs (special forms)
- closures (user defined functions)
The Evaluator
The process of evaluation in XLISP:
- Strings, integers, characters, floats, objects, arrays, streams, subrs, fsubrs and closures evaluate to themselves.
- Symbols act as variables and are evaluated by retrieving the value associated with their current binding.
- Lists are evaluated by examining the first element of the list and then taking one of the following actions:
- If it is a symbol, the functional binding of the symbol is retrieved.
- If it is a lambda expression, a closure is constructed for the function described by the lambda expression.
- If it is a subr, fsubr or closure, it stands for itself.
- Any other value is an error.
Then, the value produced by the previous step is examined: - If it is a subr or closure, the remaining list elements are evaluated and the subr or closure is called with these evaluated expressions as arguments.
- If it is an fsubr, the fsubr is called using the remaining list elements as arguments (unevaluated).
- If it is a macro, the macro is expanded using the remaining list elements as arguments (unevaluated). The macro expansion is then evaluated in place of the original macro call.
Lexical Conventions
The following conventions must be followed when entering XLISP programs:
Comments in XLISP code begin with a semi-colon character and continue to the end of the line.
Symbol names in XLISP can consist of any sequence of non-blank printable characters except the following:
( ) ' ` , " ;
Uppercase and lowercase characters are not distinguished within symbol names. All lowercase characters are mapped to uppercase on input.
Integer literals consist of a sequence of digits optionally beginning with a +
or -
. The range of values an integer can represent is limited by the size of a C long
on the machine on which XLISP is running.
Floating point literals consist of a sequence of digits optionally beginning with a +
or -
and including an embedded decimal point. The range of values a floating point number can represent is limited by the size of a C float
(double
on machines with 32 bit addresses) on the machine on which XLISP is running.
Literal strings are sequences of characters surrounded by double quotes. Within quoted strings the “\
” character is used to allow non-printable characters to be included. The codes recognized are:
\\
means the character “\
”\n
means newline\t
means tab\r
means return\f
means form feed\nnn
means the character whose octal code is nnn
Readtables
The behavior of the reader is controlled by a data structure called a readtable. The reader uses the symbol *readtable*
to locate the current readtable. This table controls the interpretation of input characters. It is an array with 128 entries, one for each of the ASCII character codes. Each entry contains one of the following things:
NIL
– Indicating an invalid character:CONSTITUENT
– Indicating a symbol constituent:WHITE-SPACE
– Indicating a whitespace character(:TMACRO . _fun_)
– Terminating readmacro(:NMACRO . _fun_)
– Non-terminating readmacro:SESCAPE
– Single escape character ('\'):MESCAPE
– Multiple escape character ('|')
In the case of :TMACRO
and :NMACRO
, the fun component is a function. This can either be a built-in readmacro function or a lambda expression. The function should take two parameters. The first is the input stream and the second is the character that caused the invocation of the readmacro. The readmacro function should return NIL
to indicate that the character should be treated as white space or a value consed with NIL
to indicate that the readmacro should be treated as an occurence of the specified value. Of course, the readmacro code is free to read additional characters from the input stream.
XLISP defines several useful read macros:
'
==(quote
)
#'
==(function
)
#(
...)
== an array of the specified expressions#x
== a hexadecimal number (0-9,A-F)#o
== an octal number (0-7)#b
== a binary number (0-1)#\
== literal of type character#\Newline
== newline character#\Space
== space character#\Tab
== tab character#|
...|#
== a comment#:
== an uninterned symbol`
__ ==(backquote
__)
,
==(comma
)
,@
==(comma-at
)
Lambda Lists
There are several forms in XLISP that require that a “lambda list” be specified. A lambda list is a definition of the arguments accepted by a function. There are four different types of arguments.
The lambda list starts with required arguments. Required arguments must be specified in every call to the function.
The required arguments are followed by the &optional
arguments. Optional arguments may be provided or omitted in a call. An initialization expression may be specified to provide a default value for an &optional
argument if it is omitted from a call. If no initialization expression is specified, an omitted argument is initialized to NIL
. It is also possible to provide the name of a supplied-p
variable that can be used to determine if a call provided a value for the argument or if the initialization expression was used. If specified, the supplied- p variable will be bound to T if a value was specified in the call and NIL
if the default value was used.
The &optional
arguments are followed by the &rest
argument. The&rest
argument gets bound to the remainder of the argument list after the required and &optional
arguments have been removed.
The &rest
argument is followed by the &key
arguments. When a keyword argument is passed to a function, a pair of values appears in the argument list. The first expression in the pair should evaluate to a keyword symbol (a symbol that begins with a “:
”). The value of the second expression is the value of the keyword argument. Like &optional
arguments, &key
arguments can have initialization expressions and supplied-p variables. In addition, it is possible to specify the keyword to be used in a function call. If no keyword is specified, the keyword obtained by adding a “:
” to the beginning of the keyword argument symbol is used. In other words, if the keyword argument symbol isfoo
, the keyword will be :foo
.
The &key
arguments are followed by the &aux
variables. These are local variables that are bound during the evaluation of the function body. It is possible to have initialization expressions for the &aux
variables.
Here is the complete syntax for lambda lists:
(rarg...
[&optional
[oarg | (oarg [init [_svar_]])]...]
[&rest
_rarg_]
[&key
[karg | ([karg | (key karg)] [init [_svar_]])]...&allow
-other-keys]
[&aux
[aux | (aux [_init_])]...])where:
rarg is a required argument symbol
oarg is an&optional
argument symbol
rarg is the&rest
argument symbol
karg is a&key
argument symbol
key is a keyword symbol
aux is an auxiliary variable symbol
init is an initialization expression
svar is a supplied-p variable symbol
Objects
Definitions:
- selector – a symbol used to select an appropriate method
- message – a selector and a list of actual arguments
- method – the code that implements a message Since XLISP was created to provide a simple basis for experimenting with object-oriented programming, one of the primitive data types included is object. In XLISP, an object consists of a data structure containing a pointer to the object's class as well as an array containing the values of the object's instance variables.
Officially, there is no way to see inside an object (look at the values of its instance variables). The only way to communicate with an object is by sending it a message.
You can send a message to an object using the send
function. This function takes the object as its first argument, the message selector as its second argument (which must be a symbol) and the message arguments as its remaining arguments.
The send
function determines the class of the receiving object and attempts to find a method corresponding to the message selector in the set of messages defined for that class. If the message is not found in the object's class and the class has a super-class, the search continues by looking at the messages defined for the super-class. This process continues from one super-class to the next until a method for the message is found. If no method is found, an error occurs.
When a method is found, the evaluator binds the receiving object to the symbol self
and evaluates the method using the remaining elements of the original list as arguments to the method. These arguments are always evaluated prior to being bound to their corresponding formal arguments. The result of evaluating the method becomes the result of the expression.
Within the body of a method, a message can be sent to the current object by calling the (send self ...)
. The method lookup starts with the object's class regardless of the class containing the current method.
Sometimes it is desirable to invoke a general method in a superclass even when it is overridden by a more specific method in a subclass. This can be accomplished by calling send-super
, which begins the method lookup in the superclass of the class defining the current method rather than in the class of the current object.
The send-super
function takes a selector as its first argument (which must be a symbol) and the message arguments as its remaining arguments. Notice that send-super
can only be sent from within a method, and the target of the message is always the current object (self
). (send-super ...)
is similar to (send self ...)
except that method lookup begins in the superclass of the class containing the current method rather than the class of the current object.
The “Object” Class
Object
– the top of the class hierarchy.
Messages:
:show
– show an object's instance variables.
returns – the object
:class
– return the class of an object
returns – the class of the object
:isa
class – test if object inherits from class
returns – t
if object is an instance of class or a subclass of class, otherwise nil
:isnew
– the default object initialization routine
returns – the object
The “Class” Class
Class
– class of all object classes (including itself)
Messages:
:new
– create a new instance of a class
returns – the new class object
:isnew
ivars [cvars [_super_]] – initialize a new class
ivars – the list of instance variable symbols
cvars – the list of class variable symbols
super – the superclass (default is object)
returns – the new class object
:answer
msg fargs code – add a message to a class
msg – the message symbol
fargs – the formal argument list (lambda list)
code – a list of executable expressions
returns – the object
When a new instance of a class is created by sending the message:new
to an existing class, the message :isnew
followed by whatever parameters were passed to the :new
message is sent to the newly created object.
When a new class is created by sending the :new
message to the object Class
, an optional parameter may be specified indicating the superclass of the new class. If this parameter is omitted, the new class will be a subclass of Object
. A class inherits all instance variables, class variables, and methods from its super-class.
Profiling
The Xlisp 2.0 release has been extended with a profiling facility, which counts how many times and where eval
is executed. A separate count is maintained for each named function, closure, or macro, and a count indicates an eval
in the immediately (lexically) enclosing named function, closure, or macro. Thus, the count gives an indication of the amount of time spent in a function, not counting nested function calls. The list of all functions executed is maintained on the global *profile*
variable. These functions in turn have *profile*
properties, which maintain the counts. The profile system merely increments counters and puts symbols on the *profile*
list. It is up to the user to initialize data and gather results. Profiling is turned on or off with the profile
function. Unfortunately, methods cannot be profiled with this facility.
Symbols
self
- the current object (within a method context)*obarray*
- the object hash table*standard-input*
- the standard input stream*standard-output*
- the standard output stream*error-output*
- the error output stream*trace-output*
- the trace output stream*debug-io*
- the debug i/o stream*breakenable*
- flag controlling entering break loop on errors*tracelist*
- list of names of functions to trace*tracenable*
- enable trace back printout on errors*tracelimit*
- number of levels of trace back information*evalhook*
- user substitute for the evaluator function*applyhook*
- (not yet implemented)*readtable*
- the current readtable*unbound*
- indicator for unbound symbols*gc-flag*
- controls the printing of gc messages*gc-hook*
- function to call after garbage collection*integer-format*
- format for printing integers (“%d” or “%ld”)*float-format*
- format for printing floats (“%g”)*print-case*
- symbol output case (:upcase or :downcase)
There are several symbols maintained by the read/eval/print loop. The symbols +
, ++
, and +++
are bound to the most recent three input expressions. The symbols *
, **
and ***
are bound to the most recent three results. The symbol -
is bound to the expression currently being evaluated. It becomes the value of +
at the end of the evaluation.
Evaluation Functions
eval(_expr_)
[SAL](eval _expr_)
[LISP] – evaluate an xlisp expression
expr – the expression to be evaluated
returns – the result of evaluating the expression
apply(_fun_, _args_)
[SAL](apply _fun_ _args_)
[LISP] – apply a function to a list of arguments
fun – the function to apply (or function symbol)
args – the argument list
returns – the result of applying the function to the arguments
funcall(_fun_, _arg_...)
[SAL](funcall _fun_ _arg_...)
[LISP] – call a function with arguments
fun – the function to call (or function symbol)
arg – arguments to pass to the function
returns – the result of calling the function with the arguments
quote(_expr_)
[SAL](quote _expr_)
[LISP] – return an expression unevaluated
expr – the expression to be quoted (quoted)
returns – expr unevaluated
(function _expr_)
[LISP] – get the
functional interpretation. Note that in SAL, the function can be accessed as #function
.
expr – the symbol or lambda expression (quoted)
returns – the functional interpretation
backquote(_expr_)
[SAL](backquote _expr_)
[LISP] – fill in a template
expr – the template
returns – a copy of the template with comma and comma-at
expressions expanded
lambda(_args_, _expr_...)
[SAL](lambda _args_ _expr_...)
[LISP] – make a function closure
args – formal argument list (lambda list) (quoted)
expr – expressions of the function body
returns – the function closure
get-lambda-expression(_closure_)
[SAL](get-lambda-expression _closure_)
[LISP] – get the lambda expression
closure – the closure
returns – the original lambda expression
macroexpand(_form_)
[SAL](macroexpand _form_)
[LISP] – recursively expand macro calls
form – the form to expand
returns – the macro expansion
macroexpand-1(_form_)
[SAL](macroexpand-1 _form_)
[LISP] – expand a macro call
form – the macro call form
returns – the macro expansion
Symbol Functions
(set _sym_ _expr_)
[LISP] – set the value of a symbol. Note that in SAL, the function can be accessed as #set
.
sym – the symbol being set
expr – the new value
returns – the new value
setq([_sym_, _expr_]...)
[SAL](setq [_sym_ _expr_]...)
[LISP] – set the value of a symbol. Note that in SAL, the set
command is normally used.
sym – the symbol being set (quoted)
expr – the new value
returns – the new value
psetq([_sym_, _expr_]...)
[SAL](psetq [_sym_ _expr_]...)
[LISP] – parallel version of setq
sym – the symbol being set (quoted)
expr – the new value
returns – the new value
setf([_place_, _expr_]...)
[SAL](setf [_place_ _expr_]...)
[LISP] – set the value of a field
place – the field specifier (quoted):
sym – set value of a symbol
(car expr) – set car of a cons node
(cdr expr) – set cdr of a cons node
(nth n expr) – set nth car of a list
(aref expr n) – set nth element of an array
(get sym prop) – set value of a property
(symbol-value sym) – set value of a symbol
(symbol-function sym) – set functional value of a symbol
(symbol-plist sym) – set property list of a symbol
expr – the new value
returns – the new value
(defun _sym_ _fargs_ _expr_...)
[LISP] – define a function(defmacro _sym_ _fargs_ _expr_...)
[LISP] – define a macro
sym – symbol being defined (quoted)
fargs – formal argument list (lambda list) (quoted)
expr – expressions constituting the body of the
function (quoted)
returns – the function symbol
gensym([_tag_])
[SAL](gensym [_tag_])
[LISP] – generate a symbol
tag – string or number
returns – the new symbol
intern(_pname_)
[SAL](intern _pname_)
[LISP] – make an interned symbol
pname – the symbol's print name string
returns – the new symbol
make-symbol(_pname_)
[SAL](make-symbol _pname_)
[LISP] – make an uninterned symbol
pname – the symbol's print name string
returns – the new symbol
symbol-name(_sym_)
[SAL](symbol-name _sym_)
[LISP] – get the print name of a symbol
sym – the symbol
returns – the symbol's print name
symbol-value(_sym_)
[SAL](symbol-value _sym_)
[LISP] – get the value of a symbol
sym – the symbol
returns – the symbol's value
symbol-function(_sym_)
[SAL](symbol-function _sym_)
[LISP] – get the functional value of a symbol
sym – the symbol
returns – the symbol's functional value
symbol-plist(_sym_)
[SAL](symbol-plist _sym_)
[LISP] – get the property list of a symbol
sym – the symbol
returns – the symbol's property list
hash(_sym_, _n_)
[SAL](hash _sym_ _n_)
[LISP] – compute the hash index for a symbol
sym – the symbol or string
n – the table size (integer)
returns – the hash index (integer)
Property List Functions
get(_sym_, _prop_)
[SAL](get _sym_ _prop_)
[LISP] – get the value of a property
sym – the symbol
prop – the property symbol
returns – the property value or nil
putprop(_sym_, _val_, _prop_)
[SAL](putprop _sym_ _val_ _prop_)
[LISP] – put a property onto a property list
sym – the symbol
val – the property value
prop – the property symbol
returns – the property value
remprop(_sym_, _prop_)
[SAL](remprop _sym_ _prop_)
[LISP] – remove a property
sym – the symbol
prop – the property symbol
returns – nil
Array Functions
aref(_array_, _n_)
[SAL](aref _array_ _n_)
[LISP] – get the nth element of an array
array – the array
n – the array index (integer)
returns – the value of the array element
make-array(_size_)
[SAL](make-array _size_)
[LISP] – make a new array
size – the size of the new array (integer)
returns – the new array
vector(_expr_...)
[SAL](vector _expr_...)
[LISP] – make an initialized vector
expr – the vector elements
returns – the new vector
List Functions
car(_expr_)
[SAL](car _expr_)
[LISP] – return the car of a list node
expr – the list node
returns – the car of the list node
cdr(_expr_)
[SAL](cdr _expr_)
[LISP] – return the cdr of a list node
expr – the list node
returns – the cdr of the list node
c_xx_r(_expr_)
[SAL](c_xx_r _expr_)
[LISP] – all c_xx_r combinations
c_xxx_r(_expr_)
[SAL](c_xxx_r _expr_)
[LISP] – all c_xxx_r combinations
c_xxxx_r(_expr_)
[SAL](c_xxxx_r _expr_)
[LISP] – all c_xxxx_r combinations
first(_expr_)
[SAL](first _expr_)
[LISP] – a synonym for car
second(_expr_)
[SAL](second _expr_)
[LISP] – a synonym for cadr
third(_expr_)
[SAL](third _expr_)
[LISP] – a synonym for caddr
fourth(_expr_)
[SAL](fourth _expr_)
[LISP] – a synonym for cadddr
rest(_expr_)
[SAL](rest _expr_)
[LISP] – a synonym for cdr
cons(_expr1_, _expr2_)
[SAL](cons _expr1_ _expr2_)
[LISP] – construct a new list node
expr1 – the car of the new list node
expr2 – the cdr of the new list node
returns – nil
list(_expr_...)
[SAL](list _expr_...)
[LISP] – create a list of values
expr – expressions to be combined into a list
returns – the new list
append(_expr_...)
[SAL](append _expr_...)
[LISP] – append lists
expr – lists whose elements are to be appended
returns – the new list
reverse(_expr_)
[SAL](reverse _expr_)
[LISP] – reverse a list
expr – the list to reverse
returns – a new list in the reverse order
last(_list_)
[SAL](last _list_)
[LISP] – return the last list node of a list
list – the list
returns – the last list node in the list
member(_expr_, _list_, test: _test_, test-not: _test-not_)
[SAL](member _expr_ _list_ &key :test :test-not)
[LISP] – find an expression in a list
expr – the expression to find
list – the list to search
:test – the test function (defaults to eql)
:test-not – the test function (sense inverted)
returns – the remainder of the list starting with the expression
assoc(_expr_, _alist_, test: _test_, test-not: _test-not_)
[SAL](assoc _expr_ _alist_ &key :test :test-not)
[LISP] – find an expression in an a-list
expr – the expression to find
alist – the association list
:test – the test function (defaults to eql)
:test-not – the test function (sense inverted)
returns – the alist entry or nil
remove(_expr_, _list_, test: _test_, test-not: _test-not_)
[SAL](remove _expr_ _list_ &key :test :test-not)
[LISP] – remove elements from a list
expr – the element to remove
list – the list
:test – the test function (defaults to eql)
:test-not – the test function (sense inverted)
returns – copy of list with matching expressions removed
remove-if(_test_, _list_)
[SAL](remove-if _test_ _list_)
[LISP] – remove elements that pass test
test – the test predicate
list – the list
returns – copy of list with matching elements removed
remove-if-not(_test_, _list_)
[SAL](remove-if-not _test_ _list_)
[LISP] – remove elements that fail test
test – the test predicate
list – the list
returns – copy of list with non-matching elements removed
length(_expr_)
[SAL](length _expr_)
[LISP] – find the length of a list, vector or string
expr – the list, vector or string
returns – the length of the list, vector or string
nth(_n_, _list_)
[SAL](nth _n_ _list_)
[LISP] – return the nth element of a list
n – the number of the element to return (zero origin)
list – the list
returns – the nth element or nil
if the list isn't that long
nthcdr(_n_, _list_)
[SAL](nthcdr _n_ _list_)
[LISP] – return the nth cdr of a list
n – the number of the element to return (zero origin)
list – the list
returns – the nth cdr or nil
if the list isn't that long
mapc(_fcn_, _list1_, _list_...)
[SAL](mapc _fcn_ _list1_ _list_...)
[LISP] – apply function to successive cars
fcn – the function or function name
listn – a list for each argument of the function
returns – the first list of arguments
mapcar(_fcn_, _list1_, _list_...)
[SAL](mapcar _fcn_ _list1_ _list_...)
[LISP] – apply function to successive cars
fcn – the function or function name
listn – a list for each argument of the function
returns – a list of the values returned
mapl(_fcn_, _list1_, _list_...)
[SAL](mapl _fcn_ _list1_ _list_...)
[LISP] – apply function to successive cdrs
fcn – the function or function name
listn – a list for each argument of the function
returns – the first list of arguments
maplist(_fcn_, _list1_, _list_...)
[SAL](maplist _fcn_ _list1_ _list_...)
[LISP] – apply function to successive cdrs
fcn – the function or function name
listn – a list for each argument of the function
returns – a list of the values returned
subst(_to_, _from_, _expr_, test: _test_, test-not: _test-not_)
[SAL](subst _to_ _from_ _expr_ &key :test :test-not)
[LISP] – substitute expressions
to – the new expression
from – the old expression
expr – the expression in which to do the substitutions
:test – the test function (defaults to eql)
:test-not – the test function (sense inverted)
returns – the expression with substitutions
sublis(_alist_, _expr_, test: _test_, test-not: _test-not_)
[SAL](sublis _alist_ _expr_ &key :test :test-not)
[LISP] – substitute with an a-list
alist – the association list
expr – the expression in which to do the substitutions
:test – the test function (defaults to eql)
:test-not – the test function (sense inverted)
returns – the expression with substitutions
Destructive List Functions
rplaca(_list_, _expr_)
[SAL](rplaca _list_ _expr_)
[LISP] – replace the car of a list node
list – the list node
expr – the new value for the car of the list node
returns – the list node after updating the car
rplacd(_list_, _expr_)
[SAL](rplacd _list_ _expr_)
[LISP] – replace the cdr of a list node
list – the list node
expr – the new value for the cdr of the list node
returns – the list node after updating the cdr
nconc(_list_...)
[SAL](nconc _list_...)
[LISP] – destructively concatenate lists
list – lists to concatenate
returns – the result of concatenating the lists
delete(_expr_, _list_, test: _test_, test-not: _test-not_)
[SAL](delete _expr_ _list_ &key :test :test-not)
[LISP] – delete elements from a list
expr – the element to delete
list – the list
:test – the test function (defaults to eql)
:test-not – the test function (sense inverted)
returns – the list with the matching expressions deleted
delete-if(_test_, _list_)
[SAL](delete-if _test_ _list_)
[LISP] – delete elements that pass test
test – the test predicate
list – the list
returns – the list with matching elements deleted
delete-if-not(_test_, _list_)
[SAL](delete-if-not) _test_ _list_)
[LISP] – delete elements that fail test
test – the test predicate
list – the list
returns – the list with non-matching elements deleted
sort(_list_, _test_)
[SAL](sort _list_ _test_)
[LISP] – sort a list
list – the list to sort
test – the comparison function
returns – the sorted list
Note: The comparison function should have two parameters and return true if the first parameter should come before the second parameter in the sorted result. For a list of numbers, built-in comparison functions can be used, e.g.
(sort '(3 2 1) #'<)
returns
(1 2 3)
. To sort a list of lists by the first element of each list, you can write a function to access the keys and compare them, e.g.
(sort '((3 c) (2 b) (1 a)) #'(lambda (x y) (< (car x) (car y))))
returns
((1 A) (2 B) (3 C))
. In SAL, you could write
function my-sort(a, b) return first(a) < first(b)
print sort({{3 c} {2 b} {1 a}}, quote(my-sort))
, which will print
{{1 A} {2 B} {3 C}}
.
Predicate Functions
atom(_expr_)
[SAL](atom _expr_)
[LISP] – is this an atom?
expr – the expression to check
returns – t
if the value is an atom, nil
otherwise
symbolp(_expr_)
[SAL](symbolp _expr_)
[LISP] – is this a symbol?
expr – the expression to check
returns – t
if the expression is a symbol, nil
otherwise
numberp(_expr_)
[SAL](numberp _expr_)
[LISP] – is this a number?
expr – the expression to check
returns – t
if the expression is a number, nil
otherwise
null(_expr_)
[SAL](null _expr_)
[LISP] – is this an empty list?
expr – the list to check
returns – t
if the list is empty, nil
otherwise
not(_expr_)
[SAL](not _expr_)
[LISP] – is this false?
expr – the expression to check
return – t
if the value is nil
, nil
otherwise
listp(_expr_)
[SAL](listp _expr_)
[LISP] – is this a list?
expr – the expression to check
returns – t
if the value is a cons or nil
, nil
otherwise
endp(_list_)
[SAL](endp _list_)
[LISP] – is this the end of a list
list – the list
returns – t
if the value is nil
, nil
otherwise
consp(_expr_)
[SAL](consp _expr_)
[LISP] – is this a non-empty list?
expr – the expression to check
returns – t
if the value is a cons, nil
otherwise
integerp(_expr_)
[SAL](integerp _expr_)
[LISP] – is this an integer?
expr – the expression to check
returns – t
if the value is an integer, nil
otherwise
floatp(_expr_)
[SAL](floatp _expr_)
[LISP] – is this a float?
expr – the expression to check
returns – t
if the value is a float, nil
otherwise
stringp(_expr_)
[SAL](stringp _expr_)
[LISP] – is this a string?
expr – the expression to check
returns – t
if the value is a string, nil
otherwise
characterp(_expr_)
[SAL](characterp _expr_)
[LISP] – is this a character?
expr – the expression to check
returns – t
if the value is a character, nil
otherwise
arrayp(_expr_)
[SAL](arrayp _expr_)
[LISP] – is this an array?
expr – the expression to check
returns – t
if the value is an array, nil
otherwise
streamp(_expr_)
[SAL](streamp _expr_)
[LISP] – is this a stream?
expr – the expression to check
returns – t
if the value is a stream, nil
otherwise
objectp(_expr_)
[SAL](objectp _expr_)
[LISP] – is this an object?
expr – the expression to check
returns – t
if the value is an object, nil
otherwise
filep(_expr_)
[SAL](filep _expr_)
[LISP] – is this a file?
expr – the expression to check
returns – t
if the value is an object, nil
otherwise
This is not part of standard XLISP nor is it built-in. Nyquist defines it though.
boundp(_sym_)
[SAL](boundp _sym_)
[LISP] – is a value bound to this symbol?
sym – the symbol
returns – t
if a value is bound to the symbol, nil
otherwise
fboundp(_sym_)
[SAL](fboundp _sym_)
[LISP] – is a functional value bound to this symbol?
sym – the symbol
returns – t
if a functional value is bound to the symbol,
nil
otherwise
minusp(_expr_)
[SAL](minusp _expr_)
[LISP] – is this number negative?
expr – the number to test
returns – t
if the number is negative, nil
otherwise
zerop(_expr_)
[SAL](zerop _expr_)
[LISP] – is this number zero?
expr – the number to test
returns – t
if the number is zero, nil
otherwise
plusp(_expr_)
[SAL](plusp _expr_)
[LISP] – is this number positive?
expr – the number to test
returns – t
if the number is positive, nil
otherwise
evenp(_expr_)
[SAL](evenp _expr_)
[LISP] – is this integer even?
expr – the integer to test
returns – t
if the integer is even, nil
otherwise
oddp(_expr_)
[SAL](oddp _expr_)
[LISP] – is this integer odd?
expr – the integer to test
returns – t
if the integer is odd, nil
otherwise
eq(_expr1_, _expr2_)
[SAL](eq _expr1_ _expr2_)
[LISP] – are the expressions identical (pointer equality)? Numbers and strings are generally not eq
, e.g. (eq 256 256)
, (eq 1.0 1.0)
, and (eq "a" "a")
are false.
expr1 – the first expression
expr2 – the second expression
returns – t
if they are equal, nil
otherwise
eql(_expr1_, _expr2_)
[SAL](eql _expr1_ _expr2_)
[LISP] – are the expressions of equal value? (eql
tests for identical objects (pointer equality) except for numbers. Two numbers can be eql
even if they are stored in different locations. However, a FIXNUM is never eql
to a FLONUM, i.e. (eql 1 1.0)
is false.)
expr1 – the first expression
expr2 – the second expression
returns – t
if they are equal, nil
otherwise
equal(_expr1_, _expr2_)
[SAL](equal _expr1_ _expr2_)
[LISP] – are the expressions equal? Arrays are not equal
unless they are the same array (pointer equality), but numbers and strings are compared by value, and lists are tested recursively for equal
content. A FIXNUM is never equal
to a FLONUM.
expr1 – the first expression
expr2 – the second expression
returns – t
if they are equal, nil
otherwise
Control Constructs
(cond _pair_...)
[LISP] – evaluate conditionally
pair – pair consisting of:
(pred expr...)
where:
pred – is a predicate expression
expr – evaluated if the predicate
is not nil
returns – the value of the first expression whose predicate is notnil
and(_expr_...)
[SAL](and _expr_...)
[LISP] – the logical and of a list of expressions
expr – the expressions to be anded
returns – nil
if any expression evaluates to nil
,
otherwise the value of the last expression
(evaluation of expressions stops after the first
expression that evaluates to nil
)
or(_expr_...)
[SAL](or _expr_...)
[LISP] – the logical or of a list of expressions
expr – the expressions to be ored
returns – nil
if all expressions evaluate to nil
,
otherwise the value of the first non-nil
expression
(evaluation of expressions stops after the first
expression that does not evaluate to nil
)
(if _texpr_ _expr1_ [_expr2_])
[LISP] – evaluate expressions conditionally.
texpr – the test expression
expr1 – the expression to be evaluated if texpr is non-nil
expr2 – the expression to be evaluated if texpr is nil
(default is nil
)
returns – the value of the selected expression.
Note that the SAL conditional expression syntax is #?(_test_, _iftrue-expression_, _iffalse-expression_)
, but #if
may be used instead of #?
. Either form may omit the third argument, which defaults to nil
.
when(_texpr_, _expr_...)
[SAL](when _texpr_ _expr_...)
[LISP] – evaluate only when a condition is true
texpr – the test expression
expr – the expression(s) to be evaluated if texpr is non-nil
returns – the value of the last expression or nil
unless(_texpr_, _expr_...)
[SAL](unless _texpr_ _expr_...)
[LISP] – evaluate only when a condition is false
texpr – the test expression
expr – the expression(s) to be evaluated if texpr is nil
returns – the value of the last expression or nil
(case _expr_ _case_...)
[LISP] – select by case
expr – the selection expression
case – pair consisting of:
(value expr...)
where:
value – is a single expression or a list of
expressions (unevaluated)
expr – are expressions to execute if the
case matches
returns – the value of the last expression of the matching case
(let (_binding_...) _expr_...)
[LISP] – create local bindings(let* (_binding_...) _expr_...)
[LISP] – let with sequential binding
binding – the variable bindings each of which is either:
a symbol (which is initialized to
nil
)a list whose car is a symbol and whose cadr
is an initialization expression
expr – the expressions to be evaluated
returns – the value of the last expression
(flet (_binding_...) _expr_...)
[LISP] – create local functions(labels (_binding_...) _expr_...)
[LISP] – flet with recursive functions(macrolet (_binding_...) _expr_...)
[LISP] – create local macros
binding – the function bindings each of which is:
(sym fargs expr...)
where:
sym – the function/macro name
fargs – formal argument list (lambda list)
expr – expressions constituting the body of
the function/macro
expr – the expressions to be evaluated
returns – the value of the last expression
catch(_sym_, _expr_...)
[SAL](catch _sym_ _expr_...)
[LISP] – evaluate expressions and catch throws
sym – the catch tag
expr – expressions to evaluate
returns – the value of the last expression the throw expression
throw(_sym_ [, _expr_])
[SAL](throw _sym_ [_expr_])
[LISP] – throw to a catch
sym – the catch tag
expr – the value for the catch to return (defaults to nil
)
returns – never returns
unwind-protect(_expr_, _cexpr_...)
[SAL](unwind-protect _expr_ _cexpr_...)
[LISP] – protect evaluation of an expression
expr – the expression to protect
cexpr – the cleanup expressions
returns – the value of the expression
Note: unwind-protect guarantees to execute the cleanup expressions
even if a non-local exit terminates the evaluation of the
protected expression
Looping Constructs
(loop _expr_...)
[LISP] – basic looping form
expr – the body of the loop
returns – never returns (must use non-local exit)
(do (_binding_...) (_texpr_ _rexpr_...) _expr_...)
[LISP](do* (_binding_...) (_texpr_ _rexpr_...) _expr_...)
[LISP]
binding – the variable bindings each of which is either:
a symbol (which is initialized to
nil
)a list of the form: (sym init [_step_])
where:
sym – is the symbol to bind
init – is the initial value of the symbol
step – is a step expression
texpr – the termination test expression
rexpr – result expressions (the default is nil
)
expr – the body of the loop (treated like an implicit prog)
returns – the value of the last result expression
(dolist (_sym_ _expr_ [_rexpr_]) _expr_...)
[LISP] – loop through a list
sym – the symbol to bind to each list element
expr – the list expression
rexpr – the result expression (the default is nil
)
expr – the body of the loop (treated like an implicit prog)
(dotimes (_sym_ _expr_ [_rexpr_]) _expr_...)
[LISP] – loop from zero to n-1
sym – the symbol to bind to each value from 0 to n-1
expr – the number of times to loop
rexpr – the result expression (the default is nil
)
expr – the body of the loop (treated like an implicit prog)
The Program Feature
(prog (_binding_...) _expr_...)
[LISP] – the program feature(prog* (_binding_...) _expr_...)
[LISP] – prog with sequential binding
binding – the variable bindings each of which is either:
a symbol (which is initialized to
nil
)a list whose car is a symbol and whose cadr
is an initialization expression
expr – expressions to evaluate or tags (symbols)
returns – nil
or the argument passed to the return function
block(_name_, _expr_...)
[SAL](block _name_ _expr_...)
[LISP] – named block
name – the block name (symbol)
expr – the block body
returns – the value of the last expression
(return [_expr_])
[LISP] – cause a prog construct to return a value
expr – the value (defaults to nil
)
returns – never returns
return-from(_name_ [, _value_])
[SAL](return-from _name_ [_value_])
[LISP] – return from a named block
name – the block name (symbol)
value – the value to return (defaults to nil
)
returns – never returns
tagbody(_expr_...)
[SAL](tagbody _expr_...)
[LISP] – block with labels
expr – expression(s) to evaluate or tags (symbols)
returns – nil
go(_sym_)
[SAL](go _sym_)
[LISP] – go to a tag within a tagbody or prog
sym – the tag (quoted)
returns – never returns
(progv _slist_ _vlist_ _expr_...)
[LISP] – dynamically bind symbols
slist – list of symbols
vlist – list of values to bind to the symbols
expr – expression(s) to evaluate
returns – the value of the last expression
prog1(_expr1_, _expr_...)
[SAL](prog1 _expr1_ _expr_...)
[LISP] – execute expressions sequentially
expr1 – the first expression to evaluate
expr – the remaining expressions to evaluate
returns – the value of the first expression
prog2(_expr1_, _expr2_, _expr_...)
[SAL](prog2 _expr1_ _expr2_ _expr_...)
[LISP] – execute expressions sequentially
expr1 – the first expression to evaluate
expr2 – the second expression to evaluate
expr – the remaining expressions to evaluate
returns – the value of the second expression
progn(_expr_...)
[SAL](progn _expr_...)
[LISP] – execute expressions sequentially
expr – the expressions to evaluate
returns – the value of the last expression (or nil
)
Debugging and Error Handling
trace(_sym_)
[SAL](trace _sym_)
[LISP] – add a function to the trace list
sym – the function to add (quoted)
returns – the trace list
untrace(_sym_)
[SAL](untrace _sym_)
[LISP] – remove a function from the trace list
sym – the function to remove (quoted)
returns – the trace list
error(_emsg_ [, _arg_])
[SAL](error _emsg_ [_arg_])
[LISP] – signal a non-correctable error
emsg – the error message string
arg – the argument expression (printed after the message)
returns – never returns
cerror(_cmsg_, _emsg_ [, _arg_])
[SAL](cerror _cmsg_ _emsg_ [_arg_])
[LISP] – signal a correctable error
cmsg – the continue message string
emsg – the error message string
arg – the argument expression (printed after the message)
returns – nil
when continued from the break loop
break([_bmsg_ [, _arg_]])
[SAL](break [_bmsg_ [_arg_]])
[LISP] – enter a break loop
bmsg – the break message string (defaults to **break**
)
arg – the argument expression (printed after the message)
returns – nil
when continued from the break loop
(clean-up)
[LISP] – clean-up after an error
returns – never returns
(top-level)
[LISP] – clean-up after an error and return to the top level
returns – never returns
(continue)
[LISP] – continue from a correctable error
returns – never returns
(errset _expr_ [_pflag_])
[LISP] – trap errors
expr – the expression to execute
pflag – flag to control printing of the error message
returns – the value of the last expression consed with nil
or nil
on error
(baktrace [_n_])
[LISP] – print n levels of trace back information
n – the number of levels (defaults to all levels)
returns – nil
(evalhook _expr_ _ehook_ _ahook_ [_env_])
[LISP] – evaluate with hooks
expr – the expression to evaluate
ehook – the value for *evalhook*
ahook – the value for *applyhook*
env – the environment (default is nil
)
returns – the result of evaluating the expression
profile(_flag_)
[SAL](profile _flag_)
[LISP] – turn profiling on or off.
flag – nil
turns profiling off, otherwise on
returns – the previous state of profiling.
This is not part of standard XLISP.
Arithmetic Functions
truncate(_expr_)
[SAL](truncate _expr_)
[LISP] – truncates a floating point number to an integer
expr – the number
returns – the result of truncating the number
float(_expr_)
[SAL](float _expr_)
[LISP] – converts an integer to a floating point number
expr – the number
returns – the result of floating the integer
(+ _expr_...)
[LISP] – add a list of numbers
expr – the numbers
returns – the result of the addition
(- _expr_...)
[LISP] – subtract a list of numbers or negate a single number
expr – the numbers
returns – the result of the subtraction
(* _expr_...)
[LISP] – multiply a list of numbers
expr – the numbers
returns – the result of the multiplication
(/ _expr_...)
[LISP] – divide a list of numbers
expr – the numbers
returns – the result of the division
(1+ _expr_)
[LISP] – add one to a number
expr – the number
returns – the number plus one
(1- _expr_)
[LISP] – subtract one from a number
expr – the number
returns – the number minus one
rem(_expr_...)
[SAL](rem _expr_...)
[LISP] – remainder of a list of numbers
expr – the numbers
returns – the result of the remainder operation
min(_expr_...)
[SAL](min _expr_...)
[LISP] – the smallest of a list of numbers
expr – the expressions to be checked
returns – the smallest number in the list
max(_expr_...)
[SAL](max _expr_...)
[LISP] – the largest of a list of numbers
expr – the expressions to be checked
returns – the largest number in the list
abs(_expr_)
[SAL](abs _expr_)
[LISP] – the absolute value of a number
expr – the number
returns – the absolute value of the number
gcd(_n1_, _n2_...)
[SAL](gcd _n1_ _n2_...)
[LISP] – compute the greatest common divisor
n1 – the first number (integer)
n2 – the second number(s) (integer)
returns – the greatest common divisor
random(_n_)
[SAL](random _n_)
[LISP] – compute a random number between 0 and |n|-1 inclusive. If n is 0, return 0.
n – the upper bound (integer)
returns – a random number
rrandom()
[SAL](rrandom)
[LISP] – compute a random real number between 0 and 1 inclusive
returns – a random floating point number
random-seed(_n_)
[SAL](random-seed _n_)
[LISP] – seed the random number generator with starting seed n. If random-seed
is not called, sranddev
or some other initialization method will be used by default.
n – the upper bound (integer)
returns – a random number
sin(_expr_)
[SAL](sin _expr_)
[LISP] – compute the sine of a number
expr – the floating point number
returns – the sine of the number
cos(_expr_)
[SAL](cos _expr_)
[LISP] – compute the cosine of a number
expr – the floating point number
returns – the cosine of the number
tan(_expr_)
[SAL](tan _expr_)
[LISP] – compute the tangent of a number
expr – the floating point number
returns – the tangent of the number
atan(_expr_ [, _expr2_])
[SAL](atan _expr_ [_expr2_])
[LISP] – compute the arctangent
expr – the value of x
expr2 – the value of y (default value is 1.0)
returns – the arctangent of x/y
This is not part of standard XLISP.
expt(_x-expr_, _y-expr_)
[SAL](expt _x-expr_ _y-expr_)
[LISP] – compute x to the y power
x-expr – the floating point number
y-expr – the floating point exponent
returns – x to the y power
exp(_x-expr_)
[SAL](exp _x-expr_)
[LISP] – compute e to the x power
x-expr – the floating point number
returns – e to the x power
sqrt(_expr_)
[SAL](sqrt _expr_)
[LISP] – compute the square root of a number
expr – the floating point number
returns – the square root of the number
(< _n1_ _n2_...)
[LISP] – test for less than(<= _n1_ _n2_...)
[LISP] – test for less than or equal to(= _n1_ _n2_...)
[LISP] – test for equal to(/= _n1_ _n2_...)
[LISP] – test for not equal to(>= _n1_ _n2_...)
[LISP] – test for greater than or equal to(> _n1_ _n2_...)
[LISP] – test for greater than
n1 – the first number to compare
n2 – the second number to compare
returns – t
if all arguments are numbers and the results of comparing n1 with n2,
n2 with n3, etc., are all true. (FIXNUMS are converted to FLONUMS in mixed-type comparisons.)
Bitwise Logical Functions
logand(_expr_...)
[SAL](logand _expr_...)
[LISP] – the bitwise and of a list of numbers
expr – the numbers
returns – the result of the and operation
logior(_expr_...)
[SAL](logior _expr_...)
[LISP] – the bitwise inclusive or of a list of numbers
expr – the numbers
returns – the result of the inclusive or operation
logxor(_expr_...)
[SAL](logxor _expr_...)
[LISP] – the bitwise exclusive or of a list of numbers
expr – the numbers
returns – the result of the exclusive or operation
lognot(_expr_)
[SAL](lognot _expr_)
[LISP] – the bitwise not of a number
expr – the number
returns – the bitwise inversion of number
String Functions
string(_expr_)
[SAL](string _expr_)
[LISP] – make a string from a value
expr – an integer (which is first converted into its ASCII character value), string, character, or symbol
returns – the string representation of the argument
string-search(_pat_, _str_, start: _start_, end: _end_)
[SAL](string-search _pat_ _str_ &key :start :end)
[LISP] – search for pattern in string
pat – a string to search for
str – the string to be searched
:start – the starting offset in str
:end – the ending offset + 1
returns – index of pat in str or NIL if not found
This is not part of standard XLISP.
string-trim(_bag_, _str_)
[SAL](string-trim _bag_ _str_)
[LISP] – trim both ends of a string
bag – a string containing characters to trim
str – the string to trim
returns – a trimed copy of the string
string-left-trim(_bag_, _str_)
[SAL](string-left-trim _bag_ _str_)
[LISP] – trim the left end of a string
bag – a string containing characters to trim
str – the string to trim
returns – a trimed copy of the string
string-right-trim(_bag_, _str_)
[SAL](string-right-trim _bag_ _str_)
[LISP] – trim the right end of a string
bag – a string containing characters to trim
str – the string to trim
returns – a trimed copy of the string
string-upcase(_str_, start: _start_, end: _end_)
[SAL](string-upcase _str_ &key :start :end)
[LISP] – convert to uppercase
str – the string
:start – the starting offset
:end – the ending offset + 1
returns – a converted copy of the string
string-downcase(_str_, start: _start_, end: _end_)
[SAL](string-downcase _str_ &key :start :end)
[LISP] – convert to lowercase
str – the string
:start – the starting offset
:end – the ending offset + 1
returns – a converted copy of the string
nstring-upcase(_str_, start: _start_, end: _end_)
[SAL](nstring-upcase _str_ &key :start :end)
[LISP] – convert to uppercase
str – the string
:start – the starting offset
:end – the ending offset + 1
returns – the converted string (not a copy)
nstring-downcase(_str_, start: _start_, end: _end_)
[SAL](nstring-downcase _str_ &key :start :end)
[LISP] – convert to lowercase
str – the string
:start – the starting offset
:end – the ending offset + 1
returns – the converted string (not a copy)
strcat(_expr_...)
[SAL](strcat _expr_...)
[LISP] – concatenate strings
expr – the strings to concatenate
returns – the result of concatenating the strings
subseq(_string_, _start_ [, _end_])
[SAL](subseq _string_ _start_ [_end_])
[LISP] – extract a substring
string – the string
start – the starting position (zero origin)
end – the ending position + 1 (defaults to end)
returns – substring between start and end
string<(_str1_, _str2_, start1: _start1_, end1: _end1_, start2: _start2_, end2: _end2_)
[SAL](string< _str1_ _str2_ &key :start1 :end1 :start2 :end2)
[LISP]string<=(_str1_, _str2_, start1: _start1_, end1: _end1_, start2: _start2_, end2: _end2_)
[SAL](string<= _str1_ _str2_ &key :start1 :end1 :start2 :end2)
[LISP]string=(_str1_, _str2_, start1: _start1_, end1: _end1_, start2: _start2_, end2: _end2_)
[SAL](string= _str1_ _str2_ &key :start1 :end1 :start2 :end2)
[LISP]string/=(_str1_, _str2_, start1: _start1_, end1: _end1_, start2: _start2_, end2: _end2_)
[SAL](string/= _str1_ _str2_ &key :start1 :end1 :start2 :end2)
[LISP]string>=(_str1_, _str2_, start1: _start1_, end1: _end1_, start2: _start2_, end2: _end2_)
[SAL](string>= _str1_ _str2_ &key :start1 :end1 :start2 :end2)
[LISP]string>(_str1_, _str2_, start1: _start1_, end1: _end1_, start2: _start2_, end2: _end2_)
[SAL](string> _str1_ _str2_ &key :start1 :end1 :start2 :end2)
[LISP]
str1 – the first string to compare
str2 – the second string to compare
:start1 – first substring starting offset
:end1 – first substring ending offset + 1
:start2 – second substring starting offset
:end2 – second substring ending offset + 1
returns – t
if predicate is true, nil
otherwise
Note: case is significant with these comparison functions.
string-lessp(_str1_, _str2_, start1: _start1_, end1: _end1_, start2: _start2_, end2: _end2_)
[SAL](string-lessp _str1_ _str2_ &key :start1 :end1 :start2 :end2)
[LISP]string-not-greaterp(_str1_, _str2_, start1: _start1_, end1: _end1_, start2: _start2_, end2: _end2_)
[SAL](string-not-greaterp _str1_ _str2_ &key :start1 :end1 :start2 :end2)
[LISP]string-equal(_str1_, _str2_, start1: _start1_, end1: _end1_, start2: _start2_, end2: _end2_)
[SAL](string-equal _str1_ _str2_ &key :start1 :end1 :start2 :end2)
[LISP]string-not-equal(_str1_, _str2_, start1: _start1_, end1: _end1_, start2: _start2_, end2: _end2_)
[SAL](string-not-equal _str1_ _str2_ &key :start1 :end1 :start2 :end2)
[LISP]string-not-lessp(_str1_, _str2_, start1: _start1_, end1: _end1_, start2: _start2_, end2: _end2_)
[SAL](string-not-lessp _str1_ _str2_ &key :start1 :end1 :start2 :end2)
[LISP]string-greaterp(_str1_, _str2_, start1: _start1_, end1: _end1_, start2: _start2_, end2: _end2_)
[SAL](string-greaterp _str1_ _str2_ &key :start1 :end1 :start2 :end2)
[LISP]
str1 – the first string to compare
str2 – the second string to compare
:start1 – first substring starting offset
:end1 – first substring ending offset + 1
:start2 – second substring starting offset
:end2 – second substring ending offset + 1
returns – t
if predicate is true, nil
otherwise
Note: case is not significant with these comparison functions.
Character Functions
char(_string_, _index_)
[SAL](char _string_ _index_)
[LISP] – extract a character from a string
string – the string
index – the string index (zero relative)
returns – the ascii code of the character
upper-case-p(_chr_)
[SAL](upper-case-p _chr_)
[LISP] – is this an upper case character?
chr – the character
returns – t
if the character is upper case, nil
otherwise
lower-case-p(_chr_)
[SAL](lower-case-p _chr_)
[LISP] – is this a lower case character?
chr – the character
returns – t
if the character is lower case, nil
otherwise
both-case-p(_chr_)
[SAL](both-case-p _chr_)
[LISP] – is this an alphabetic (either case) character?
chr – the character
returns – t
if the character is alphabetic, nil
otherwise
digit-char-p(_chr_)
[SAL](digit-char-p _chr_)
[LISP] – is this a digit character?
chr – the character
returns – the digit weight if character is a digit, nil
otherwise
char-code(_chr_)
[SAL](char-code _chr_)
[LISP] – get the ascii code of a character
chr – the character
returns – the ascii character code (integer)
code-char(_code_)
[SAL](code-char _code_)
[LISP] – get the character with a specified ascii code
code – the ascii code (integer)
returns – the character with that code or nil
char-upcase(_chr_)
[SAL](char-upcase _chr_)
[LISP] – convert a character to upper case
chr – the character
returns – the upper case character
char-downcase(_chr_)
[SAL](char-downcase _chr_)
[LISP] – convert a character to lower case
chr – the character
returns – the lower case character
digit-char(_n_)
[SAL](digit-char _n_)
[LISP] – convert a digit weight to a digit
n – the digit weight (integer)
returns – the digit character or nil
char-int(_chr_)
[SAL](char-int _chr_)
[LISP] – convert a character to an integer
chr – the character
returns – the ascii character code
int-char(_int_)
[SAL](int-char _int_)
[LISP] – convert an integer to a character
int – the ascii character code
returns – the character with that code
char<(_chr1_, _chr2_...)
[SAL](char< _chr1_ _chr2_...)
[LISP]char<=(_chr1_, _chr2_...)
[SAL](char<= _chr1_ _chr2_...)
[LISP]char=(_chr1_, _chr2_...)
[SAL](char= _chr1_ _chr2_...)
[LISP]char/=(_chr1_, _chr2_...)
[SAL](char/= _chr1_ _chr2_...)
[LISP]char>=(_chr1_, _chr2_...)
[SAL](char>= _chr1_ _chr2_...)
[LISP]char>(_chr1_, _chr2_...)
[SAL](char> _chr1_ _chr2_...)
[LISP]
chr1 – the first character to compare
chr2 – the second character(s) to compare
returns – t
if predicate is true, nil
otherwise
Note: case is significant with these comparison functions.
char-lessp(_chr1_, _chr2_...)
[SAL](char-lessp _chr1_ _chr2_...)
[LISP]char-not-greaterp(_chr1_, _chr2_...)
[SAL](char-not-greaterp _chr1_ _chr2_...)
[LISP]char-equal(_chr1_, _chr2_...)
[SAL](char-equal _chr1_ _chr2_...)
[LISP]char-not-equal(_chr1_, _chr2_...)
[SAL](char-not-equal _chr1_ _chr2_...)
[LISP]char-not-lessp(_chr1_, _chr2_...)
[SAL](char-not-lessp _chr1_ _chr2_...)
[LISP]char-greaterp(_chr1_, _chr2_...)
[SAL](char-greaterp _chr1_ _chr2_...)
[LISP]
chr1 – the first string to compare
chr2 – the second string(s) to compare
returns – t
if predicate is true, nil
otherwise
Note: case is not significant with these comparison functions.
Input/Output Functions
read([_stream_ [, _eof_ [, _rflag_]]])
[SAL](read [_stream_ [_eof_ [_rflag_]]])
[LISP] – read an expression
stream – the input stream (default is standard input)
eof – the value to return on end of file (default is nil
)
rflag – recursive read flag (default is nil
)
returns – the expression read
(print _expr_ [_stream_])
[LISP] – print an expression on a new line
expr – the expression to be printed
stream – the output stream (default is standard output)
returns – the expression
(display _label_ _expr_...)
[LISP] – print expressions and their values
label – a string prefix to print, often the current function name or something to identify the source of this line of output
expr – a list of expressions. Each expression is printed literally without evaluation, then it is evaluated and the value is printed. This helps to generate readable debugging output such as "In foo: A = 4, B = 2.5"
returns – the expression
Note: Output from
display
can be turned on and off by calling
display-on
or
display-off
as described below. This is not part of standard XLISP nor is it built-in. Nyquist defines it though.
(display-on)
[LISP] – enable display macro
returns – T
Note: This call gives
display
its default definition as described above. This is not part of standard XLISP nor is it built-in. Nyquist defines it though.
(display-off)
[LISP] – disable display macro
returns – NIL
Note: This call redefines
display
to just evaluate arguments and not print anything.
This is not part of standard XLISP nor is it built-in. Nyquist defines it though.
prin1(_expr_ [, _stream_])
[SAL](prin1 _expr_ [_stream_])
[LISP] – print an expression
expr – the expression to be printed
stream – the output stream (default is standard output)
returns – the expression
princ(_expr_ [, _stream_])
[SAL](princ _expr_ [_stream_])
[LISP] – print an expression without quoting
expr – the expressions to be printed
stream – the output stream (default is standard output)
returns – the expression
pprint(_expr_ [, _stream_])
[SAL](pprint _expr_ [_stream_])
[LISP] – pretty print an expression
expr – the expressions to be printed
stream – the output stream (default is standard output)
returns – the expression
terpri([_stream_])
[SAL](terpri [_stream_])
[LISP] – terminate the current print line
stream – the output stream (default is standard output)
returns – nil
flatsize(_expr_)
[SAL](flatsize _expr_)
[LISP] – length of printed representation using prin1
expr – the expression
returns – the length
flatc(_expr_)
[SAL](flatc _expr_)
[LISP] – length of printed representation using princ
expr – the expression
returns – the length
The Format Function
format(_stream_, _fmt_, _arg_...)
[SAL](format _stream_ _fmt_ _arg_...)
[LISP] – do formated
output
stream – the output stream
fmt – the format string
arg – the format arguments
returns – output string if stream is nil
, nil
otherwise
The format string can contain characters that should be copied directly to the output and formatting directives. The formatting directives are:
~A
– print next argument using princ~S
– print next argument using prin1~%
– start a new line~~
– print a tilde character~
– ignore this one newline and white space on the
next line up to the first non-white-space character or newline. This
allows strings to continue across multiple lines
File I/O Functions
Note that files are ordinarily opened as text. Binary files (such as standard midi files) must be opened with open-binary
on non-unix systems.
open(_fname_, direction: _direction_)
[SAL](open _fname_ &key :direction)
[LISP] – open a file stream
fname – the file name string or symbol
:direction – :input or :output (default is :input)
returns – a stream
open-binary(_fname_, direction: _direction_)
[SAL](open-binary _fname_ &key :direction)
[LISP] – open a binary file stream
fname – the file name string or symbol
:direction – :input or :output (default is :input)
returns – a stream
close(_stream_)
[SAL](close _stream_)
[LISP] – close a file stream
stream – the stream
returns – nil
setdir(_path_ [, _verbose_])
[SAL](setdir _path_ [_verbose_])
[LISP] – set current directory
path – the path of the new directory
verbose – print error message if current directory cannot be changed to path
returns – the resulting full path, e.g. (setdir ".") gets the current working directory, or nil
if an error occurs
This is not part of standard XLISP.
listdir(_path_)
[SAL](listdir _path_)
[LISP] – get a directory listing
path – the path of the directory to be listed
returns – list of filenames in the directory
This is not part of standard XLISP.
get-temp-path()
[SAL](get-temp-path)
[LISP] – get a path where a temporary file can be created. Under Windows, this is based on environment variables. If XLISP is running as a sub-process to Java, the environment may not exist, in which case the default result is the unfortunate choice c:\windows\
.
returns – the resulting full path as a string
This is not part of standard XLISP.
get-user()
[SAL](get-user)
[LISP] – get the user ID. In Unix systems (including OS X and Linux), this is the value of the USER environment variable. In Windows, this is currently just “nyquist”, which is also returned if the environment variable cannot be accessed. This function is used to avoid the case of two users creating files of the same name in the same temp directory.
returns – the string naming the user
This is not part of standard XLISP.
find-in-xlisp-path(_filename_)
[SAL](find-in-xlisp-path _filename_)
[LISP] – search the XLISP search path (e.g. XLISPPATH
from the environment) for filename. If filename is not found as is, and there is no file extension, append ".lsp
" to filename and search again. The current directory is not searched.
filename – the name of the file to search for
returns – a full path name to the first occurrence found
This is not part of standard XLISP.
read-char([_stream_])
[SAL](read-char [_stream_])
[LISP] – read a character from a stream
stream – the input stream (default is standard input)
returns – the character
peek-char([_flag_ [, _stream_]])
[SAL](peek-char [_flag_ [_stream_]])
[LISP] – peek at the next character
flag – flag for skipping white space (default is nil
)
stream – the input stream (default is standard input)
returns – the character
write-char(_ch_ [, _stream_])
[SAL](write-char _ch_ [_stream_])
[LISP] – write a character to a stream
ch – the character to write
stream – the output stream (default is standard output)
returns – the character
read-int([_stream_ [, _length_]])
[SAL](read-int [_stream_ [_length_]])
[LISP] – read a binary integer from a stream
stream – the input stream (default is standard input)
length – the length of the integer in bytes (default is 4)
returns – the integer
Note: Integers are assumed to be big-endian (high-order byte first) and
signed, regardless of the platform. To read little-endian format, use a
negative number for the length, e.g. -4 indicates a 4-bytes, low-order
byte first. The file should be opened in binary mode.
write-int(_ch_ [, _stream_ [, _length_]])
[SAL](write-int _ch_ [_stream_ [_length_]])
[LISP] – write a binary integer to a stream
ch – the character to write
stream – the output stream (default is standard output)
length – the length of the integer in bytes (default is 4)
returns – the integer
Note: Integers are assumed to be big-endian (high-order byte first) and
signed, regardless of the platform. To write in little-endian format, use a
negative number for the length, e.g. -4 indicates a 4-bytes, low-order
byte first. The file should be opened in binary mode.
read-float([_stream_ [, _length_]])
[SAL](read-float [_stream_ [_length_]])
[LISP] – read a binary floating-point number from a stream
stream – the input stream (default is standard input)
length – the length of the float in bytes (default is 4, legal values are -4, -8, 4, and 8)
returns – the integer
Note: Floats are assumed to be big-endian (high-order byte first) and
signed, regardless of the platform. To read little-endian format, use a
negative number for the length, e.g. -4 indicates a 4-bytes, low-order
byte first. The file should be opened in binary mode.
write-float(_ch_ [, _stream_ [, _length_]])
[SAL](write-float _ch_ [_stream_ [_length_]])
[LISP] – write a binary floating-point number to a stream
ch – the character to write
stream – the output stream (default is standard output)
length – the length of the float in bytes (default is 4, legal values are -4, -8, 4, and 8)
returns – the integer
Note: Floats are assumed to be big-endian (high-order byte first) and
signed, regardless of the platform. To write in little-endian format, use a
negative number for the length, e.g. -4 indicates a 4-bytes, low-order
byte first. The file should be opened in binary mode.
read-line([_stream_])
[SAL](read-line [_stream_])
[LISP] – read a line from a stream
stream – the input stream (default is standard input)
returns – the string
read-byte([_stream_])
[SAL](read-byte [_stream_])
[LISP] – read a byte from a stream
stream – the input stream (default is standard input)
returns – the byte (integer)
write-byte(_byte_ [, _stream_])
[SAL](write-byte _byte_ [_stream_])
[LISP] – write a byte to a stream
byte – the byte to write (integer)
stream – the output stream (default is standard output)
returns – the byte (integer)
String Stream Functions
These functions operate on unnamed streams. An unnamed output stream collects characters sent to it when it is used as the destination of any output function. The functions get-output-stream-string
and get-output-stream-list
return a string or a list of characters.
An unnamed input stream is setup with the make-string-input-stream
function and returns each character of the string when it is used as the source of any input function.
make-string-input-stream(_str_ [, _start_ [, _end_]])
[SAL](make-string-input-stream _str_ [_start_ [_end_]])
[LISP]
str – the string
start – the starting offset
end – the ending offset + 1
returns – an unnamed stream that reads from the string
make-string-output-stream(_stream_)
[SAL](make-string-output-stream)
[LISP]
returns – an unnamed output stream
get-output-stream-string(_stream_)
[SAL](get-output-stream-string _stream_)
[LISP]
stream – the output stream
returns – the output so far as a string
Note: the output stream is emptied by this function
get-output-stream-list(_stream_)
[SAL](get-output-stream-list _stream_)
[LISP]
stream – the output stream
returns – the output so far as a list
Note: the output stream is emptied by this function
System Functions
Note: the load
function first tries to load a file from the current directory. A .lsp
extension is added if there is not already an alphanumeric extension following a period. If that fails, XLISP searches the path, which is obtained from the XLISPPATH environment variable in Unix and HKEY_LOCAL_MACHINE\SOFTWARE\CMU\Nyquist\XLISPPATH under Win32. (The Macintosh version has no search path.)
get-real-time()
[SAL](get-real-time)
[LISP] – get the time
returns – Time as FLONUM seconds since Jan 1, 1970
Note: Since the time is a large number, you may want to subtract an earlier time to measure a time interval or use a custom format to print enough precision. (See *float-format*
in Section Symbols.)
get-run-time()
[SAL](get-run-time)
[LISP] – get the run time based on number of Lisp expression evaluations. Typically, a computer will use one unit of run time in about 10ms, but this can vary either way and depends on CPU speed.
returns – Time as a FIXNUM.
get-env(_name_)
[SAL](get-env _name_)
[LISP] – get from an environment variable
name – the name of the environment variable
returns – string value of the environment variable, nil
if variable does not exist
(load _fname_ &key :verbose :print)
[LISP] – load a source file
fname – the filename string or symbol
:verbose – the verbose flag (default is t)
:print – the print flag (default is nil
)
returns – the filename
save(_fname_)
[SAL](save _fname_)
[LISP] – save workspace to a file
fname – the filename string or symbol
returns – t
if workspace was written, nil
otherwise
restore(_fname_)
[SAL](restore _fname_)
[LISP] – restore workspace from a file
fname – the filename string or symbol
returns – nil
on failure, otherwise never returns
dribble([_fname_])
[SAL](dribble [_fname_])
[LISP] – create a file with a transcript of a session
fname – file name string or symbol
(if missing, close current transcript)
returns – t
if the transcript is opened, nil
if it is closed
gc()
[SAL](gc)
[LISP] – force garbage collection
returns – nil
expand(_num_)
[SAL](expand _num_)
[LISP] – expand memory by adding segments
num – the number of segments to add
returns – the number of segments added
alloc(_num_)
[SAL](alloc _num_)
[LISP] – change number of nodes to allocate in each segment
num – the number of nodes to allocate
returns – the old number of nodes to allocate
info()
[SAL](info)
[LISP] – show information about memory usage.
returns – nil
room()
[SAL](room)
[LISP] – show memory allocation statistics
returns – nil
type-of(_expr_)
[SAL](type-of _expr_)
[LISP] – returns the type of the expression
expr – the expression to return the type of
returns – nil
if the value is nil
otherwise one of the symbols:
SYMBOL – for symbols
OBJECT – for objects
CONS – for conses
SUBR – for built-in functions
FSUBR – for special forms
CLOSURE – for defined functions
STRING – for strings
FIXNUM – for integers
FLONUM – for floating point numbers
CHARACTER – for characters
FILE-STREAM – for file pointers
UNNAMED-STREAM – for unnamed streams
ARRAY – for arrays
peek(_addrs_)
[SAL](peek _addrs_)
[LISP] – peek at a location in memory
addrs – the address to peek at (integer)
returns – the value at the specified address (integer)
poke(_addrs_, _value_)
[SAL](poke _addrs_ _value_)
[LISP] – poke a value into memory
addrs – the address to poke (integer)
value – the value to poke into the address (integer)
returns – the value
bigendianp()
[SAL](bigendianp)
[LISP] – is this a big-endian machine?
returns – T if this a big-endian architecture, storing the high-order byte of an integer at the lowest byte address of the integer; otherwise, NIL.
This is not part of standard XLISP.
address-of(_expr_)
[SAL](address-of _expr_)
[LISP] – get the address of an xlisp node
expr – the node
returns – the address of the node (integer)
exit()
[SAL](exit)
[LISP] –
exit xlisp. (Note: in Audacity plug-ins, exit
is
undefined because exiting would terminate Audacity.)
returns – never returns
setup-console()
[SAL](setup-console)
[LISP] – set default console attributes
returns – NIL
Note: Under Windows, Nyquist normally starts up in a medium-sized console window with black text and a white background, with a window title of “Nyquist.” This is normally accomplished by calling setup-console
in system.lsp
. In Nyquist, you can avoid this behavior by setting *setup-console*
to NIL in your init.lsp
file. If setup-console
is not called, Nyquist uses standard input and output as is. This is what you want if you are running Nyquist inside of emacs, for example.
echoenabled(_flag_)
[SAL](echoenabled _flag_)
[LISP] – turn console input echoing on or off
flag – T to enable echo, NIL to disable
returns – NIL
Note: This function is only implemented under Linux and Mac OS X. If Nyquist I/O is redirected through pipes,
the Windows version does not echo the input, but the Linux and Mac versions do. You can turn off echoing with
this function. Under windows it is defined to do nothing.
File I/O Functions
Input from a File
To open a file for input, use the open
function with the keyword argument :direction
set to :input
. To open a file for output, use the open
function with the keyword argument :direction
set to :output
. The open
function takes a single required argument which is the name of the file to be opened. This name can be in the form of a string or a symbol. The open
function returns an object of typeFILE-STREAM
if it succeeds in opening the specified file. It returns the value nil
if it fails. In order to manipulate the file, it is necessary to save the value returned by the open
function. This is usually done by assigning it to a variable with the setq
special form or by binding it using let
or let*
. Here is an example:
(setq fp (open "init.lsp" :direction :input))
Evaluating this expression will result in the file init.lsp
being opened. The file object that will be returned by the open
function will be assigned to the variable fp
.
It is now possible to use the file for input. To read an expression from the file, just supply the value of the fp
variable as the optional stream argument to read
.
(read fp)
Evaluating this expression will result in reading the first expression from the file init.lsp
. The expression will be returned as the result of the read
function. More expressions can be read from the file using further calls to the read
function. When there are no more expressions to read, the read
function will return nil
(or whatever value was supplied as the second argument to read
).
Once you are done reading from the file, you should close it. To close the file, use the following expression:
(close fp)
Evaluating this expression will cause the file to be closed.
Output to a File
Writing to a file is pretty much the same as reading from one. You need to open the file first. This time you should use theopen
function to indicate that you will do output to the file. For example:
(setq fp (open "test.dat" :direction :output))
Evaluating this expression will open the file test.dat
for output. If the file already exists, its current contents will be discarded. If it doesn't already exist, it will be created. In any case, a FILE-STREAM
object will be returned by the OPEN
function. This file object will be assigned to the fp
variable.
It is now possible to write to this file by supplying the value of the fp
variable as the optional stream parameter in the print
function.
(print "Hello there" fp)
Evaluating this expression will result in the string “Hello there” being written to the file test.dat
. More data can be written to the file using the same technique.
Once you are done writing to the file, you should close it. Closing an output file is just like closing an input file.
(close fp)
Evaluating this expression will close the output file and make it permanent.
A Slightly More Complicated File Example
This example shows how to open a file, read each Lisp expression from the file and print it. It demonstrates the use of files and the use of the optional stream argument to the read
function.
(do* ((fp (open "test.dat" :direction :input)) (ex (read fp) (read fp))) ((null ex) nil) (print ex))
Previous Section | Next Section (Index) | Table of Contents | Title Page