properties of
play

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


  1. ● 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 ● Higher-order functions – Functions that operate on other functions, not normal data ● Free order of execution – Order of evaluation does not effect the outcome • Lazy and parallel evaluation ● Implicit memory handling 41

  2. ● No assignment: "variable" is a questionable concept (doesn't vary...) Functionality ● "Opposite" of object-oriented programming (methods invoked on object, changing the object) ● Traditional for-loop is impossible, no loop variables ! – Instead, recursion or application of a function on a set (map) 42

  3. ● New variables/data can be created (and have to be initialized) Immutable ● Old variables/data cannot be data changed ● Garbage collection takes care of old unused data ● "Opposite" of object-oriented programming ● (Traditional for-loop vs. new range- for) 43

  4. ● Data structures (vector, lists, maps, ...) cannot be changed either! Functional ● Operations (insert, erase, ...) produce data new (changed) data structures structures ● Chain of operations: pass data from operation to operation ● Efficiency preserved by sharing data ● Compiler can also internally modify data structurs as "optimization" 44

  5. ● Scala maps: add new element 'john' with value 1): Functional m2 = m1 + ('john'->1) data ● Scala vectors: "Update" existing structures, vector by replacing 2nd element examples v2 = v1.updated(1, 'mary') ● Haskell arrays: replace first ten elements with zeroes ar2 = ar // [ (i, 0) | i <- [1..10] ] 45

  6. ● No side-effects, variables do not change ● → When a function is executed does not Laziness 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..] ] 46

  7. ● Pure functional language ● Compiled, static typing at compilation Haskell time, type deduction ● Lazy evaluation ( nothing is evaluated before the value is used) ● Has increased in popularity, other languages have copied ideas from Haskell 47

  8. ● Expressions are evaluated only when used ● Alternate way of thinking: Haskell & – "Values" are parameterless functions that lazy are executed when needed evaluation – 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 48

  9. ● [1, 2, 3] ● [1..3] Haskell lists � [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 49

  10. ● definition: plus x y = x + y ● call: plus (plus 3 4) 5 Haskell functions ● 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 50

  11. ● In tradiotional imperative programming (which you already Exercise know), how would you build a program solving the following: ● You receive a list/vector/whatever of strings, which are sentences (words + spaces) ● You should find out the maximum of the average word lengths of the senteces 51

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend