Functional Programming: Lisp

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.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
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)
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)
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
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.
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.
Introduction to Artificial Intelligence Lisp Ruth Bergman Fall 2002.
מבוא מורחב - שיעור 10 1 Symbols Manipulating lists and trees of symbols: symbolic differentiation Lecture 10.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Rahman Lavaee Mashhadi Mohammad Shadravan. Conditional expressions LISP was the first language to contain a conditional expression In Fortran and Pascal.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
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.
Alok Mehta - Programming in Lisp - Data Abstraction and Mapping Programming in Lisp Data Abstraction and Mapping.
Common lisp A functional programming language. Useful URL:
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
CSE 341, S. Tanimoto Lisp Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures.
Introduction to Scheme CS 480/680 – Comparative Languages “And now for something completely different…” – Monty Python “And now for something completely.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
מבוא מורחב 1 Lecture #9. מבוא מורחב 2 Symbol: a primitive type constructors: (quote alpha) ==> quote is a special form. One argument: a name. selectors.
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.
Functional Programming: Lisp MacLennan Chapter 10.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
ISBN Chapter 15 Functional Programming Languages.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Ch Ch jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (n-n-n-notes) Summer 2003 Dr. Carter Tiernan.
List Processing LISP MacLennan- Chap. 9
Operational Semantics of Scheme
Functional Programming Languages
Example of formula (defun roots (a b c) (list
Modern Programming Languages Lecture 20 Fakhar Lodhi
Introduction to Scheme
The Environment Model*
Chapter 15 – Functional Programming Languages
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
COP4020 Programming Languages
LISP A brief overview.
First Lecture on Introductory Lisp
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
Lisp Tutorial Click on Xlisp icon – you enter the interpreter
The Metacircular Evaluator
Abstraction and Repetition
Modern Programming Languages Lecture 20 Fakhar Lodhi
The Metacircular Evaluator (Continued)
Lecture #9 מבוא מורחב.
LISP A brief overview.
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
6.001 SICP Variations on a Scheme
Lecture #7 מבוא מורחב.
Defining Functions with DEFUN
Abstraction and Repetition
To understand recursion, one must first understand recursion.
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.
LISP primitives on sequences
Abstraction and Repetition
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

Functional Programming: Lisp MacLennan Chapter 10

Control Structures Atoms are the only primitives: since they are the only constructs that do not alter the control flow. Literals (represent themselves) Numbers Quoted atoms lists Unquoted atoms are bound to Functions (if they have expr property) Data values (if they have apval property) Control-structure constructors Conditional expression Recursive application of a function to its arguments

In LISP we have conditional expression. While in Fortran and Pascal we have to drop from expression level to instruction level to make a choice.

The logical connectives are evaluated conditionally (or x y) = (if x t y) (or (eq (car L) ‘key) (null L) ) What happen if L is null? (or (null L) (eq(car L) ‘key) ) Does this work? (if (null L) t (eq (car L) ‘key))

Iteration is done by recursive (defun getprop (p x) (if (eq (car x) p) ( cadr x) (getprop p (cddr x)) ))

Reduction : reducing a list to one value (defun plus-red (a) (if (null a) (plus (car a) (plus-red (cdr a)) )) ) (plus-red ‘(1 2 3 4 5)) >> 15

Mapping: mapping a list into another list of the same size (defun add1-map (a) (if (null a) nil (cons (add1 (car a)) (add1-map (cdr a)) )) ) (add1-map ‘(1 9 8 4)) >>(2 10 9 5)

Filtering: forming a sublist containing all the elements that satisfy some property (defun minus-fil (a) (cond ((null a) nil) (( minusp (car a)) (cons (car a) minusp-fil (cdr a)) )) (t (minusp-fil (cdr a)) )) )

Some thing like Cartesian product , which needs two nested loop For example: (All-pairs ‘(a b c) ‘(x y z)) >>((a x) (a y) (a z) (b x) (b y) (b z) (c x) (c y) (c z)) We use distl (distribute from left) (distl ‘b ‘(x y z)) >>((b x) (b y) (b z))

(defun all-pairs (M N) (if (null M) nil (append (distl (car M) N) (all-pairs (cdr M) N)) )) (defun distl (x N) (if (null N) (cons (list x) (car N)) (distl x (cdr N)) )) ) (the list function makes a list out of its argument)

Recursion for Hierarchical structures (defun equal (x y) ( or (and (atom x) (atom y) (eq x y)) (and (not (atom x)) (not (atom y)) (equal (car x) (car y)) (equal (cdr x) (cdr y)) )) )

Recursion and Iteration are theoretically equivalent

Functional arguments allow abstraction mapcar: a function which applies a given function to each element of a list and returns a list of the results. (defun mapcar (f x) (if (null x) nil (cons (f (car x)) (mapcar f (cdr x)) )) ) (mapcar ‘add1 ‘(1 9 8 4)) >> (2 10 9 5) (mapcar ‘zerop ‘(4 7 0 3 -2 0 1)) >> (nil nil t nil nil t nil) (mapcar ‘not (mapcar ‘zerop ‘(4 7 0 3 -2 0 1))) >> (t t nil t t nil t)

Functional arguments allow programs to be combined They simplify the combination of already implemented programs.

Lambda expressions are anonymous functions Instead of defining a name for a function (defun twotimes (x) (times x 2) (mapcar ‘twotimes L) We may write: (mapcar ‘(lambda (x) (times x 2)) L)

Name Structures The primitive name structures are the individual bindings of names (atoms) to their values. Bindings are established through Property lists By pseudo-functions set (like declaring a variable) Defun (like declaring a procedure) Actual-formal correspondence

Application binds Formals to Actuals

Temporary bindings are a simple, syntactic extension (with let )

Dynamics scoping is the constructor Dynamic scoping complicates functional arguments