functional programming 07 symbols and numbers. symbols - symbol names a symbol can have any string...

26
Functional Programming 07 Symbols and Numbers

Upload: kathryn-mathews

Post on 11-Jan-2016

228 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Functional Programming 07 Symbols and Numbers. Symbols - Symbol Names A symbol can have any string as its name ▫ > (symbol-name ‘abc) “ABC” ▫ The name

Functional Programming07 Symbols and Numbers

Page 2: Functional Programming 07 Symbols and Numbers. Symbols - Symbol Names A symbol can have any string as its name ▫ > (symbol-name ‘abc) “ABC” ▫ The name

Symbols - Symbol Names

•A symbol can have any string as its name▫> (symbol-name ‘abc)

“ABC”▫The name of the symbol is all uppercase letters▫Common Lisp is not case-sensitive▫> (eql ‘aBc ‘Abc)

T▫> (CaR ‘(a b c)

A

Page 3: Functional Programming 07 Symbols and Numbers. Symbols - Symbol Names A symbol can have any string as its name ▫ > (symbol-name ‘abc) “ABC” ▫ The name

Symbols - Symbol Names

•Use vertical bars to denote a symbol▫> (list ‘|Lisp 1.5| ‘|| ‘|abc| ‘|ABC|)

(|Lisp 1.5| || |abc| |ABC|)▫When the name of such a symbol is read, there is no

case conversion▫> (symbol-name ‘|a b c|)

“a b c”

Page 4: Functional Programming 07 Symbols and Numbers. Symbols - Symbol Names A symbol can have any string as its name ▫ > (symbol-name ‘abc) “ABC” ▫ The name

Symbols - Property Lists• Every symbol has a property list (plist)• > (get ‘alizarin ‘color)

NIL• > (setf (get ‘alizarin ‘color) ‘red)

RED• > (get ‘alizarin ‘color)

RED• > (setf (get ‘alizarin ‘transparency) ‘high)

HIGH• > (symbol-plist ‘alizarin)

(TRANSPARENCY HIGH COLOR RED)

Page 5: Functional Programming 07 Symbols and Numbers. Symbols - Symbol Names A symbol can have any string as its name ▫ > (symbol-name ‘abc) “ABC” ▫ The name

Symbols - Symbols Are Big•Symbol is a substantial object

Page 6: Functional Programming 07 Symbols and Numbers. Symbols - Symbol Names A symbol can have any string as its name ▫ > (symbol-name ‘abc) “ABC” ▫ The name

Symbols - Symbols Are Big

•Symbols are real objects, not just names•When two variables are set to the same symbol, it’s

the same as when two variables are set to the same list▫Both variables have pointers to the same object

Page 7: Functional Programming 07 Symbols and Numbers. Symbols - Symbol Names A symbol can have any string as its name ▫ > (symbol-name ‘abc) “ABC” ▫ The name

Symbols - Creating Symbols

•Packages are symbol-tables, mapping names to symbols

•Every ordinary symbol belongs to a particular package

•A symbol that belongs to a package is said to be interned in that package

•The first time you type the name of a new symbol▫Lisp will create a new symbol object▫Lisp will intern it in the current package

Page 8: Functional Programming 07 Symbols and Numbers. Symbols - Symbol Names A symbol can have any string as its name ▫ > (symbol-name ‘abc) “ABC” ▫ The name

Symbols - Multiple Packages

•Larger programs are often divided up into multiple packages

• If each part of a program is in its own package, then someone working on one part of the program will be able to use a symbol as the name of a function or variable without worrying that the name is already used elsewhere

Page 9: Functional Programming 07 Symbols and Numbers. Symbols - Symbol Names A symbol can have any string as its name ▫ > (symbol-name ‘abc) “ABC” ▫ The name

Symbols - Multiple Packages• (defpackage :package-test

(:use “COMMON-LISP” “MY-UTILITIES”) (:nicknames :test) (:export :a :b))(in-package :test)▫ Define a new package called my-application▫ Uses two other packages: COMMON-LISP and MY-UTILITIES▫ Nickname is app▫ The my-application package itself exports three symbols: win, lose,

and draw▫ Code in other packages will be able to refer to them as e.g. test:a

Page 10: Functional Programming 07 Symbols and Numbers. Symbols - Symbol Names A symbol can have any string as its name ▫ > (symbol-name ‘abc) “ABC” ▫ The name

Symbols - Multiple Packages• > (make-package :bob) #<Package BOB>• > (make-package :jane) #<Package JANE>• > (in-package :bob) #<Package BOB>• > (setf a 33) 33• > a

33• > (in-package :jane) #<Package JANE>• > (setf a 55) 55• > a

55

Page 11: Functional Programming 07 Symbols and Numbers. Symbols - Symbol Names A symbol can have any string as its name ▫ > (symbol-name ‘abc) “ABC” ▫ The name

Symbols - Multiple Packages

• If Jane wants to use a function written by Bob▫> (in-package :jane) #<Package JANE>▫> bob::a

33

Page 12: Functional Programming 07 Symbols and Numbers. Symbols - Symbol Names A symbol can have any string as its name ▫ > (symbol-name ‘abc) “ABC” ▫ The name

Symbols - Keywords

•Symbols in the keyword package (keywords)▫They always evaluate to themselves▫You can refer to them anywhere simply as :x, instead of

keyword:x, e.g. (member ‘(a) ‘((a) (z)) :test #’equal)▫(defun noise (animal)

(case animal (:dog :woof) (:cat :meow) (:pig :oink)))

Page 13: Functional Programming 07 Symbols and Numbers. Symbols - Symbol Names A symbol can have any string as its name ▫ > (symbol-name ‘abc) “ABC” ▫ The name

Symbols - Example: Random Text• If you are going to write programs that operate on

words, it’s often a good idea to use symbols instead of strings▫Symbols can be compared in one step with eql▫Strings have to be compared charater-by-character with

string-equal or string=•> (read-text “..\\test\\test.txt”)•> (hash-table-count *words*)•> (generate-text 100)

Page 14: Functional Programming 07 Symbols and Numbers. Symbols - Symbol Names A symbol can have any string as its name ▫ > (symbol-name ‘abc) “ABC” ▫ The name

Symbols - Example: Random Text(defparameter *words* (make-hash-table :size 10000))(defconstant maxword 100)(defun read-text (pathname) (with-open-file (s pathname :direction :input) (let ((buffer (make-string maxword)) (pos 0)) (do ((c (read-char s nil :eof) (read-char s nil :eof))) ((eql c :eof)) (if (or (alpha-char-p c) (char= c #\’)) (progn (setf (aref buffer pos) c) (incf pos)) (progn (unless (zerop pos) (see (intern (string-downcase

(subseq buffer 0 pos)))) (setf pos 0)) (let ((p (punc c))) (if p (see p)))))))))

Page 15: Functional Programming 07 Symbols and Numbers. Symbols - Symbol Names A symbol can have any string as its name ▫ > (symbol-name ‘abc) “ABC” ▫ The name

Symbols - Example: Random Text

(defun punc (c) (case c (#\. ‘|.|) (#\, ‘|,|) (#\; ‘|;|) (#\! ‘|!|) (#\? ‘|?|) ))(let ((prev ‘|.|)) (defun see (symb) (let ((pair (assoc symb (gethash prev *words*)))) (if (null pair) (push (cons symb 1) (gethash prev *words*)) (incf (cdr pair)))) (setf prev symb)))

Page 16: Functional Programming 07 Symbols and Numbers. Symbols - Symbol Names A symbol can have any string as its name ▫ > (symbol-name ‘abc) “ABC” ▫ The name

Symbols - Example: Random Text(defun generate-text (n &optional (prev ‘|.|)) (if (zerop n) (terpri) (let ((next (random-next prev))) (format t “~A “ next) (generate-text (1- n) next))))(defun random-next (prev) (let* ((choices (gethash prev *words*)) ( i (random (reduce #’+ choices :key #’cdr )))) (dolist (pair choices) (if (minusp (decf i (cdr pair))) (return (car pair))))))

Page 17: Functional Programming 07 Symbols and Numbers. Symbols - Symbol Names A symbol can have any string as its name ▫ > (symbol-name ‘abc) “ABC” ▫ The name

Term Project

•Due: June 27•Write a program to analyze the input article and

produce:▫The title of the article▫The abstract of the article

•Explain your evaluation strategies to decide whether a title or an abstract is suitable for this article

•Requirements▫Program demo▫Detailed report

Page 18: Functional Programming 07 Symbols and Numbers. Symbols - Symbol Names A symbol can have any string as its name ▫ > (symbol-name ‘abc) “ABC” ▫ The name

Numbers - Types

•Four types of numbers▫Integer 2001▫Floating-point number 253.72 or 2.5372e2 ▫Ratio 2/3▫Complex number #c(a b) is a+bi

•Predicates for numbers▫integerp▫floatp▫complexp

Page 19: Functional Programming 07 Symbols and Numbers. Symbols - Symbol Names A symbol can have any string as its name ▫ > (symbol-name ‘abc) “ABC” ▫ The name

Numbers - Types• Rules for determining what kind of number a computation will

return▫ If a numeric function receives one or more floating-point numbers

as arguments, the return value will be a floating-point number (or a complex number with floating-point components) (+ 1.0 2) evaluates to 3.0 (+ #c(0 1.0) 2) evaluates to #c(2.0 1.0)

▫Ratios that divide evenly will be converted into integers (/ 10 2) returns 5

▫Complex numbers whose imaginary part would be zero will be converted into reals (+ #c(1 -1) #c(2 1)) returns 3

• > (list (ratiop 2/2) (complexp #c(1 0)))??

Page 20: Functional Programming 07 Symbols and Numbers. Symbols - Symbol Names A symbol can have any string as its name ▫ > (symbol-name ‘abc) “ABC” ▫ The name

Numbers – Conversion and Extraction

• > (mapcar #’float ‘(1 2/3 .5))(1.0 0.6666667 0.5)

• > (truncate 1.3)10.29999995

• floor• ceiling• (defun our-truncate (n)

(if (> n 0) (floor n) (ceiling n)))

Page 21: Functional Programming 07 Symbols and Numbers. Symbols - Symbol Names A symbol can have any string as its name ▫ > (symbol-name ‘abc) “ABC” ▫ The name

Numbers – Conversion and Extraction

• round: retuns the nearest even digit▫> (mapcar #’round ‘(-2.5 -1.5 1.5 2.5))

(-2 -2 2 2)• signum: returns either 1, 0, or -1, depending on

whether its argument is positive, zero, or negative▫(* (abs x) (signum x)) = x

Page 22: Functional Programming 07 Symbols and Numbers. Symbols - Symbol Names A symbol can have any string as its name ▫ > (symbol-name ‘abc) “ABC” ▫ The name

Numbers – Comparison• > (= 1 1.0)

T• > (eql 1 1.0)

NIL• > (equal 1 1.0)

??• (<= w x y z) ≡ (and (<= w x) (<= x y) (<= y z))• (/= w x y z) ≡ (and (/= w x) (/=w y) (/= w z)

(/= x y) (/=x z) (/= y z))• > (list (minusp -0.0) (zerop -0.0))

(NIL T)• > (list (max 1 2 3 4 5) (min 1 2 3 4 5))

(5 1)

Page 23: Functional Programming 07 Symbols and Numbers. Symbols - Symbol Names A symbol can have any string as its name ▫ > (symbol-name ‘abc) “ABC” ▫ The name

Numbers – Arithmetic• (- x y z) ≡ (- (- x y) z)• (-1 x) returns x-1• (incf x n) ≡ (setf x (+ x n))

(decf x n) ≡ (setf x (- x n))• > (/ 365 12)

365/12• > (float 365/12)

30.416666• > (/ 3)

1/3• (/ x y z) ≡ (/ (/ x y) z)

Page 24: Functional Programming 07 Symbols and Numbers. Symbols - Symbol Names A symbol can have any string as its name ▫ > (symbol-name ‘abc) “ABC” ▫ The name

Numbers – Exponentiation• (expt x n) →

▫> (expt 2 5)32

• (log x n) →▫ (log 32 2)

5.0• > (exp 2)

7.389056• > (log 7.389056)

2.0• > (expt 27 1/3)

3.0• > (sqrt 4)

2.0

nx

logn x

Page 25: Functional Programming 07 Symbols and Numbers. Symbols - Symbol Names A symbol can have any string as its name ▫ > (symbol-name ‘abc) “ABC” ▫ The name

Homework

•Modify the following program (defun mirror? (s) (let ((len (length s)))

(and (evenp len) (let ((mid (/ len 2))) (equal (subseq s 0 mid) (reverse (subseq s mid))))))) to recognize all palindromes

Page 26: Functional Programming 07 Symbols and Numbers. Symbols - Symbol Names A symbol can have any string as its name ▫ > (symbol-name ‘abc) “ABC” ▫ The name

Homework

• (defun palindrome? (x) (let ((mid (/ (length x) 2))) (equal (subseq x 0 (floor mid)) (reverse (subseq x (ceiling mid))))))