clisp programming language
Learn from examples
1. Program to output an integer, a string,
and a floating point number, initially just one method.
exc1.lisp source code
; excl1.lisp example print string, integer, float
(print "clisp excl1.lisp # running")
#| block comments
(<clisp> <xxx.lisp≷) executes the function operation
and prints a function result
|#
(setq mystr "my string") ; give a string a variable name
(print mystr)
(setq i 7) ; give a integer a variable name
(print "i 7 =")
(print i)
(print "increment i")
(setq i (+ i 1))
(print i)
(setq x 76.54) ; give a real a variable name
(setf xx 45.67)
(setq y 1.2345D2) ; D allowed
(setq z 3.456e+4) ; e+ more common
(setq zz 3.456e-4) ; e- OK see how it prints
(print "x 76.54 =")
(print x)
(print "xx 45.67 =")
(print xx)
(print "y 1.234D2 =")
(print y) ; shifted
(print "z 3.456e+4 =")
(print z) ; shifted
(print "zz 3.456e-4 =")
(print zz) ; same
(print "(+ x y) =") ; using variables set above
(print (+ x y))
(print "(+ x z) =")
(print (+ x z))
(print "(+ z zz} =")
(print (+ z zz))
(print "(+ 2 3) =") ; operate on constants
(write (+ 2 3))
(print "(newline) is not defined");
(print " ")
(print "two blank lines")
(FORMAT T "~% ~%" #\return #\linefeed ) NIL
(print "end blank lines")
; a list, rather than an operation starts with '(
(print '("text" 123 3.14159))
(print "(cons 123 (cons 3.14159 nil))=" )
(print (cons 123 (cons 3.14159 nil)) )
(print "why? (cons 123 3.14159)=" )
(print (cons 123 3.14159) )
(print (format t "string=~s int=~d real=~f" "abc" 123 3.24))
(print "T stands for true, NIL stands for false")
(print "(print (eq 2 2))=")
(print (eq 2 2))
(print "(print (eq 2 3))=")
(print (eq 2 3))
(print "excl1.lisp finished")
Output from execution:
"clisp excl1.lisp # running"
"(+ 2 3)=" 5
"(newline) is not defined"
" "
"two blank lines"
"end blank lines"
("text" 123 3.14159)
"(cons 123 (cons 3.14159 nil))="
(123 3.14159)
"why? (cons 123 3.14159)="
(123 . 3.14159) string="abc" int=123 real=3.24
NIL
"T stands for true, NIL stands for false"
"(print (eq 2 2))="
T
"(print (eq 2 3))="
NIL
"excl1.lisp finished"
2. commands to execute the source code
at a minimum, Windows, Linux, MacOSX.
Windows: optional write output to file
clisp excl1.lisp clisp excl1.lisp > excl1_lisp.out
Linux:
clisp excl1.lisp
MacOSX
clisp excl1.lisp
3. You must be able to declare variables and arrays
and matrix of various types.
excl3.lisp source code
; excl3.lisp example declare variables, arrays, matrix
(print "clisp excl3.lisp # running")
(setf X 2.0)
(print "X =")
(write X)
(setf A (make-array '(3)))
(setf (aref A 0) 1.0)
(setf (aref A 1) 2.0)
(setf (aref A 2) 3.0)
(print "A= ")
(write A)
(print "(* X (aref A 1)) =")
(write (* X (aref A 1)))
(setf M2 (make-array '(3 3) :initial-contents '((0.0 1.0 2.0)
(3.0 4.0 5.0)
(6.0 7.0 8.0)) ))
(print "M2= ")
(write M2)
(print "print M2= ")
(print M2)
(dotimes (i 3)
(dotimes (j 3)
(print "M2 i j value ")
(write i)
(write " ")
(write j)
(write " ")
(write (aref M2 i j))
)
)
(print "excl3.lisp finished")
Execution output:
"clisp excl3.lisp # running"
"X =" 2.0
"A= " #(1.0 2.0 3.0)
"(* X (aref A 1)) =" 4.0
"M2= " #2A((0.0 1.0 2.0) (3.0 4.0 5.0) (6.0 7.0 8.0))
"print M2= "
#2A((0.0 1.0 2.0) (3.0 4.0 5.0) (6.0 7.0 8.0))
"M2 i j value " 0" "0" "0.0
"M2 i j value " 0" "1" "1.0
"M2 i j value " 0" "2" "2.0
"M2 i j value " 1" "0" "3.0
"M2 i j value " 1" "1" "4.0
"M2 i j value " 1" "2" "5.0
"M2 i j value " 2" "0" "6.0
"M2 i j value " 2" "1" "7.0
"M2 i j value " 2" "2" "8.0
"excl3.lisp finished"
4. You need to be able to have loops, iteration statements
excl4.lisp source code
; excl4.lisp example loops, iteration
(print "clisp excl4.lisp # running")
(print "(loop for i from 1 to 3")
(loop for i from 1 to 3
do (print i)
)
(setf n 3)
(print "n = ")
(write n)
(setf V (make-array n :initial-contents '(0.5 1.5 2.5) ))
(print "(loop for i from 0 to (- n 1) ; C for(i=0; i<n; i++)")
(loop for i from 0 to (- n 1) ; C for(i=0; i<n; i++)
do (print "V[")
(write i)
(write " ]=")
(write (aref V i))
)
(print "excl4.lisp finished")
Execution output:
"clisp excl4.lisp # running"
"(loop for i from 1 to 3"
1
2
3
"n = " 3
"(loop for i from 0 to (- n 1) ; C for(i=0; i<n; i++)"
"V[" 0" ]="0.5
"V[" 1" ]="1.5
"V[" 2" ]="2.5
"excl4.lisp finished"
5. You need if then else conditional statements
excl5.lisp source code
; excl5.lisp example if then, when, case, cond
(print "clisp excl5.lisp # running")
(setq i 2)
(if(≷ i 1) ; = /= ≷= <=
(print "i ≷ 1") ; true part
)
(if(= i 3)
(print "bad i=2") ; true part
(print "else good i |= 3") ; false part
)
(if (and (≷ i 1) (< i 3))
(print "(and (≷ i 1) (< i 3))")) ; end if
(if (or (/= i 3) (≷= i 0))
(print "(or (/= i 3 ) (≷= i 0 ))"))
(if (not (and (≷ i 3) (≷= i 3)))
(print "((not (and (≷ i 3) (≷= i 3)))"))
; when
; cond
; case
(print "excl5.lisp finished")
Execution output:
6. You need to be able to create functions, procedures,
subroutines.
excl6.lisp source code
Execution output:
7. You need to be able to read and write files in various formats.
excl7.lisp source code
Execution output:
8. You need to be able to use a number of files combined to
build a program. This may include packages, libraries,
operating system commands, header files, etc.
excl8.lisp source code
Execution output:
Last updated 7/27/2019