Functional Programming: Lisp MacLennan Chapter 10.

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) Mini Language Interpreter Jeremy R. Johnson.
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)
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
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)
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).
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation.
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.
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.
Slide 1 Vitaly Shmatikov CS 345 Introduction to Scheme.
ISBN Chapter 15 Functional Programming Languages.
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.
Shayan Ehsani Hessameddin Akhlaghpour. Remembrance History Eval funciton Applications.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
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.
1 Introduction to Functional Programming in Racket CS 270 Math Foundations of CS Jeremy Johnson.
Operational Semantics of Scheme
Functional Programming Languages
Example of formula (defun roots (a b c) (list
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.
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
Lisp Tutorial Click on Xlisp icon – you enter the interpreter
The Metacircular Evaluator
The Metacircular Evaluator (Continued)
Lecture #9 מבוא מורחב.
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
Lecture #7 מבוא מורחב.
Defining Functions with DEFUN
Abstraction and Repetition
Functional Programming: Lisp
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
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

Functional Programming: Lisp MacLennan Chapter 10

2 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

3 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.

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

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

6 Reduction : reducing a list to one value (defun plus-red (a) (if (null a) 0 (plus (car a) (plus-red (cdr a)) )) ) (plus-red ‘( )) >> 15

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

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

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

10 (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) nil (cons (list x) (car N)) (distl x (cdr N)) )) ) (the list function makes a list out of its argument)

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

12 Recursion and Iteration are theoretically equivalent

13 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 ‘( )) >> ( ) (mapcar ‘zerop ‘( )) >> (nil nil t nil nil t nil) (mapcar ‘not (mapcar ‘zerop ‘( ))) >> (t t nil t t nil t)

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

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

16 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

17 Application binds Formals to Actuals

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

19 Dynamics scoping is the constructor Dynamic scoping complicates functional arguments