Modern Programming Languages Lecture 20 Fakhar Lodhi

Slides:



Advertisements
Similar presentations
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.
Advertisements

CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
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.
Common Lisp! John Paxton Montana State University Summer 2003.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
1 COSC3306: Programming Paradigms Lecture 11: Applicative Programming with Lisp Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.
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.
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.
Common Lisp! John Paxton Montana State University Summer 2003.
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.
Symbolic Expressions (S Expressions) Syntax: Opening and Closing parenthesis having elements in between. List represented in LISP: (A B2 C3 Shahid) (A.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
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.
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. 
(5.1) COEN Functional Languages  Functional programming basics  Atoms and lists; cons  Useful primitive functions  Predicates  Arithmetic functions.
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.
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.
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.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
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,
Introduction to ACL2 CS 680 Formal Methods for Computer Verification Jeremy Johnson Drexel University.
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.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
Control in LISP More on Predicates & Conditionals.
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.
Comparative Programming Languages Functional programming with Lisp/Scheme.
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,
Functional Programming Languages
Section 15.4, 15.6 plus other materials
Lisp S-Expressions: ATOMs
CS 550 Programming Languages Jeremy Johnson
Chapter 15 – Functional Programming Languages
CS 270 Math Foundations of CS Jeremy Johnson
LISP A brief overview.
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
Modern Programming Languages Lecture 20 Fakhar Lodhi
Functional Programming Concepts
LISP A brief overview.
Functional Programming Concepts
Defining Functions with DEFUN
Abstraction and Repetition
Functional Programming: Lisp
LISP: Basic Functionality
Modern Programming Languages Lecture 18 Fakhar Lodhi
Functional Programming Concepts
LISP: Basic Functionality
Common Lisp II.
LISP: Basic Functionality
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

List has elements (A B C) Functions List has elements (A B C) > (reverse L) ; reverses a list (C B A) > (length L) ; returns the length of list L 3

score<84 and score>65 Control Flow 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 score<84 and score>65

Control Flow > (cond (<test-1> <action-1>) ... (<test-k> <action-k>)) Each (<test-i> <action-i>) pair is called a clause If test-i (where i=1) returns T (or anything other than NIL), this function returns the value of action-i; otherwise it will go to the next clause Usually the last test is T, which always holds (default case) cond can be nested and may contain other conditional statements (action-i may contain (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

Predicates A special function which returns NIL if the predicate is false, T or anything other than NIL, otherwise We can use the following operators to build the predicates =, >, <, >=, <= for numeric values eq or equal for others atom: test if x is an atom listp: test if x is a list Also number, symbolp, null

Set Operations A list can be viewed as a set whose members are the top elements of the list > (member 'b L) (B C) > (member ‘b (cons 'b L)) (B A B C) > (member x L) NIL ; if no, returns NIL > (union L1 L2) ; returns the union of the two lists > (intersection L1 L2) ; returns the intersection of the two lists > (set-difference L1 L2) ; returns the difference of the two lists List L has elements (A B C) Test if symbol b is a member (a top element) of L; if yes, returns the sublist of L starting at the first occurrence of symbol b

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 are defined in function body Function returns the value of last expression in its body

Recursion Example: (defun power (x y) (if (= y 0) 1 (* x (power x (1- y))) ) There is a limit to the depth of recursion, and it depends on the version of LISP

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