Modern Programming Languages Lecture 20 Fakhar Lodhi

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.
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.
C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
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)
Helper functions: when extra arguments are needed Consider this problem: we want a function index_items that takes a list L and gives a number to each.
ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
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.
Common Lisp! John Paxton Montana State University Summer 2003.
Imperative programming public int factorial (int N){ int F = 1; for(X=N; X>1; X-- ){ F= F*X; } return F; } Functional programming (defun factorial(N) (cond.
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?
Scheme examples. Recursion Iteration is achieved via recursion Selection is achieved via COND Review the following examples to learn to think recursively.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester, 2010
Programming in Lisp; Instructor: Alok Mehta Programming in Lisp Recursion, Data Abstraction, Mapping, Iteration.
Symbolic Expressions (S Expressions) Syntax: Opening and Closing parenthesis having elements in between. List represented in LISP: (A B2 C3 Shahid) (A.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
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 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.
Conditionals and Recursion "To iterate is human, to recurse divine." - L. Peter Deutsch.
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.
The Case primitive: matches the evaluated key form against the unevaluated keys by using eql The general format of case is the following: (case (... ).....
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.
Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.
CSE 341, S. Tanimoto Lisp Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures.
Iteration Chapters 6 & 7. Iteration in LISP n LISP (unlike Prolog) allows iteration –mapcar, remove-if(-not), count-if, find-if for special purpose iteration.
Lecture 6-2CS250: Intro to AI/Lisp Programming in Your Favorite Language Lecture 5-2 February 11 th, 1999 CS250.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
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.
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)
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.
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester,
CS 550 Programming Languages Jeremy Johnson
Example of formula (defun roots (a b c) (list
Modern Programming Languages Lecture 20 Fakhar Lodhi
Racket CSC270 Pepper major portions credited to
Chapter 15 – Functional Programming Languages
Getting Started with Lisp
CS 270 Math Foundations of CS Jeremy Johnson
LISP A brief overview.
Using Lisp Lisp is a interactive system
Allegro CL Certification Program
First Lecture on Introductory Lisp
Writing LISP functions
Modern Programming Languages Lecture 21 Fakhar Lodhi
Lisp Tutorial Click on Xlisp icon – you enter the interpreter
CSE S. Tanimoto Explicit Function Application
Lisp: Using Functions as Data
LISP A brief overview.
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
Defining Functions with DEFUN
Abstraction and Repetition
Functional Programming: Lisp
Lisp: Using Functions as Data
Modern Programming Languages Lecture 18 Fakhar Lodhi
Common Lisp II.
Lisp.
The general format of case is the following: (case <key form>
LISP primitives on sequences
Presentation transcript:

Modern Programming Languages Lecture 20 Fakhar Lodhi

Defining LISP functions (defun func-name (arg-1 ... Arg-n) func-body) Example: >(defun y-plus (x) (+ x y)) ;definition of y-plus >(setq y 2) >(y-plus 23) 25 Local and global variables: local variables defined in function body Function returns the value of last expression in its body

Conditional control: if, when and cond (if <test> <then> <else>) > (setq SCORE 78) > 78 > (if (> score 85) ‘HIGH (if (and (< score 84) (> score 65)) ‘MEDIUM ‘LOW)) > MEDIUM

Conditional control: if and cond >(cond (<test-1> <action-1>) ... (<test-k> <action-k>)) each (<test-i> <action-i>) is called a clause; if test-i (start with i=1) returns T (or anything other than NIL), this function returns the value of action-i; else, go to the next clause; usually, the last test is T, which always holds, meaning otherwise. cond can be nested (action-i may contain (cond ...))

Conditional control: if and cond > (setf operation ‘area L 100 W 50) > 50 > (cond ((eq operation ‘perimeter) (* 2 (+ L W))) (eq operation ‘area) (* L W)) (t ‘i-do-not-understand-this) ) > 5000

Recursion Main tool used for iteration There is a limit to the depth of the recursion, and it depends on the version of LISP Use trace to watch recursion Example: (defun power (x y) (if (= y 0) 1 (* x (power x (1- y)))))

>(trace power) >(power 3 4) 1> (POWER 3 4) ;; Your actual output 2> (POWER 3 3) ;; may vary in format 3> (POWER 3 2) 4> (POWER 3 1) 5> (POWER 3 0) 1 3 9 27 81

Example: > (Length ‘(a b c d)) > 4 (defun length (x) (if (null x) 0 (+ length (rest x) 1) ) > (Length ‘(a b c d)) > 4

(defun intersection (L1 L2) examples: (defun member (x L) (cond ((null L) nil) ; base case 1: L is empty ((equal x (car L)) L) ; base case 2: x=first(L) (t (member x (cdr L))) ; recursion: test if x is in rest(L) )) (defun intersection (L1 L2) (cond ((null L1) nil) ((null L2) nil) ((member (car L1) L2) (cons (car L1) (intersection (cdr L1) L2))) (t (intersection (cdr L1) L2)) > (intersection '(a b c) '(b a b c)) > (a b c) > (intersection '(b a b c) '(a b c)) > (b a b c)

(defun set-difference (L1 L2) (cond ((null L1) nil) ((null L2) L1) ((not (member (car L1) L2)) (cons (car L1) (set-difference (cdr L1) L2))) (t (set-difference (cdr L1) L2)) ))

Iteration: dotimes and dolist (dolist (x L result) body) for each top level element x in L, do body(x); x is not equal to an element of L in each iteration, but rather x takes an element of L as its value; (dotimes (count n result) body) do body n times. count starts with 0, ends with n-1 Note: result is optional, to be used to hold the computing result. If result is given, the function will return the value of result, returns NIL, otherwise. (may change global variables as side effects.)

Iteration: dotimes and dolist (dolist (x L result) body) for each top level element x in L, do body(x); x is not equal to an element of L in each iteration, but rather x takes an element of L as its value; result is optional, to be used to hold the computing result. If result is given, the function will return the value of result, returns NIL, otherwise. (may change global variables as side effects.)

Iteration: dotimes and dolist > (setf cold 15 hot 35) > 35 > (defun count-pleasant (list-of-temperatures) (let ((count-is 0)) ; initialize (dolist (element my-list count-is) (when (and (< element hot) (> element cold)) (setf count-is (+ count-is 1)))))) > (count-pleasant ‘(30 45 12 25 10 37 32)) > 3