lambda
play

Lambda Usefull outside functional programming, functions nowadays - PowerPoint PPT Presentation

Nameless functions, "functions as values" or "function literals" Lambda Usefull outside functional programming, functions nowadays also in Java, C++, Python, C#, ... Compare "int i = 3;" and


  1. ● Nameless functions, "functions as values" or "function literals" Lambda ● Usefull outside functional programming, functions nowadays also in Java, C++, Python, C#, ... ● Compare "int i = 3;" and "3" with "void f(int i){...}" and "[](int i){...}" (C++ syntax), named function = "variable containing a lambda" ● Used heavily in functional programming, since functions are 1st class citizens ● Uses: passing short functions as parameters, storing them... 61

  2. Named and Named function: nameless <func_name> <formal_params> = <expr> ) functions square x = x * x square 5 Nameless function (lambda): \ <formal_param> -> <expr> \x -> x * x ( \x -> x * x ) 5 square 5 square = \x -> x * x 62

  3. ● Functions are 1st class citizens ● Higher order functions: functions that Higher order functions take functions as parameters and/or return them ● A little bit like function pointers in C/C++/... ● Store functionality in data structures, create new functionality based on that, call later 63

  4. ● Functions as variable/parameter values: next x = x + 1 Functions as make = next; make 4 ➜ 6 5 1 st class use3 f = f 3 citizens use3 next ➜ 6 4 map next [1, 2, 3] ➜ 6 [2, 3, 4] frac3 n = foldl (*) 1 [1..n] ● Lambda-functions: use (\x -> x*x) ➜ 6 9 filter (\x -> x*x<10) [1..] ➜ 6 [1, 2, 3] ● (C++11:) for_each(v.begin(), v.end(), [](int x){cout<<x}); 64

  5. ● In Haskell all functions have just one parameter: Currying plus x y = x + y t: plus ➜ 6 plus :: a -> a -> a ● plus takes a parameter x and returns another function, that takes y and calculates the result ● ("Values" can be thought of as functions with no parameters) 65

  6. ● Currying makes it possible to use only some of the parameters: Currying plus = x + y plus3 = plus 3 :t plus3 ➜ 6 plus3 :: a -> a plus3 8 ➜ 6 11 map plus3 [2, 4, 6] ➜ 6 [5, 7, 9] 66

  7. ● I/O is a side-effect and demands strictly ordered execution, which contradicts I/O in Haskell functional paradigm ● Haskell uses monads ● Complicated, not discussed here in detail ● Idea: I/O-functions have their own "category", I/O-functions are chained with invisible parameters, forcing sequential execution ● Monads have many other uses too... 67

  8. ● Logic programming paradigm – "Logic programming can be viewed as Logic controlled deduction." programming ● Logic program – expressing facts and rules about some problem domain ● Execution of a program – Searching for a result that fulfills the rules 68

  9. ● Declarative semantics – Simpler than for imperative languages Features of – (Functional programming also Logic declarative) ● Programmer describes the result, programming not how it is achieved – e.g., how to sort vs. ”The challenge of what does "sorted" imperative programming mean is that all details – Sometimes solving of computation strategy can be given must be expressed.” sort ( old_list, new_list )  permutation ( old_list, new_list )  sorted ( new_list ) sorted ( list )   j: 1  j < n, list ( j )  list ( j + 1 ) 69

  10. ● "Modern" research language ● Combines functional and logic Curry - paradigms functional ● Several implementations exist logic ● Syntax (almost) from Haskell language ● Adds features for logic programming: – Free variables – Non-deterministic functions – Logical constraints, built-in search (with several search strategies) 70

  11. Non-deterministic functions ● f x = x New in Curry f x = x+1 (vs Haskell) f 3 – Haskell: first match is chosen, 3 returned – Curry: both matches chosen, two execution branches, both 3 and 4 returned ● Builtin choice-operator ?: 0 ? 1 returns both 0 and 1 71

  12. Partial functions ● empty [] = [] New in Curry (vs Haskell) empty [3] – Haskell: Run-time error – Curry: "No solution" (continue search for other solutions) 72

  13. Constraints ● equality =:= , constraint combinator & (and New in Curry &> ), anything returning success (vs Haskell) ● Unlike booleans ( == ), requires constraint to hold ● Can be used to limit function definitions (other major uses too) f x y | x =:= reverse y = x++y function defined only for some lists ● "Functional patterns" do this implicitly: last (_++[e]) = e 73

  14. Free variables ● Value not known beforehand New in Curry (vs Haskell) ● Curry tries to deduce (search for) the value based on constraints (unification) ● [1]++x =:= [1,2,3] where x free returns binding x=[2,3] 74

  15. ● take 3 x =:= [1,2,3] & reverse x =:= x where x free Curry ● insert x ys = x:ys examples insert x (y:ys) = y : insert x ys permutate [] = [] permutate (x:xs) = insert x (permutate xs) ● permutate [1,2,3,1] =:= a++[1,2]++b where a,b free 75

  16. ● Controlling the order of resolution – efficiency Problems – Infinite loops possible with logic ● Closed-world assumption paradigm – A goal can be proven but not disproven ● Natural limits – No need to describe computation – However, different solution strategies differ in efficiency 76

  17. ● Relational databases – Input (facts) Applications – Database relations (rules) for logic – Queries programming ● AI ● Expert systems – Fact-based deduction ● Language processing – top-down –resolution resembles natural language ● Teaching 77

Recommend


More recommend