![Page 1: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.3 Top Ten Things to Know](https://reader030.vdocuments.us/reader030/viewer/2022032706/56649dfe5503460f94ae5df2/html5/thumbnails/1.jpg)
Allegro CL Certification Program
Lisp Programming Series Level I
Session 1.1.3
Top Ten Things to Know
![Page 2: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.3 Top Ten Things to Know](https://reader030.vdocuments.us/reader030/viewer/2022032706/56649dfe5503460f94ae5df2/html5/thumbnails/2.jpg)
1. Defining a Function
(defun plus (a b)
(+ a b))
• Function name
• Argument list
• Function body (sequence of one or more expressions)
![Page 3: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.3 Top Ten Things to Know](https://reader030.vdocuments.us/reader030/viewer/2022032706/56649dfe5503460f94ae5df2/html5/thumbnails/3.jpg)
Calling a function(plus 3 5)
(defun mean (a b)
(/ (plus a b) 2))
• First: call PLUS to add a to b
• Second: divide the result by 2
![Page 4: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.3 Top Ten Things to Know](https://reader030.vdocuments.us/reader030/viewer/2022032706/56649dfe5503460f94ae5df2/html5/thumbnails/4.jpg)
2. Printing Output
(defun print-mean (a b)
(format *standard-output*
"~A ~%"
(mean a b)))
• First argument is the stream• Second argument is the “control string”• Third argument is the value to be printed
– ~A takes the argument and prints it– ~% prints a newline– (Analogous to printf in C)
![Page 5: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.3 Top Ten Things to Know](https://reader030.vdocuments.us/reader030/viewer/2022032706/56649dfe5503460f94ae5df2/html5/thumbnails/5.jpg)
Common Format Control Strings• ~A prints any value (strings w/o double quotes)• ~S prints any value (strings w/ double quotes)• ~5F prints a float in a 5 character wide field• ~% prints a newline• Combine them to print many things:
(format *standard-output* “~A ~5F ~A ~%” 5 pi 10)
5 3.142 10
![Page 6: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.3 Top Ten Things to Know](https://reader030.vdocuments.us/reader030/viewer/2022032706/56649dfe5503460f94ae5df2/html5/thumbnails/6.jpg)
Printing Hello
(defun hello (name)
(format *standard-output*
" Hello, ~A ~%"
name))
(hello "Linda")
Hello, Linda
![Page 7: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.3 Top Ten Things to Know](https://reader030.vdocuments.us/reader030/viewer/2022032706/56649dfe5503460f94ae5df2/html5/thumbnails/7.jpg)
3. Using Local Variables(defun mean (a b)
(let* ((sum (+ a b)))
(/ sum 2)))
• LET* creates local variables
• Example creates one named SUM
• Initial value is result of (+ a b)
![Page 8: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.3 Top Ten Things to Know](https://reader030.vdocuments.us/reader030/viewer/2022032706/56649dfe5503460f94ae5df2/html5/thumbnails/8.jpg)
More Local Variables(defun polynomial (a b c x)
(let* ((y1 (* a (* x x)))
(y2 (* b x))
(y3 c))
(+ y1 y2 y3)))
• Ax2 + Bx + C
• Three local variables
![Page 9: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.3 Top Ten Things to Know](https://reader030.vdocuments.us/reader030/viewer/2022032706/56649dfe5503460f94ae5df2/html5/thumbnails/9.jpg)
Avoid this Mistake(defun polynomial (a b c x)
(setq y1 (* a (* x x)))
(setq y2 (* b x))
(setq y3 c))
(+ y1 y2 y3))
;;; y1, y2, y3 become globals
;;; compiler will give warnings
![Page 10: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.3 Top Ten Things to Know](https://reader030.vdocuments.us/reader030/viewer/2022032706/56649dfe5503460f94ae5df2/html5/thumbnails/10.jpg)
4. Using if(defun sign-name (number)
(if (> number 0)
’positive
’not-positive))
• Boolean test returns true or NIL (false)
• (If <test> <then> <else>)
![Page 11: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.3 Top Ten Things to Know](https://reader030.vdocuments.us/reader030/viewer/2022032706/56649dfe5503460f94ae5df2/html5/thumbnails/11.jpg)
Using if, cont’d (defun sign-name (number)
(if (> number 0)
“positive”
(if (= number 0)
"zero"
"negative")))
![Page 12: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.3 Top Ten Things to Know](https://reader030.vdocuments.us/reader030/viewer/2022032706/56649dfe5503460f94ae5df2/html5/thumbnails/12.jpg)
Compound Tests
• NOT: (not (> x 3))
• AND: (and (> x 3) (< x 10))
• OR: (or (> x 3) (< x 0) (= y 7) (< (+ x y) 5 ))
![Page 13: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.3 Top Ten Things to Know](https://reader030.vdocuments.us/reader030/viewer/2022032706/56649dfe5503460f94ae5df2/html5/thumbnails/13.jpg)
Other Types of Tests
• Numeric comparisons: >, >=, <, <=, =
• Equality of objects: EQ, EQL, EQUAL
• Equality of strings: string=, string-equal– (string-equal "Radar" "RADAR")
• Type tests:– (typep x 'integer)
![Page 14: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.3 Top Ten Things to Know](https://reader030.vdocuments.us/reader030/viewer/2022032706/56649dfe5503460f94ae5df2/html5/thumbnails/14.jpg)
5. Global Variables and Constants
• (defvar *website* "www.whitehouse.gov")– *website* is the symbol– Naming convention is to use asterisk in names
of global variables• (defconstant +timeout+ 60.0)
– +timeout+ is the symbol– Naming convention is to use + in names of
program constants
![Page 15: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.3 Top Ten Things to Know](https://reader030.vdocuments.us/reader030/viewer/2022032706/56649dfe5503460f94ae5df2/html5/thumbnails/15.jpg)
6. Assigning Values to Variables
• In Lisp we use setq or setf instead of =, := etc
(setq *todays-temp* 101.5)
*todays-temp* 101.5
![Page 16: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.3 Top Ten Things to Know](https://reader030.vdocuments.us/reader030/viewer/2022032706/56649dfe5503460f94ae5df2/html5/thumbnails/16.jpg)
7. Using Symbols
(defun color-name (action)
(if (eq action 'stop)
'red
(if (eq action 'go)
'green
'yellow)))
![Page 17: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.3 Top Ten Things to Know](https://reader030.vdocuments.us/reader030/viewer/2022032706/56649dfe5503460f94ae5df2/html5/thumbnails/17.jpg)
8. Using Lists
(defvar *abbreviations* '((bos “Boston”) (lga “NY/LaGuardia”) (sfo “San Francisco”)))
(defun decode-abbreviation (symbol) (second (assoc symbol *abbreviations*)))
(defun add-abbreviation (symbol str) (push (list symbol str) *abbreviations*))
![Page 18: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.3 Top Ten Things to Know](https://reader030.vdocuments.us/reader030/viewer/2022032706/56649dfe5503460f94ae5df2/html5/thumbnails/18.jpg)
9. Iteration(defun count (N)
(dotimes (I N)
(format *standard-output* "~A " N)))
(defun enumerate (list)
(dolist (element list)
(format *standard-output* "~A"
element)))
![Page 19: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.3 Top Ten Things to Know](https://reader030.vdocuments.us/reader030/viewer/2022032706/56649dfe5503460f94ae5df2/html5/thumbnails/19.jpg)
Iteration(defun sum (list)
(let* ((result 0))
(dolist (item list)
(setq result (+ result item)))
result))
(sum '(3 5 2 4)) 14
![Page 20: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.3 Top Ten Things to Know](https://reader030.vdocuments.us/reader030/viewer/2022032706/56649dfe5503460f94ae5df2/html5/thumbnails/20.jpg)
10. Packages• A Lisp package is a namespace for related
functionality– COMMON-LISP: a package for ANSI
Common Lisp; you can’t add to it or change it– USER: a package for you to play around in (it’s
also the default or initial package)– CG-USER
• Large applications should use their own package
![Page 21: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.3 Top Ten Things to Know](https://reader030.vdocuments.us/reader030/viewer/2022032706/56649dfe5503460f94ae5df2/html5/thumbnails/21.jpg)
Package Example(defpackage math
(:use common-lisp))
(in-package :math)
(defun square (x) (* x x))
• The symbol SQUARE now exists in the MATH package
• The math package “uses” or inherits from the common-lisp package
![Page 22: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.3 Top Ten Things to Know](https://reader030.vdocuments.us/reader030/viewer/2022032706/56649dfe5503460f94ae5df2/html5/thumbnails/22.jpg)
Packages
• Every symbols exists in a package– *package* will return the current package– in-package will change the package
• Default package in the IDE is
“COMMON-GRAPHICS-USER”