Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interlude: Functional Programming CSE 331 Section 2 James Daly.

Similar presentations


Presentation on theme: "Interlude: Functional Programming CSE 331 Section 2 James Daly."— Presentation transcript:

1 Interlude: Functional Programming CSE 331 Section 2 James Daly

2 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

3 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.orgwww.scala-lang.org

4 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

5 Morris’s Corollary “… including Common Lisp.” Robert Morris

6 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

7 Java vs C++ Java C++

8 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

9 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

10 Scala Object-functional Compiles to Java bytecode

11 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

12 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

13 Immutable Example

14 Sharing Data Bulk of the list can be shared

15 Purity Functions could do things other than return values Change program state (environment) Called side effects Pure functions have no side-effects

16 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

17 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

18 Higher Order Function Lambda

19 Higher Order Functions Generic Stack Function takes a T and returns an S

20 Sorting Source: Scala by Example, by Martin Odersky

21 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

22 Pattern Matching Empty string Single letter List starting with a List ending with a Anything else

23 More Pattern Matching Parse tree!

24 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

25 Cish way

26 Option Is a collection May only have one item

27 Functional Way

28 A lot of them

29 C++ Version


Download ppt "Interlude: Functional Programming CSE 331 Section 2 James Daly."

Similar presentations


Ads by Google