Top Banner
Interlude: Functional Programming CSE 331 Section 2 James Daly
29

Interlude: Functional Programming CSE 331 Section 2 James Daly.

Dec 24, 2015

Download

Documents

Bruno Ferguson
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: Interlude: Functional Programming CSE 331 Section 2 James Daly.

Interlude: Functional ProgrammingCSE 331Section 2James Daly

Page 2: Interlude: Functional Programming CSE 331 Section 2 James Daly.

Reminder

• Homework 2 due• Leave on table before you leave

• Project 2 is out• Covers tree sets• Due next Friday at midnight

• Exam 1 will be Thursday, February 26• Review day the preceding Tuesday.• 1 sheet of notes

Page 3: Interlude: Functional Programming CSE 331 Section 2 James Daly.

Interlude

• Covering something completely different today

• Discussing differences between languages• Focus on a very different way of programming

• Most examples will be in Scala• More available at www.scala-lang.org

Page 4: Interlude: Functional Programming CSE 331 Section 2 James Daly.

Greenspun’s Rule

• “Any sufficiently complicated C or Fortran program contains an ad hoc, informally specified, bug-ridden, slow implementation of half of Common Lisp.”• Phillip Greenspun

Page 5: Interlude: Functional Programming CSE 331 Section 2 James Daly.

Morris’s Corollary

• “… including Common Lisp.”• Robert Morris

Page 6: Interlude: Functional Programming CSE 331 Section 2 James Daly.

Java vs C++

• Garbage collection• No explicit delete calls• Memory cleaned up automatically when object

no longer used• Not cleaned up immediately• Better throughput but worse latency

• Always has smart-pointers (references)• Can’t stack-allocate an object

• Always passes references by value

Page 7: Interlude: Functional Programming CSE 331 Section 2 James Daly.

Java vs C++

Java C++

Page 8: Interlude: Functional Programming CSE 331 Section 2 James Daly.

Programming Paradigms

• C/C++ and many other languages use an imperative paradigm• Focus on how to do something• Change of state

• Procedural programming is a common sub-type• Basis is function calls or sub-routines

• Frequently combined with object-oriented programming

Page 9: Interlude: Functional Programming CSE 331 Section 2 James Daly.

Programming Paradigms

• In functional programming, the focus is on what rather than how• Based on mathematical expressions• Avoids changing state / mutable data• Output depends only on inputs

• Basis for Lisp, Scheme, OCaml, and Haskell

Source: rosettacode.org/Factorial

Page 10: Interlude: Functional Programming CSE 331 Section 2 James Daly.

Scala

• Object-functional• Compiles to Java bytecode

Page 11: Interlude: Functional Programming CSE 331 Section 2 James Daly.

Functional Ideas

• First-class Functions / Higher-order Functions• Functions can be passes as arguments to other

functions

• Purity• Function calls don’t change state

• Immutable Data• Data structures cannot be modified

• Reference transparency• Function results depend only on its arguments

Page 12: Interlude: Functional Programming CSE 331 Section 2 James Daly.

Immutable Data

• Data elements cannot be modified• Return a modified copy instead

• Con – may have to make extra copies• May have to make “defensive” copies anyway

• Pro – may be able to share some sub-data

• Very good for multi-threading• Don’t need to worry whether your copy is up to

date• Nobody can change the data on you

Page 13: Interlude: Functional Programming CSE 331 Section 2 James Daly.

Immutable Example

Page 14: Interlude: Functional Programming CSE 331 Section 2 James Daly.

Sharing Data

Bulk of the list can be shared

Page 15: Interlude: Functional Programming CSE 331 Section 2 James Daly.

Purity

• Functions could do things other than return values• Change program state (environment)• Called side effects

• Pure functions have no side-effects

Page 16: Interlude: Functional Programming CSE 331 Section 2 James Daly.

Reference Transparency

• Calling a function with the same arguments yields the same results• So Func(x) = Func(y) if x = y• Not dependent on the environment• Makes it easier to reason about

• Purity and Immutability ensure the environment doesn’t change

Page 17: Interlude: Functional Programming CSE 331 Section 2 James Daly.

Higher Order Functions

• Functions can be passed as arguments to other functions

• Often takes the form of a lambda expression

• Allows us to make small changes to an algorithm without having to rewrite the whole thing

Page 18: Interlude: Functional Programming CSE 331 Section 2 James Daly.

Higher Order Function

Higher Order Function

Lambda

Page 19: Interlude: Functional Programming CSE 331 Section 2 James Daly.

Higher Order FunctionsGeneric Stack

Function takes a Tand returns an S

Page 20: Interlude: Functional Programming CSE 331 Section 2 James Daly.

Sorting

Source: Scala by Example, by Martin Odersky

Page 21: Interlude: Functional Programming CSE 331 Section 2 James Daly.

Palindromes

• The empty string is a palindrome• A single character is a palindrome• awa is a palindrome if w is a palindrome• aw and wa are both palindromes if w is a

palindrome and a is a special symbol• Nothing else is a palindrome

Page 22: Interlude: Functional Programming CSE 331 Section 2 James Daly.

Pattern MatchingEmpty string

Single letterList starting with a

List ending with a

Anything else

Page 23: Interlude: Functional Programming CSE 331 Section 2 James Daly.

More Pattern Matching Parse tree!

Page 24: Interlude: Functional Programming CSE 331 Section 2 James Daly.

Option

• Return type for when calculations can fail• Two sub-classes

• Some• None

• Replaces null• Don’t need to worry about null in other

cases

Page 25: Interlude: Functional Programming CSE 331 Section 2 James Daly.

Cish way

Page 26: Interlude: Functional Programming CSE 331 Section 2 James Daly.

Option

• Is a collection• May only have one item

Page 27: Interlude: Functional Programming CSE 331 Section 2 James Daly.

Functional Way

Page 28: Interlude: Functional Programming CSE 331 Section 2 James Daly.

A lot of them

Page 29: Interlude: Functional Programming CSE 331 Section 2 James Daly.

C++ Version