Functional Programing Referencing material from Programming Language Pragmatics – Third Edition – by Michael L. Scott Andy Balaam (Youtube.com/user/ajbalaam)

Slides:



Advertisements
Similar presentations
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Advertisements

1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
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.
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
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.
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.
Chapter 10 :: Functional Languages
Copyright © 2009 Elsevier Chapter 10 :: Functional Languages Programming Language Pragmatics Michael L. Scott.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
Functional Programming in Scheme
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.
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Functional Programming in Scheme and Lisp.
Chapter Fifteen: Functional Programming Languages Lesson 12.
Functional Programming CS331 Chapter 14. Functional Programming Original functional language is LISP –LISt Processing –The list is the fundamental data.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
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.
Copyright © 2009 Elsevier Chapter 10 :: Functional Languages Programming Language Pragmatics Michael L. Scott.
Functional Programming. Some Functional Languages Lisp Scheme - a dialect of Lisp Haskell Miranda.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
Ada, Scheme, R Emory Wingard. Ada History Department of Defense in search of high level language around Requirements drafted for the language.
Comparative Programming Languages Functional programming with Lisp/Scheme.
1 PROGRAMMING IN HASKELL An Introduction Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
CSE 341 Lecture 21 delayed evaluation; thunks; streams slides created by Marty Stepp
CS314 – Section 5 Recitation 9
Functional Programming
Lecture 4: Metacircles Eval Apply David Evans
CS314 – Section 5 Recitation 10
Functional Programming Languages
Functional Programming
CS 3304 Comparative Languages
Edited by Original material by Eric Grimson
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2017.
Chapter 11 :: Functional Languages
CS 326 Programming Languages, Concepts and Implementation
CS 326 Programming Languages, Concepts and Implementation
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2013.
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2016.
Distributed System Gang Wu Spring,2018.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Autumn 2017.
CS 36 – Chapter 11 Functional programming Features Practice
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Autumn 2018.
Streams, Delayed Evaluation and a Normal Order Interpreter
6.001 SICP Variations on a Scheme
Announcements Quiz 5 HW6 due October 23
6.001 SICP Interpretation Parts of an interpreter
COP4020 Programming Languages
CSE S. Tanimoto Lambda Calculus
topics interpreters meta-linguistic abstraction eval and apply
More Scheme CS 331.
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2019.
Presentation transcript:

Functional Programing Referencing material from Programming Language Pragmatics – Third Edition – by Michael L. Scott Andy Balaam (Youtube.com/user/ajbalaam)

Historical Origins How did we get here

History From the work of Alan Turing, Alonzo Church, Stephen Kleene, Emil Post, and others Each worked on their own Each made a formalized notion of an algorithm Church’s Thesis: Any intuitively appealing model of computing would be equally powerfull

Two Paradigms Turing Machine (Imperative Languages) Based on pushdown automaton A pushdown automata uses a stack Uses an unbounded storage “tape” Computation is done by reading and writing values from cells on the tape Example: Google Doodle for Alan Turing’s 100 th Birthday All of the languages you have learned in 201 and 202 Lambda Calculus (Functional Languages)

Functional Programming Concepts A completely new paradigm

No side effects Based on function A function takes parameters and returns something Functions can not modify values

First Class values Everything is a first class value, including functions This allows for higher order functions, which operate on functions.

Polymorphism Most functional languages are polymorphic Lisp (Scheme, Rocket, etc.) is dynamically typed Functions can take many different types and conditionally deal with them based on type

Lists A list is an item followed by a list This leads to natural recursion Provides the only way to repeatedly do something Operate on the first element, do the same with the rest (hint: recursion)

Scheme A language with only one feature

Scheme is a dialect of Lisp Lisp stands for LISt Processing It is usually interpreted, although can be compiled Scheme uses prefix (Caimbrige Polish Notation) – although this makes sense

Scheme Interpreters Dr. Scheme – deprecated Rocket – for the Rocket dialect MIT Scheme – its own implementation My Chosen best: SISC - Second Interpreter of Scheme Code In java – portable Uses standard Scheme in a simple command-line environment

You can do one thing (item item item item item item item)

A Scheme program (operation operation operation operation) Operation: (operator operand operand operand)

How to do things Addition, subtraction, multiplication, and division are predefined and referred to with +,-,*,/. Other operations, like modulus are referred to with words In order to trigger evaluation you must wrap an operation in parenthasys (+ 1 2) evaluates to 3 7 is already evaluated, it results in 7 (7) tries to run the function 7. 7 is not a function Similarly ((+ 1 2)) tries to run the function 3

How to not do things A single quote defines a list Because an operation is a list, this means that we can use the single quote to do operations on a operation or return the operation ‘(+ 1 2) results in the list (+ 1 2)

Booleans #t for true #f for false

Control flow If If [Boolean] [expr if true] [expr if false] Cond ([boolean] [expr]) (else [expr if else])

Dynamic typing (if (> a 0) (+ 2 3) (+ 2 “foo”)) This will execute fine? Why?

Defining items – lambda expressions From lambda calculus Lambda takes two arguments, a list of identifiers, and an expression to compute using them Lambda (x) (* x x) is a function that takes a value and returns its square

Defining – function ‘Define’ As the Book does it Define takes two parameters, an identifier, and a function (Define pow (lambda (x) (* x x)) allows us to use the function pow that takes a parameter and returns its square

Defining – function ‘Define’ Another way Define takes two parameters, a list matching how it should be called, and an expression using the identifiers given in the first part This merges lambda expressions and definition (define (pow x) (* x x)) defines the same thing as before

Defining – local bindings Defining is just global binding You can create local bindings using let Let takes a list of defines parameters and an expression, and runs the expression using that set of defines

Lists Everything is a list Recall: a single quote makes a set of parenthesis not evaluate and stay as a list ‘(1 2 3) is a list Recall: a list is an item followed by a list What about the last item? Null? [list]

List operations Car [list] Cdr [list] Cons [item] [list]

Higher-Order Functions I heard you like functions, so we made your functions return functions, so you can compute what you compute.

Metaprogramming is just programming Metaprogramming is writing code about code Lisp doesn’t care Lisp is homoiconic – a lisp program is a list. A function can be an argument to a function, or it can be returned from a function

Common Higher order functions Define Load Lambda For-each Call apply compose

Example – Folding (define fold (lambda (f I l) (if (null? L) I (f (car l) (fold f I (cdr l)))))) This takes a function f to fold the list l using the identity i

Evaluation Order Putting functional programming in order

Evaluation order Applicative-order You evaluate each argument before you pass it to a function Normal-order You pass each argument as an unevaluated expression

Example (Right from the book) Applicative-order (double (* 3 4)) (double 12) ( ) 24 What kind of cases could applicative order be wasteful? Normal-order (double (* 3 4)) (+ (* 3 4) (* 3 4)) (+ 12 (* 3 4)) ( ) 24 This is much longer We calculate the same value twice (Define double (lambda (x) (+ x x))) (double (* 3 4))

Scheme The book claims that scheme evaluates in applicative-order. But what about this line? (We just saw this in dynamic typing) (if (> a 0) (+ 2 3) (+ 2 “foo”))

In reality: Lazy evaluation We evaluate any evaluable expressions and store their value for later use We can forget about this, it is behind the scenes

Functional Programming in Perspective Functional and comparative, when and why

Side-effect free Its simple Not much advanced computer science needed Perfect for math No required evaluation order (other than common sense) Parallelism doesn’t matter (the only way to “talk” is to pass variables) Some general programming ideas require assignment (we can’t do that) I/O is difficult (technically impossible without side effects) Any small update requires an entire new copy of the data

In conclusion Fun Easy to use You can make a computational program easily Not a tool for every job, but every tool has a job.