Land of Lisp 👷🏼♂️👷🏼♀️
Lisp Keywords
Setting Values
defparameter
- defines a mutable global value (can be changed by further calls todefparameter
)defvar
- defines an immutable global value (cannot be changed by further calls todefvar
but can be changed bysetf
)setf
- sets a value, e.g.(setf *my_number* 5)
List Keywords
cons
- creates a list from two items (one of which can be nil)car
- returns the head of a listcdr
- returns the tail of a listlist
- creates a list, e.g.(list 1 2 3)
creates the list(1 2 3)
member
- returns the tail of a list when an item is found e.g.(member 1 '(3 4 1 5))
will return(1 5)
assoc
find
remove-if
remove-if-not
Lists - Higher Order Functions
apply
mapcar
- maps function to each item in list
> (mapcar #'print '(a b c))
A
B
C
maplist
- maps function to a list of each item in list
> (maplist #'print '(a b c))
(A B C)
(B C)
(C)
Maths Keywords
expt
- exponential, e.g.(expt 2 3)
returns 8ash
- arithmetic shift e.g(ash 8 1)
returns 16,(ash 8 -1)
returns 4
Conditional Keywords
if
when
unless
cond
case
and
or
Other Keywords
oddp
- returnt
if odd number,nil
if even, errors if not integerprogn
- run a sequence of expressions, returning the last evaluated expressiony-or-n-p
- prompts for a 'y or n' answer `(y-or-n-p "It is Thursday? "), return T for 'y' and Nil for 'n'- complement - equivalent to "not"
- alphanumericp - returns true for alpha numeric chars, false otherwise
- digit-char-p - returns true if numeric character
- substitute-if [cond] [new item] [list] - generic function can handle multiple datatypes
Reading Keyboard Input
read
- usage:(my_val (read))
- will read numbers, etc. Useful to read structures from files (which have been written by a print statement)read-line
- usage:(my_val (read-line))
- will only read strings and chars
Printing
-
[ ] TODO: confirm how the print statements differ
-
print
prin1
princ
princ-to-string
prin1-to-string
- converts any basic lisp type to a stringwrite-to-string
- writes an expression to a string- :pretty [t/nil] -> adds new lines or tabs into string
Special Chars
#\newline
#\space
#\tab
#\backspace
#\linefeed
#\page
#\return
#\rubout
#\?
#\!
#\.
File Handling
(with-open-file
(my-stream
"testfilename.txt"
:direction :output
:if-exists :supersede
)
(princ "Hello File!" my-strean)
)