Ch. 9 - 11 Ch. 9 - 111 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (n-n-n-notes) Summer 2003 Dr. Carter Tiernan.

Slides:



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

1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 14 Functional Programming Languages - The design of the imperative languages is based directly.
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.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
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.
Lisp – Introduction יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
Storage Reclamation Explicit Erasure Reference Count Garbage collection.
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
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.
ISBN Chapter 15 Functional Programming Languages Mathematical Functions Fundamentals of Functional Programming Languages Introduction to.
Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester, 2010
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.
JavaScript, Third Edition
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.
(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.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University
ISBN Chapter 15 Functional Programming Languages.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
LISP John McCarthy and Marvin Minsky formed MIT’s AI Project in Suggested reading: John McCarthy’s home page
Functional Programming in Scheme and Lisp.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
Ch. 5 Ch. 51 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (more notes) Dr. Carter Tiernan.
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
ISBN Chapter 15 Functional Programming Languages.
ISBN Chapter 15 Functional Programming Languages.
Shayan Ehsani Hessameddin Akhlaghpour. Remembrance History Eval funciton Applications.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Functional Programming: Lisp MacLennan Chapter 10.
Ch Ch jcmt CSE 3302 Programming Languages CSE3302 Programming Languages Dr. Carter Tiernan.
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.
Comparative Programming Languages Functional programming with Lisp/Scheme.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
1 jcmt Summer 2003Programming Languages CSE3302 Programming Languages Summer 2003 Dr. Carter Tiernan.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester,
CS314 – Section 5 Recitation 9
Functional Programming
CS314 – Section 5 Recitation 10
Functional Programming Languages
Functional Programming
History of Computing – Lisp
CS 326 Programming Languages, Concepts and Implementation
Introduction to Scheme
Scheme : variant of LISP
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.
Functional Programming Languages
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
Cons operation calls for the allocation of a new cons-cell, like new in Pascal. How to make it free?
Abstraction and Repetition
Functional Programming: Lisp
Modern Programming Languages Lecture 18 Fakhar Lodhi
Lisp.
Presentation transcript:

Ch Ch jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (n-n-n-notes) Summer 2003 Dr. Carter Tiernan

Ch Ch jcmt CSE 3302 Programming Languages Functional Programming List Processing - LISP Complex interrelationships among data Recursion in conjunction with conditional expressions Primitive list-handling subroutines Applicative language

Ch Ch jcmt CSE 3302 Programming Languages LISP John McCarthy Function applications –Prefix (Polish) notation : flexibility –Fully parenthesized : no precedence rules Symbolic data –Lists of symbols called atoms –List is ONLY data structure in LISP

Ch Ch jcmt CSE 3302 Programming Languages Lists S-expressions –Function applications are evaluated – Quoted lists are treated as data Programs and data represented the same way –Convenient for one program to generate and call for execution of another –Simple to write program manipulators

Ch Ch jcmt CSE 3302 Programming Languages LISP Execution Usually interpreted and often interactive Functions – Pure : compute a value only eq, plus, difference, etc. – Pseudo : have side effects on computer state set, defun, etc.

Ch Ch jcmt CSE 3302 Programming Languages Data Structures Constructor : list Primitives : atoms –Numeric (integer and floating point) Many ops provided: arithmetic, increment, decrement, max, min, relational, predicates –Nonnumeric character strings Limited ops: comparisons for equality nil (false, empty set) null is the test op

Ch Ch jcmt CSE 3302 Programming Languages Constructor List –Surrounded by parentheses –Separated by blanks –Zero, one or more elements –Can contain lists –() equivalent to nil ; called empty or null list

Ch Ch jcmt CSE 3302 Programming Languages Accessing Parts of a List Car –Selects the first element of a list Cdr –Returns all of a list except its first element Car and Cdr are selectors Car and Cdr can be combined to access interior parts of a list: ex. caadr

Ch Ch jcmt CSE 3302 Programming Languages Building a list Cons –Creates a list from a first element and a list –Is the inverse of car and cdr –Example: (car ‘(to be or not))= to (cdr ‘(to be or not))= (be or not) (cons ‘to ‘(be or not))= (to be or not)

Ch Ch jcmt CSE 3302 Programming Languages Info Representation Property lists –( p1 v1 p2 v2 … pn vn) where pn is the nth property and vn is the value of that property –P-lists give flexibility –Easy to define a function to get properties

Ch Ch jcmt CSE 3302 Programming Languages Info Representation Association lists –(( a1 v1) (a2 v2) … ( an vn )) –Handles properties which are flags or properties with multiple values

Ch Ch jcmt CSE 3302 Programming Languages Recursive list construction Append two lists together –Identify simplest cases (append ‘() L) = L (append L ‘()) = L –Reduce other cases to the simplest cases (append L M) = (cons (car L) (append (cdr L) M) –(defun append (L M) (if (null L)) M (cons (car L) (append (cdr L) M) ))

Ch Ch jcmt CSE 3302 Programming Languages Atoms Created by mentioning them Have properties and relations (p-list) –Print name / pname – putprop adds a property to an atom – apval denotes the binding of an atom to a value – set binds an atom – symbol-plist shows property list of an atom

Ch Ch jcmt CSE 3302 Programming Languages List representation Linked list Cells in the list have a right and a left –Right part points to next cell in list –Left part points to value of the cell Data and programs both represented the same way –Program linked list called an expression tree

Ch Ch jcmt CSE 3302 Programming Languages List Primitives Efficient –Car returns a left part –Cdr returns a right part –Cons requires storage allocation for new cell –Car, Cdr, and Cons do not change values Sublists can be shared Pseudo-functions alter lists –rplaca, rplacd –Have same drawbacks as aliasing

Ch Ch jcmt CSE 3302 Programming Languages Conditional expression Cond –Mimics mathematical notation –Logical ops are defined in terms of the conditional And & Or –Operands are evaluated sequentially – Conditional interpretation vs. strict

Ch Ch jcmt CSE 3302 Programming Languages Iteration Performed by recursion Reduction –Perform some op on every element of a list –Uses a binary function to reduce a list to a single value Mapping –Apply a function to every element of a list –Returns a list of the results Filtering –Forms a sublist containing all elements that satisfy some property

Ch Ch jcmt CSE 3302 Programming Languages Functional arguments Abstracting out pattern Mapcar Filter Reduce Suppress details Simplify combination

Ch Ch jcmt CSE 3302 Programming Languages Recursive Interpreters Arranged by cases –Atoms Numeric Nonnumeric –Quote –Conditional –Functions

Ch Ch jcmt CSE 3302 Programming Languages Interpreters Primitive ops performed explicitly User-defined functions –Evaluate parameters –Bind formals to actuals –Add bindings to environment –Evaluate function in environment

Ch Ch jcmt CSE 3302 Programming Languages Environment of evaluation Arguments going to ( apply f x a) (p.380) – f the function - lambda expression – x the parameters - bound formals – a the environment - existing environment with the addition of the bound formals Universal function

Ch Ch jcmt CSE 3302 Programming Languages Static scoping Closure to indicate environment of definition - function keyword – Instruction part (ip) - program, lambda expr. – Environment part (ep) - definition environment ( closure ip ep) User-defined functions can be –Dynamically scoped lambda expressions –Statically scoped function closures

Ch Ch jcmt CSE 3302 Programming Languages Incompatible scope rules Options –Adopt uniform static scoping –Use default static scoping but allow “special” dynamic scoping

Ch Ch jcmt CSE 3302 Programming Languages Storage reclamation Explicit erasure –Work for programmers –Security problems (dangling pointers) Automatic reclamation –Reference counts –Garbage collection

Ch Ch jcmt CSE 3302 Programming Languages Reference counts Accessible cells are referenced Reference count keeps track of how many other cells reference the current one Count must be incremented and decremented correctly Cells with zero count are added to the free list

Ch Ch jcmt CSE 3302 Programming Languages Reference counts Cycles defeat reference counts –Cyclic reference boosts the count of each member plus one member has an outside reference. –If outside reference goes away, cycle still has references internally but no access from anywhere else in program –Allowing cycles is an open issue

Ch Ch jcmt CSE 3302 Programming Languages Garbage Collection When storage becomes low, system starts up garbage collection Mark and sweep process –Starting at roots mark all cells that can be reached recursively moving from each cell to its children –When all are marked, sweep through memory from top, free unmarked cells and reset flags on marked cells

Ch Ch jcmt CSE 3302 Programming Languages Garbage Collection Wreaks havoc on execution time Non uniform and unpredictable Approaches to more efficiency include –Continuous garbage collection –Parallel

Ch Ch jcmt CSE 3302 Programming Languages LISP AI - represent and manipulate complex interrelationships among symbolic data Suited to ill-specified problems Easy to manipulate LISP programs in LISP