Lisp - GNU Common Lisp (GCL)



Numbers ( based on book "PCL" by P Seibel) - built-in types :

integers

123 ; the integer one hundred twenty-three
+42 ; the integer forty-two
-42 ; the integer negative forty-two

ratios

3/7 ; the ratio three-sevenths
-1/4 ; the ratio negative one-quarter
-2/8 ; another way to write negative one-quarter
246/2 ; another way to write the integer one hundred twenty-three

floating-point numbers

subtypes of floating-point number:
* short , exponent marker s
*single (default representation), exponent markers are f or e
*double , exponent marker : d
*long, exponent marker l

1.0 ; ==> 1.0
1e0 ; ==> 1.0
1d0 ;==> 1.0d0
123.0 ;==> 123.0
123e0 ;==> 123.0
0.123 ;==> 0.123
.123 ;==> 0.123
123e-3 ;==> 0.123
123E-3 ;==> 0.123
0.123e20 ;==> 1.23e+19

; rationals in bases other than 10 :

#b10101 ; binary number = 21 in decimal base
#o777 ; octal number (legal digits 0-7) = 511 in decimal base
#xDADA ; hexadecimal number (legal digits 0-F or 0-f) = 56026 in decimal base

; rationals in other bases from 2 to 36 with #nR where n is the base (always written in decimal).
; Additional "digits" beyond 9 are taken from the letters A-Z or a-z

#36rABCDEFGHIJKLMNOPQRSTUVWXYZ ; = 8337503854730415241050377135811259267835

complex numbers

Creating using Lisp reader macro #c :
( Standard Macro Characters / Sharpsign C )
The #c reader macro can not evaluate its parameters, so one can not use variables here

#c(2 1) ==> #c(2 1)
#c(2/3 3/4) ==> #c(2/3 3/4)
#c(2 1.0) ==> #c(2.0 1.0)
#c(2.0 1.0d0) ==> #c(2.0d0 1.0d0)
#c(1/2 1.0) ==> #c(0.5 1.0)
#c(3 0) ==> 3
#c(3.0 0.0) ==> #c(3.0 0.0)
#c(1/2 0) ==> 1/2
#c(-6/3 0) ==> -2
(setq z #c(2 1.0)) ; creates z variable and gives it initial value ( here complex number)


Creating using complex function ( here one can use variables , not only numbers) :

(defvar s)
(setq s 600)
(defvar pos)
(setq pos (complex s s))

some operations on complex numbers :

(abs z) ; abs of z



Strings

(setq line "\".**.")

(setq line (concatenate 'string line "a")) ; add a to line



; Run shell command under win XP :
(run-shell-command "cmd /c start cmd /k dir")


(probe-file "foo")

; 3 ways to change line in Lisp :

(terpri); command for printing a carriage-return

; or

(princ #\Newline) ;

; or

(format t "~%") ; stream t is a standard output



Loops :
Common Lisp's Loop Macro Examples for Beginners by Yusuke Shinyama


Packages

; check current package
; checka value of the global variable *PACKAGE* :

*PACKAGE*
#< PACKAGE COMMON-LISP-USER >

change current package to jane package:

(in-package jane)

" The :CL-USER package :uses the :COMMON-LISP package.
The :COMMON-LISP package :exports a ton of functions that you can use in :CL-USER without specifically telling the REPL where to find the functions." ( by macajemianuts )
The COMMON-LISP package has nickname CL.
The COMMON-LISP-USER package is also known as CL-USER. (from Peter Seibel book)


indentation

(pprint '( your_code ))

* output will be all uppercase.
* Your code should be in lower case, so if you use the output of pprint, you should downcase everything.

rules of lisp indentation  by Robert Strandh



Q. How to speed up your program :
A.
1. compile it
2. use "profiling tools to see where time is spent on particular calculations of interest" RJF
3. "In the different lisp implementations, it is possible that a factor of 2 to 10 speedup remains to be obtained." RJF
4. explicitly typing
5. "disassemble the suspicious code"



Some notes on the Lisp heap size  at Franz Inc.





Books :

Tips for beginners from beginner (:-)) :
* check which Lisp version you are using ( here is common lisp )
* check which IDE you use ( I use Emacs with SLIME: The Superior Lisp Interaction Mode for Emacs based on ILISP major mode)
* do not forget about parentheses
* use space instead of comma ( comma is illegal outside of backquote)
(Early mistakes by Nick Levine)
* Hoping that an argument list will destructure for you, or trying to give argument types to a function:
(defun foo (list a b) ....) ; hopes to peel a and b out of a list
* Using a C-like calling syntax: foo(a b) ; should be (foo a b)
* Parentheses around variables:
(foo (a) (b)) ; should be (foo a b)
* Parentheses around strings:
("hello") ; should be "hello"
*Not quoting when you need to suspend evaluation:
(98 99 100) ; attempts to call function called 98
'(98 99 100) ; a list containing three numbers
* Trying an "else" in (if ...)
(if (foo)
(bar)
else (baz)) ; drop the "else"!




Some built-in reader macros:
* #'foo for functions,
* #\\ for literal characters,
* #c(4 3) for complex numbers,
* #p"/path/" for filesystem paths,



Structures :
(defstruct point x y z) ; define structure
(defvar my-point) ; define var
(setf my-point (make-point :x 3 :y 4 :z 12)) ; set value of slots of var
(point-x my-point) ; acces to slots
; See also s# macro



Class :
(defclass 3d-point ()
((x :accessor 3d-point-x)
(y :accessor 3d-point-y)
(z :accessor 3d-point-z))) ; define class

(defparameter *point* (make-instance '3d-point)) ; define var

(setf ( 3d-point-x *point*) 10) ; acces using accessor function



Directories :

( clisp under windows and emacs ) :

CL-USER> *default-pathname-defaults*
#P"C:"

( sbcl under linux and emacs ) :
CL-USER> *default-pathname-defaults*
#P"/home/adam/"

Is there a way to get a list of all environment variables from sbcl?
(sb-ext:posix-environ)


SBCL / Linux
(user-homedir-pathname)

Running SBCL on Windows  by brainrack



emacs init.el file
"Can I then automate that so that I don't have to paste those
three lines in repl every time I open emacs ? "
Haris
"Depends on your CL implementation.  Edit the appropriate file in your home directory (user-homedir-pathname).

Allegro: .clinit.cl or clinit.cl
Clisp: .clisprc.lisp
Clozure: ccl-init.lisp or openmcl-init.lisp
CMUCL: .cmucl-init.lisp or init.lisp
ECL: .eclrc or .ecl
LispWorks: .lispworks
SBCL: .sbclrc

These files are loaded whenever CL starts, with or without emacs (unless they are suppressed by a flag like --norc or --no-userinit).

- Daniel "


; To find the name of the current package :
(package-name *package*)
or :
*package*

To find the package in which a symbol is interned :
(symbol-package 'radiant-lambda)

to find the package using the function :
(FIND-PACKAGE 'gradient-example)





ASDF

asdf:*central-registry*