Control in LISP More on Predicates & Conditionals.

Slides:



Advertisements
Similar presentations
ANSI Common Lisp 3. Lists 20 June Lists Conses List Functions Trees Sets Stacks Dotted Lists Assoc-lists.
Advertisements

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.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
1 Programming Languages and Paradigms Lisp Programming.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
Defining functions in lisp In lisp, all programming is in terms of functions A function is something which –takes some arguments as input –does some computing.
Introduction to Artificial Intelligence Lisp Ruth Bergman Fall 2002.
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.
>(setf oldlist ) Constructing a list We know how to make a list in lisp; we simply write it: ‘4321( ) What if we want a new list with that list as a part?
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)
Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation.
Mapping And Iteration So far, the only Lisp mechanism we have considered, which allows an action to be performed repeatedly is recursion. Most programming.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
General pattern for selecting some elements of a list This negatives example illustrates a general pattern: If you want a function which selects some elements.
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.
Lisp by Namtap Tapchareon Lisp Background  Lisp was developed by John McCarthy in  Lisp is derives from List Processing Language. 
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.
Lisp: a history Developed by John McCarthy in the 1950’s. Developed by John McCarthy in the 1950’s. Only Fortran has higher “seniority” Only Fortran has.
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.
Advanced Functions In CL, functions are often supplied as parameters to other functions –This gives us tremendous flexibility in writing functions whose.
For Wednesday Read Chapter 4, sections 1 and 2 Homework: –Lisp handout 3.
Functional Programming
Functional Programming 02 Lists
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
The Case primitive: matches the evaluated key form against the unevaluated keys by using eql The general format of case is the following: (case (... ).....
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.
Lisp Laboratory gcLisp (Golden Common Lisp). Lect. ratchadaporn kanawong2 The history of Lisp In summer 1956, Allen Newell, J.C. Shaw, and Herbert Simon.
KU NLP Artificial Intelligence1 Ch 15. An Introduction to LISP q 15.0 Introduction q 15.1 LISP: A Brief Overview  Symbolic Expressions, the Syntactic.
Common lisp A functional programming language. Useful URL:
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
CSCI 2210: Programming in Lisp
The Loop Macro Many of the CL “functions” are actually macros (let, progn, if, etc) The most complicated macro in CL is probably the Loop macro –The Loop.
Basic LISP Programming Common LISP follows the algorithm below when interacting with users: loop read in an expression from the console; evaluate the expression;
Introduction to ACL2 CS 680 Formal Methods for Computer Verification Jeremy Johnson Drexel University.
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.
PRACTICAL COMMON LISP Peter Seibel 1.
Artificial Intelligence Lecture No. 24 Dr. Asad Ali Safi ​ Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
You can access the members of a list with the functions car (or first) and cdr (or rest): (setf list '(a b c)) (car list) ⇒ a (first list) ⇒ a (cdr list)
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)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
Chapter 14 LISP – Practical 3 Instructor: Haris Shahzad Artificial Intelligence CS-402.
Comparative Programming Languages Functional programming with Lisp/Scheme.
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
Conditional Expressions
Section 15.4, 15.6 plus other materials
Modern Programming Languages Lecture 20 Fakhar Lodhi
EGR 2261 Unit 4 Control Structures I: Selection
LISP A brief overview.
Chapter 4: Control Structures I (Selection)
First Lecture on Introductory Lisp
J.E. Spragg Mitthögskolan 1997
Modern Programming Languages Lecture 21 Fakhar Lodhi
PROGRAMMING IN HASKELL
LISP A brief overview.
Announcements Quiz 5 HW6 due October 23
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:

Control in LISP More on Predicates & Conditionals

Equality Tests n LISP provides multiple equality tests –EQUAL and = –EQL and EQ n Serve different purposes –they give different answers in some cases

Equal and = n EQUAL is for general comparisons n = is used for numeric comparisons –an error to use = with non-numbers n = does type conversion; EQUAL does not –(= ) returns T –(equal ) returns NIL

Eq n EQ is “pointer” equality –are these “two” objects pointing to the same hunk of memory n T if same atom or same variable n Maybe T if same number & type > (list (eq ‘a ‘a) (eq ‘(a) ‘(a))) (T NIL)

Eq and SetF > (setf a ‘(1 2 3)) > (setf b ‘(1 2 3)) > (setf c a) > (list (eq a ‘(1 2 3)) (eq a a) (eq a b) (eq a c)) (NIL T NIL T) n A and B are different, but A and C are same –even tho’ they all look exactly the same

Eql and Eq n EQL is just like EQ except for numbers n A number is always EQL to itself –it may not be EQ to itself –neither EQ nor EQL if different types > (list (eql ) (eql ) (eq )) (T NIL T)or(T NIL NIL)

Equal n EQUAL is general purpose –except for numbers it’s what you’d expect > (setf a ‘(1 2 3)) > (setf b ‘(1 2 3)) > (list (equal a ‘(1 2 3)) (equal a a) (equal a b)) (T T T) > (list (equal 1 1.0) (equal a ‘( ))) (NIL NIL)

Equal is Equal n EQUAL goes all the way down –lists with lists treated properly > (equal ‘(1 (2 (3 4) (5 6))) ‘(1 (2 (3 4) (5 6)))) T > (equal ‘(1 2 (3 4)) ‘( )) NIL

Testing for Equality n Use EQUAL if need to compare lists n Use EQ or EQL for atoms/integers –more efficient than equal n Use = for general numbers –only one that equates integers with floats

Exercise n True, false, either or error: (eq ‘a ‘a) (eq 10 10) (eq ) (eq ‘(10) ‘(10.0)) (equal ) (equal ‘(10) ‘(10.0)) (eql ‘a ‘a) (eql 10 10) (eql ) (eql ‘(10) ‘(10.0)) (= ) (= ‘(10) ‘(10.0))

Data Type Predicates n Can test an object to see whether it’s a particular type –ATOM, NUMBERP, SYMBOLP, LISTP –(no P at the end of ATOM) n Numbers and symbols are also atoms n NIL is an atom and a list

Testing for Type > (list (atom ‘a) (numberp ‘a) (symbolp ‘a) (listp ‘a)) (T NIL T NIL) > (list (atom 5) (numberp 5) (symbolp 5) (listp 5)) (T T NIL NIL) > (list (atom ()) (numberp ()) (symbolp ()) (listp ())) (T NIL T T) > (list (atom ‘(a 1)) (numberp ‘(a 1)) (symbolp ‘(a 1)) (listp ‘(a 1))) (NIL NIL NIL T)

Exercise n Evaluate: > (setf a ‘(+ 2 5)) > (list (atom ‘a) (numberp ‘a)) > (list (atom a) (numberp a)) > (list (atom (+ 2 5)) (numberp (+ 2 5))) > (list (listp ‘a) (listp a) (listp (+ 2 5))) > (list (symbolp ‘a) (symbolp a) (symbolp (+ 2 5)))

Number Tests n Simple tests on numbers –ZEROP, PLUSP, MINUSP, EVENP, ODDP n All do what you’d expect, but –errors if not given numbers –errors if given more than one argument > (list (zerop 0) (plusp 1) (minusp 2) (evenp 3)) (T T NIL NIL)

Compound Conditions n LISP provides functions for logical combination of conditions –AND, OR, NOT n Can be used anywhere a condition is required

Logical And n All arguments must be true (i.e. non-NIL) –returns NIL if one argument is NIL –returns last argument if all non-NIL > (and (member ‘a ‘(d a d)) (member ‘o ‘(m o m))) (O M) > (and (member ‘a ‘(d a d)) (member ‘b ‘(m o m))) NIL

Exercise n Evaluate the following: > (and (member ‘a ‘(d a d)) (member ‘u ‘(b u d))) > (and (member ‘a ‘(d a d)) (atom ‘(b u d))) > (and (eq ‘a ‘a) (eql ‘a ‘a) (equal ‘a ‘a)) > (and (eql ) (= )) > (and (atom nil) (listp nil) (numberp nil)) > (and (numberp 10) (numberp 20) ( ))

Logical Or n One argument must be non-NIL –returns NIL if all are NIL –returns first non-NIL if one is non-NIL > (or (member ‘a ‘(d a d)) (member ‘b ‘(m o m))) (A D) > (or (member ‘a ‘(d u d)) (member ‘b ‘(m i m))) NIL

Exercise n Evaluate the following: > (or (member ‘a ‘(d a d)) (member ‘u ‘(b u d))) > (or (member ‘a ‘(b u d)) (atom ‘(b u d))) > (or (eq ‘a ‘a) (eql ‘a ‘a) (equal ‘a ‘a)) > (or (eql ) (= )) > (or (atom ‘a) (listp ‘a) (numberp ‘a)) > (or (numberp 10) (numberp 20) ( ))

Logical Not n Reverses its one argument –returns T if argument is NIL –returns NIL if argument is non-NIL > (not (member ‘a ‘(d a d))) NIL > (not (member ‘b ‘(m o m))) T

Exercise n Evaluate the following: > (not (member ‘a ‘(d a d))) > (not (member ‘u ‘(b a d))) > (not (or (member ‘a ‘(d a)) (member ‘u ‘(u p)))) > (not (and (member ‘a ‘(d a)) (member ‘u ‘(u p)))) > (or (not (atom ‘a)) (not (listp ‘a))) > (and (not (evenp 3)) (or (oddp 8) (zerop 0)))

Exercise n Write a function that takes an atom and two lists, and says how many of the lists the atom is in: ‘none, ‘one, or ‘both > (two-member ‘a ‘(s a d) ‘(d a d)) BOTH > (two-member ‘o ‘(c a l m) ‘(m o m)) ONE

“Short-Circuit” Evaluation n AND & OR are special forms –only evaluate arguments until answer known –can “guard” conditions > (defun eqn (M N) (and (numberp N) (numberp M) (= N M))) > (eqn ) T > (eqn ‘a ‘a) NIL

Short Circuits > (eqn ) (and (numberp 15) => T (numberp 15.0) => T (= ) => T ) => T > (eqn ‘a ‘a) (and (numberp ‘a) => NIL ) => NIL n Doesn’t do: (numberp ‘a) => NIL (= ‘a ‘a) => ERROR AND stops as soon as it sees a NIL

Short Circuits n OR stops as soon as it sees non-NIL (setf L ‘(a)) (or (null L) (= (length L) 1) (eq (first L) (second L)) (or (null ‘(a)) => NIL (= (length ‘(a)) 1) => (= 1 1) => T ) => T Does not compare (first L) = a with (second L) = NIL

Exercise n Write “safe” versions of EVENP & PLUSP –don’t give errors when called with non- numbers n Version 1: use if, when or unless n Version 2: use and, or or not

Case n CASE special form simplifies CONDs –all conditions (except else) involve checking one object against others > (defun fib (N) (cond ((= N 0) 1) ((= N 1) 1) (T (+ (fib (– N 1)) (fib (– N 2)))))) FIB

Case n CASE is a special form –first argument is evaluated –remaining arguments are not > (defun fib (N) (case N (01) (11) (T (+ (fib (– N 1)) (fib (– N 2))))))

Case Cases n Cases appear a (LABEL FORM) pairs n First matching case is selected –the form paired with it is evaluated & returned n Matching: –if LABEL is T or OTHERWISE, match –if LABEL is an atom, use EQL to compare –if LABEL is a list, use member on that list

Compound Case > (defun fib2 (N) (case N ((1 2)1) (T (+ (fib2 (– N 1)) (fib2 (– N 2)))))) n Calls (member N ‘(1 2)) for first case n Just succeeds for second case

Exercise n Write a function to expand three-letter abbreviations for days of the week to the full day name. Use a case statement. –return NIL if the day is not recognized > (day-full-name ‘wed) WEDNESDAY

Exercise n Write a function with case statement to return the number of days in a month (given as a 3-letter abbr.). Ignore leap years. –Thirty days hath September, April, June and November. All the rest have thirty-one, Save February all alone, Which hath twenty eight days clear, & so on

Next Time n More Control in LISP –Chapters 4, 5 & 7