1 Lisp and Functional Languages Functional forms Referential transparency Function construction Function composition Mapping functions Designing functional.

Slides:



Advertisements
Similar presentations
Functional Programming Languages Session 12
Advertisements

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.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 14 Functional Programming Languages - The design of the imperative languages is based directly.
1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
Recap 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression.
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.
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
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.
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
Functional Programming Languages
Chapter 15 Other Functional Languages. Copyright © 2007 Addison-Wesley. All rights reserved. Functional Languages Scheme and LISP have a simple syntax.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
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.
Common Lisp Derek Debruin Mark Larue Vinh Doan. Problem Domains There was a need in the early 1960s to define a programming language whose computational.
ISBN Lecture 01 Preliminaries. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Lecture 01 Topics Motivation Programming.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
ISBN Chapter 15 Functional Programming Languages.
ISBN Chapter 15 Functional Programming Languages.
Dr. Muhammed Al-Mulhem ICS An Introduction to Functional Programming.
Haskell Chapter 1, Part I. Highly Recommended  Learn you a Haskell for Great Good. Miran Lipovaca.
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
LISP – Not just a Speech Impediment Jim Lowe. Brief History of LISP Initial development begins at the Dartmouth Summer Research Project in 1956 by Dr.
1 Functional Programming In Text: Chapter Chapter 2: Evolution of the Major Programming Languages Outline Functional programming (FP) basics A bit.
 The design of the imperative languages is based directly on the von Neumann architecture  Efficiency is the primary concern  Low-level specifications:
ISBN Chapter 15 Functional Programming Languages.
Functional Programming Languages Chapter 14
ISBN Chapter 15 Functional Programming Languages.
CHAPTER 15 & 16 Functional & Logic Programming Languages.
Chapter Fifteen: Functional Programming Languages Lesson 12.
ISBN Chapter 15 Functional Programming Languages.
CS 363 Comparative Programming Languages Functional Languages: Scheme.
Comparative Programming Languages Language Comparison: Scheme, Smalltalk, Python, Ruby, Perl, Prolog, ML, C++/STL, Java, Haskell.
ISBN Chapter 15 Functional Programming Languages.
ISBN Chapter 15 Functional Programming Languages.
ISBN Chapter 15 Functional Programming Languages.
1 Chapter 15 © 2002 by Addison Wesley Longman, Inc Introduction - The design of the imperative languages is based directly on the von Neumann architecture.
ISBN Chapter 15 Functional Programming Languages.
TIVDM2Functional Programming Language Concepts 1 Concepts from Functional Programming Languages Peter Gorm Larsen.
1-1 An Introduction to Functional Programming Sept
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Functional Programming Part 1. Organization of Programming Languages-Cheng Big Picture u What we’ve learned so far: Imperative Programming Languages 
ISBN Chapter 15 Functional Programming Languages.
Cs7120 (Prasad)L1-FP-HOF1 Functional Programming Basics Correctness > Clarity > Efficiency.
C H A P T E R E I G H T Functional Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
ISBN Chapter 15 Functional Programming Languages.
Functional Programming
Functional Programming
Functional Programming Languages
Functional Programming Languages
Chapter 15 :Functional Programming Languages
Functional Programming Languages
Zuse’s Plankalkül – 1945 Never implemented Problems Zuse Solved
Functional Programming
History of Computing – Lisp
PROGRAMMING LANGUAGES
Functional Programming
Functional Programming Languages
Fundamentals of Functional Programming Languages
Functional Programming
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming Languages
Functional Programming Languages
15.2 Mathematical Functions
Chapter 15 Functional Programming 6/1/2019.
Functional Programming Languages
Presentation transcript:

1 Lisp and Functional Languages Functional forms Referential transparency Function construction Function composition Mapping functions Designing functional languages Other functional language: Scheme, ML, APL,Haskell Applications of Functional Languages Functional and Imperative Language – a comparison Reference: Sebesta Chapter 15

2 Functional Programming Language Design The objective of the design of a FPL is to create and use pure functions to the greatest extent possible Process of computation is fundamentally different –In an imperative language, operations are executed and the results are stored in variables for later use –Management of variables is a constant concern and source of complexity for imperative programming In a purely FPL variables are not necessary –as is the case in mathematics Results of each function application are passed as input to other functions –or are the final result of all computations

3 Functional Forms Definition: –A higher-order function, or functional form, either 1.takes function(s) as parameters 2.or yields a function as its result, 3.or both – takes and returns functions Function Parameters (possibly functions) Return Value (possibly a Function)

4 Viewing Information and Using Functions > (function write-sentence) ; returns defn # > (setq L1 (list "dogs" "cats" "rats" "gerbils")) ("dogs" "cats" "rats" "gerbils") > L1 ("dogs" " cats" "rats" "gerbils" ) > (setq L2 ; pass function as parameter (sort L1 (function string<))) ("cats" "dogs" "gerbils" "rats") > L2 ("cats" "dogs" "gerbils" "rats")

5 Function Construction A functional form that –takes a list of functions as parameters, and –yields a list of the results of applying each parameter function to a parameter Form: [f, g] means [f, g] (x) ≡ (f (x), g (x)) For f (x) ≡ x * x * x and g (x) ≡ x + 3, [f, g] (4) yields (f (4), g (4)) = (64, 7) Application of multiple functions (passed as parameters) to the same piece of data

6 Function Construction Examples > (defun cube (x) (* x x x)) CUBE > (defun add3 (y) (+ y 3)) ADD3 > (defun myfun (fn1 fn2 data) (list (funcall fn1 data) (funcall fn2 data))) MYFUN > (myfun (function cube) (function add3) 4) (64 7) > (myfun #'cube #'add3 4) ; #'… same as (function …) (64 7)

7 Function Composition A functional form that –takes two functions as parameters and –yields a function containing the first actual parameter function applied to the result of application of the second form Form: f ° g means f ° g (x) ≡ f ( g (x)) For f (x) ≡ x * x * x and g (x) ≡ x + 3, f ° g (x) yields (x + 3) * (x + 3) * (x + 3) Continuous application of multiple functions (passed as parameters). Each function is applied to the result of applying the preceding function(s)

8 Function Composition Examples > (defun cube (x) (* x x x)) CUBE > (defun add3 (y) (+ y 3)) ADD3 > (defun myfun (fn1 fn2 data) (funcall fn1 (funcall fn2 data))) MYFUN > (myfun (function cube) (function add3) 4) 343 > (myfun #'cube #'add3 4) ; #'… same as (function …) 343

9 Apply-to-all (Mapping) Functions A functional form that –takes a single function as a parameter –yields a list of values obtained by applying the given function to each element of a list of parameters Form: α means α (f, (x 1, …, x n )) ≡ (f (x 1 ), …, f (x n )) For h (x) ≡ x * x * x α (h, (3, 2, 4)) yields (27, 8, 64) Application of one function to multiple pieces of data

10 mapcar Examples > (defun mydouble (num) "returns 2 times its argument" (* 2 num)) MYDOUBLE > (mapcar (function mydouble) (list 1 2 3)) (2 4 6) > (mapcar #'+ (list 1 2 3) (list )) ( )

11 Lisp – The First Functional Language John McCarthy Stanford since 1953 –MIT & Dartmouth Invented Lisp – 1958 Pioneered logical and commonsense reasoning Lisp’s 20th birthday article (from 1980) –www2.hawaii.edu/~janst/313/lisp/20th.pdf Web: –

12 John McCarthy, professor emeritus of computer science, Stanford Univ., helped to invent the field of artificial intelligence. McCarthy received the Benjamin Franklin Medal in Computer and Cognitive Science on April 24, 2003.

13 Lisp Hardware Problem –Limited processing power in 70s and 80s –Slow execution of Lisp programs Solution –Specialized hardware to improve efficiency of execution and garbage collection –Different from von Neumann machine Symbolics Lisp Machine – mid 1980s – Software - OS was written in Lisp! Other lisp machines by –LMI (Lisp Machine Inc.) –Texas Instruments Explorer

14 Symbolics Lisp Machine Family

15 Optimizations in Lisp Machines Hardware Type Checking –Special type bits let a value’s type be checked efficiently at run-time Hardware Garbage Collection –Special "gc" bits make garbage collection efficient Fast Function Calls Efficient Representation of Lists System Software Integrated Programming Environments

16 Scheme and Common Lisp Scheme is a dialect of LISP (mid-1970s) –Designed to be a cleaner and simpler version of LISP –Uses only static scoping –Functions are first-class entities Can be the values of expressions and elements of lists Can be assigned to variables and passed as parameters Scheme has syntax very similar to Lisp We learn Common Lisp –most used and practical Sebesta text has Scheme examples

17 More Functional Languages APL – A Programming Language –Kenneth Iverson at IBM –About 1960 (book released in 1962) –Not related to ALGOL or other early languages –Dynamic typing and dynamic storage allocation –Large collection of operators –First to include matrix operations –Powerful capabilities –Poor readability –Still in limited use

18 APL Program Example This line of code calculates the prime numbers from 2 to the starting value of R, in this example 20. the "iota function" of R fills a vector (and that will be R again) with numbers from 1 to the value of the variable (20 in this example), the first element is dropped (that is the 1); so to the right of the "/" there will be the "small.circle-dot-multiply" defines an outer product so all elements of R are multiplied by all elements of R giving a matrix; check whether elements of R are in the matrix and make a vector containing "1"-s at the place where that is true and "0"-s where that is not true inverse that vector and use it to grab the elements from R using the "over" function

19 ML (Meta Language) Robert Milner, University of Edinburgh,1980s Designed for program verification A statically scoped functional language Pascal-like syntax Strongly typed, no type coercions Uses type declarations as well as type inferencing to determine the type of a variable “Variables” are only bound once, they cannot change value after binding

20 ML List functions: hd ( head or car ), tl ( tail or cdr ) The val statement binds a name to a value –similar to setq in Lisp Function declaration form: fun ( ) = ; e.g., fun cube (x : int) = x * x * x;

21 Haskell Group formed at 1987 FPL meeting Named after logician Haskell B. Curry Version 1 – 1990 –report centered at the University of Glasgow, Scotland –Also Yale, MIT, U of Wellington AU, Cambridge, Chalmers SE, … Syntax similar to ML Static scoping Strongly typed, type inferencing Purely functional –E.g., no variables, no assignment statements and no side effects of any kind! –unlike most other functional languages!

22 Haskell Features Most Important Features –Lazy evaluation evaluate no sub-expression until its value is needed –"List comprehensions" allow it to deal with infinite lists

23 Haskell Example: Fibonacci Fibonacci numbers –illustrates function definitions fib 0 = 1 fib 1 = 1 fib (n + 2) = fib (n + 1) + fib n

24 Haskell Example: Factorial Factorial –illustrates guards fact n | n == 0 = 1 | n > 0 = n * fact (n - 1) Special word otherwise can be a guard

25 Applications of Functional Languages APL is used primarily for throw-away programs (why?) LISP is used for artificial intelligence research and applications –Knowledge representation –Machine learning –Natural language processing –Modeling of speech and vision AI techniques in Common Lisp power Orbitz Google sponsors summer of code, several Lisp projects Scheme is –Used to teach introductory programming (MIT, Stanford) –Used in the Final Fantasy film to automate the generation of frames (Square USA) –Used in courses at UH on game programming!

26 Functional vs. Imperative Languages Imperative Languages: –Complex syntax –Complex semantics –Closer to von Neumann hardware – machine language –Moderate - efficient execution speed –Concurrency is programmer designed Functional Languages: –Simple syntax –Simple semantics –Slower execution unless compiled –More abstract/higher level – shorter programs, more flexibility –Faster development – shorter design & test cycle –Programs can automatically be made concurrent –More flexible - easily extend the language, creating new ones