Presentation is loading. Please wait.

Presentation is loading. Please wait.

Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.3 Top Ten Things to Know.

Similar presentations


Presentation on theme: "Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.3 Top Ten Things to Know."— Presentation transcript:

1 Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.3 Top Ten Things to Know

2 1. Defining a Function (defun plus (a b) (+ a b)) Function name Argument list Function body (sequence of one or more expressions)

3 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

4 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)

5 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

6 Printing Hello (defun hello (name) (format *standard-output* " Hello, ~A ~%" name)) (hello "Linda") Hello, Linda

7 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)

8 More Local Variables (defun polynomial (a b c x) (let* ((y1 (* a (* x x))) (y2 (* b x)) (y3 c)) (+ y1 y2 y3))) Ax 2 + Bx + C Three local variables

9 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

10 4. Using if (defun sign-name (number) (if (> number 0) ’positive ’not-positive)) Boolean test returns true or NIL (false) (If )

11 Using if, cont’d (defun sign-name (number) (if (> number 0) “positive” (if (= number 0) "zero" "negative")))

12 Compound Tests NOT: (not (> x 3)) AND: (and (> x 3) (< x 10)) OR: (or (> x 3) (< x 0) (= y 7) (< (+ x y) 5 ))

13 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)

14 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

15 6. Assigning Values to Variables In Lisp we use setq or setf instead of =, := etc (setq *todays-temp* 101.5) *todays-temp*  101.5

16 7. Using Symbols (defun color-name (action) (if (eq action 'stop) 'red (if (eq action 'go) 'green 'yellow)))

17 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*))

18 9. Iteration (defun count (N) (dotimes (I N) (format *standard-output* "~A " N))) (defun enumerate (list) (dolist (element list) (format *standard-output* "~A" element)))

19 Iteration (defun sum (list) (let* ((result 0)) (dolist (item list) (setq result (+ result item))) result)) (sum '(3 5 2 4))  14

20 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

21 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

22 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”


Download ppt "Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.3 Top Ten Things to Know."

Similar presentations


Ads by Google