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

Functional Programing

Feb 24, 2016

Download

Documents

saxon

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. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Functional Programing

Functional Programing

Referencing material fromProgramming Language Pragmatics – Third Edition – by Michael L.

ScottAndy Balaam (Youtube.com/user/ajbalaam)

Page 2: Functional Programing

Historical OriginsHow did we get here

Page 3: Functional Programing

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

Page 4: Functional Programing

Two ParadigmsTuring 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 100th Birthday

• All of the languages you have learned in 201 and 202

Lambda Calculus(Functional Languages)

• Based on parameterized expressions, each parameter is introduced with a

• One substitutes parameters into expression to compute each expression

• Example: Scheme (later)• Lisp (scheme, rocket), Haskell, Miranda,

pH, Sisal, Single Assignment C, Erlang

Page 5: Functional Programing

Functional Programming ConceptsA completely new paradigm

Page 6: Functional Programing

No side effects

• Based on function• A function takes parameters and returns something• Functions can not modify values

Page 7: Functional Programing

First Class values

• Everything is a first class value, including functions• This allows for higher order functions, which operate on

functions.

Page 8: Functional Programing

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

Page 9: Functional Programing

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)

Page 10: Functional Programing

SchemeA language with only one feature

Page 11: Functional Programing

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

Page 12: Functional Programing

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

Page 13: Functional Programing

You can do one thing

(item item item item item item item)

Page 14: Functional Programing

A Scheme program

(operation operation operation operation)Operation: (operator operand operand operand)

Page 15: Functional Programing

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

Page 16: Functional Programing

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)

Page 17: Functional Programing

Booleans

• #t for true• #f for false

Page 18: Functional Programing

Control flow

IfIf [Boolean] [expr if true] [expr if false]

CondCond

([boolean] [expr])([boolean] [expr])(else [expr if else])

Page 19: Functional Programing

Dynamic typing

(if (> a 0) (+ 2 3) (+ 2 “foo”))

This will execute fine? Why?

Page 20: Functional Programing

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

Page 21: Functional Programing

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

Page 22: Functional Programing

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

Page 23: Functional Programing

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

Page 24: Functional Programing

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]

Page 25: Functional Programing

List operations

• Car [list]• Cdr [list]• Cons [item] [list]

Page 26: Functional Programing

Higher-Order FunctionsI heard you like functions, so we made your functions return

functions, so you can compute what you compute.

Page 27: Functional Programing

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

Page 28: Functional Programing

Common Higher order functions

• Define• Load• Lambda• For-each• Call• apply• compose

Page 29: Functional Programing

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

Page 30: Functional Programing

Evaluation OrderPutting functional programming in order

Page 31: Functional Programing

Evaluation orderApplicative-order

• You evaluate each argument before you pass it to a function

Normal-order• You pass each argument as an

unevaluated expression

Page 32: Functional Programing

Example (Right from the book)

Applicative-order(double (* 3 4))(double 12)(+ 12 12)24

What kind of cases could applicative order be wasteful?

Normal-order(double (* 3 4))(+ (* 3 4) (* 3 4))(+ 12 (* 3 4))(+ 12 12)24This is much longerWe calculate the same value twice

(Define double (lambda (x) (+ x x)))(double (* 3 4))

Page 33: Functional Programing

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

Page 34: Functional Programing

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

Page 35: Functional Programing

Functional Programming in Perspective

Functional and comparative, when and why

Page 36: Functional Programing

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

Page 37: Functional Programing

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.