Properties of (functions may return functions) Functions may be - - PowerPoint PPT Presentation

properties of
SMART_READER_LITE
LIVE PREVIEW

Properties of (functions may return functions) Functions may be - - PowerPoint PPT Presentation

Functions as first class citizens Function may be a value of an expression Properties of (functions may return functions) Functions may be passed as parameter functional Functions may be stored in data structures languages


slide-1
SLIDE 1

41

Properties of functional languages

  • Functions as ”first class citizens”

– Function may be a value of an expression (functions may return functions) – Functions may be passed as parameter – Functions may be stored in data structures

  • Higher-order functions

– Functions that operate on other functions, not normal data

  • Free order of execution

– Order of evaluation does not effect the

  • utcome
  • Lazy and parallel evaluation
  • Implicit memory handling
slide-2
SLIDE 2

42

Functionality

  • No assignment: "variable" is a

questionable concept (doesn't vary...)

  • "Opposite" of object-oriented

programming (methods invoked on

  • bject, changing the object)
  • Traditional for-loop is impossible, no

loopvariables!

– Instead, recursion or application of a

function on a set (map)

slide-3
SLIDE 3

43

Immutable data

  • New variables/data can be created

(and have to be initialized)

  • Old variables/data cannot be

changed

  • Garbage collection takes care of old

unused data

  • "Opposite" of object-oriented

programming

  • (Traditional for-loop vs. new range-

for)

slide-4
SLIDE 4

44

Functional data structures

  • Data structures (vector, lists,

maps, ...) cannot be changed either!

  • Operations (insert, erase, ...) produce

new (changed) data structures

  • Chain of operations: pass data from
  • peration to operation
  • Efficiency preserved by sharing data
  • Compiler can also internally modify

data structurs as "optimization"

slide-5
SLIDE 5

45

Functional data structures, examples

  • Scala maps: add new element 'john'

with value 1): m2 = m1 + ('john'->1)

  • Scala vectors: "Update" existing

vector by replacing 2nd element v2 = v1.updated(1, 'mary')

  • Haskell arrays: replace first ten

elements with zeroes ar2 = ar // [ (i, 0) | i <- [1..10] ]

slide-6
SLIDE 6

46

Laziness

  • No side-effects, variables do not change
  • → When a function is executed does not

matter!

  • Lazy evaluation: nothing is

evaluated/executed until its value is really needed (= makes difference in the program)

  • Haskell is a purely lazy language
  • Scala & Clojure allow laziness explicitly
  • Example: lazy infinite (generated) data

structures allsquares = [ n*n | n <- [0..] ]

slide-7
SLIDE 7

47

Haskell

  • Pure functional language
  • Compiled, static typing at compilation

time, type deduction

  • Lazy evaluation (nothing is evaluated

before the value is used)

  • Has increased in popularity, other

languages have copied ideas from Haskell

slide-8
SLIDE 8

48

Haskell & lazy evaluation

  • Expressions are evaluated only when used
  • Alternate way of thinking:

– "Values" are parameterless functions that

are executed when needed

– A variable contains a reference to a

function that calculates its value

– (in reality, memoization is used)

  • Very powerful, allows "infinite" data

structures, for example

slide-9
SLIDE 9

49

Haskell lists

  • [1, 2, 3]
  • [1..3]

[1, 2, 3], [1, 3..10] [1, 3, 5, 7, 9]

  • Strings: ['M', 'o', 'i']

"Moi"

  • Extending: 0 : [1, 2, 3]

[0, 1, 2, 3]

  • joining: [0, 1] ++ [2, 3]

[0, 1, 2, 3]

  • Lazy: list = 2 functions, first returns the 1st

element, second the rest of the list.

  • Infinite lists:

– even = [2, 4..] – evensqr = [ n*n | n <- even] – elem 64 evensqr

True

slide-10
SLIDE 10

50

Haskell functions

  • definition: plus x y = x + y
  • call: plus (plus 3 4) 5
  • Piece-wise definition:

fact 0 = 1 fact n = n * fact (n - 1)

  • Parameter pattern matching:

prod [] = 1 prod (fst:rest) = fst * prod rest fact2 n = prod [1..n]

  • As-patterns, wildcards

prod-not-empty par@(_:_) = prod par

slide-11
SLIDE 11

51

Exercise

  • In tradiotional imperative

programming (which you already know), how would you build a program solving the following:

  • You receive a list/vector/whatever
  • f strings, which are sentences

(words + spaces)

  • You should find out the maximum
  • f the average word lengths of the

senteces