interlude: functional programming cse 331 section 2 james daly

29
Interlude: Functional Programming CSE 331 Section 2 James Daly

Upload: bruno-ferguson

Post on 24-Dec-2015

220 views

Category:

Documents


3 download

TRANSCRIPT

Interlude: Functional ProgrammingCSE 331Section 2James 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

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

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

Morris’s Corollary

• “… including Common Lisp.”• Robert Morris

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

Java vs C++

Java C++

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

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

Scala

• Object-functional• Compiles to Java bytecode

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

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

Immutable Example

Sharing Data

Bulk of the list can be shared

Purity

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

• Pure functions have no side-effects

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

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

Higher Order Function

Higher Order Function

Lambda

Higher Order FunctionsGeneric Stack

Function takes a Tand returns an S

Sorting

Source: Scala by Example, by Martin Odersky

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

Pattern MatchingEmpty string

Single letterList starting with a

List ending with a

Anything else

More Pattern Matching Parse tree!

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

Cish way

Option

• Is a collection• May only have one item

Functional Way

A lot of them

C++ Version