Functional Programming

Slides:



Advertisements
Similar presentations
Lisp Control and Data Structures CIS 479/579 Bruce R. Maxim UM-Dearborn.
Advertisements

09 Examples Functional Programming. Tower of Hanoi AB C.
CS 63 LISP Philip Greenspun's Tenth* Rule of Programming:
ANSI Common Lisp 3. Lists 20 June Lists Conses List Functions Trees Sets Stacks Dotted Lists Assoc-lists.
Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
1 Programming Languages and Paradigms Lisp Programming.
Lisp – Introduction יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
First Lecture on Introductory Lisp Yun Peng. Why Lisp? Because it’s the most widely used AI programming language Because AI researchers and theoreticians.
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
CMSC 471 LISP. Why Lisp? Because it’s the most widely used AI programming language Because it’s good for writing production software (Graham article)
TES3111 Oct 2001 Data Structures S-Expression - Symbolic expression. It can be an Atom, a List or a collection of S- Expression enclosed by (…) Atom -
LISP A brief overview. Lisp stands for “LISt Process” –Invented by John McCarthy (1958) –Simple data structure (atoms and lists) –Heavy use of recursion.
COMP 205 – Week 11 Dr. Chunbo Chu. Intro Lisp stands for “LISt Process” Invented by John McCarthy (1958) Simple data structure (atoms and lists) Heavy.
CSE 341, S. Tanimoto Lisp Data Structures - 1 Data Structures in Lisp 0. Collections of associations, association lists. 1. Creating graphs with conses.
LISP 1.5 and beyond A very quick tour. Data Atoms (symbols) including numbers – All types of numbers including Roman! (well, in the early days) – Syntactically.
F UNCTIONAL P ROGRAMMING 05 Functions. F UNCTIONS - G LOBAL F UNCTIONS fboundp Tells whether there is a function with a given symbol as its name > (fboundp.
Allegro CL Certification Program Lisp Programming Series Level I Session Top Ten Things to Know.
Functional Programming 02 Lists
The Case primitive: matches the evaluated key form against the unevaluated keys by using eql The general format of case is the following: (case (... ).....
PRACTICAL COMMON LISP Peter Seibel 1.
Mitthögskolan 10/8/ Common Lisp LISTS. Mitthögskolan 10/8/2015 2Lists n Lists are one of the fundamental data structures in Lisp. n However, it.
1 Lists in Lisp and Scheme. 2 Lists are Lisp’s fundamental data structures. Lists are Lisp’s fundamental data structures. However, it is not the only.
Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.
For Monday Read Chapter 3 Homework: –Lisp handout 2.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
PRACTICAL COMMON LISP Peter Seibel 1.
Common lisp A functional programming language. Useful URL:
1 More About Lisp. 2 Functions on Lists (setf x '(a b c)) => (A B C) (setf y '(1 2 3)) => (1 2 3) (rest x) => (B C) ;;all but the first element (nth 1.
Introduction An exponent is a quantity that shows the number of times a given number is being multiplied by itself in an exponential expression. In other.
04 Control. Control-Blocks Common Lisp has 3 basic operators for creating blocks of code progn block tagbody If ordinary function calls are the leaves.
06 INPUT AND OUTPUT Functional Programming. Streams Two kinds of streams  Character streams  Binary streams Character streams are Lisp objects representing.
Lecture 6-2CS250: Intro to AI/Lisp Programming in Your Favorite Language Lecture 5-2 February 11 th, 1999 CS250.
Predicates, Functions and Files "I suppose I should learn Lisp, but it seems so foreign." - Paul Graham, Nov 1983.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
Building user-defined functions: the progressive envelopment technique The idea: define combinations of LISP primitives through a sequence of experiments.
Java Expressions MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh.
Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
PRACTICAL COMMON LISP Peter Seibel 1.
Control in LISP More on Predicates & Conditionals.
Operating on Lists Chapter 6. Firsts and Seconds n Transforming list of pairs into two lists –(firsts ‘((1 5) (2 6) (3 7)))  (1 2 3) –(seconds ‘((1 5)
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
1 Variable Declarations Global and special variables – (defvar …) – (defparameter …) – (defconstant …) – (setq var2 (list 4 5)) – (setf …) Local variables.
Basic Introduction to Lisp
Functional Programming: Lisp MacLennan Chapter 10.
Chapter 14 LISP – Practical 3 Instructor: Haris Shahzad Artificial Intelligence CS-402.
Comparative Programming Languages Functional programming with Lisp/Scheme.
Common LISP VS JAVA 김민철. Table of contents  1. LISP …………………………………………… 3  2. 데이터 타입 …………………………………… 4  3. 산술 및 논리 연산 ……………………………… 8  4. 변수 선언 및 할당 ………………………………
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
Section 15.4, 15.6 plus other materials
Intelligent Agents Lecture 3-2 October 14th, 1999 CS250 Lecture 2-1
LISP – (LISt Processing)
Example of formula (defun roots (a b c) (list
LISP A brief overview.
First Lecture on Introductory Lisp
J.E. Spragg Mitthögskolan 1997
Imperative Data Structures
Modern Programming Languages Lecture 21 Fakhar Lodhi
LISP A brief overview.
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
Functional Programming: Lisp
Data Structures in Lisp
Data Structures in Lisp
Modern Programming Languages Lecture 18 Fakhar Lodhi
Common Lisp II.
Programming Languages
Lisp.
List manipulation Consider student database, where each student is represented by the following list: * (setf student1 '((Paul Bennett) ((hw1 4.3) (hw2.
Presentation transcript:

Functional Programming 07 Symbols and Numbers

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

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”

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)

Symbols - Symbols Are Big Symbol is a substantial object

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

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

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

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

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

Symbols - Multiple Packages If Jane wants to use a function written by Bob > (in-package :jane) #<Package JANE> > bob::a 33

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

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)

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)) (unless (zerop pos) (see (intern (string-downcase (subseq buffer 0 pos)))) (setf pos 0)) (let ((p (punc c))) (if p (see p)))))))))

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

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

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

Numbers - Types Four types of numbers Predicates for 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

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

Numbers – Conversion and Extraction > (mapcar #’float ‘(1 2/3 .5)) (1.0 0.6666667 0.5) > (truncate 1.3) 1 0.29999995 floor ceiling (defun our-truncate (n) (if (> n 0) (floor n) (ceiling n)))

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

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)

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)

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

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

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