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

lambda
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

61

Lambda functions

  • Nameless functions, "functions as values"
  • r "function literals"
  • Usefull outside functional programming,

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...

slide-2
SLIDE 2

62

Named and nameless functions

<func_name> <formal_params> = <expr> ) \ <formal_param> -> <expr> square x = x * x square 5 \x -> x * x square = \x -> x * x ( \x -> x * x ) 5 square 5

Named function: Nameless function (lambda):

slide-3
SLIDE 3

63

Higher order functions

  • Functions are 1st class citizens
  • Higher order functions: functions that

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

slide-4
SLIDE 4

64

Functions as 1st class citizens

  • Functions as variable/parameter values:

next x = x + 1 make = next; make 4 5 ➜ 6 use3 f = f 3 use3 next 4 ➜ 6 map next [1, 2, 3] [2, 3, 4] ➜ 6 frac3 n = foldl (*) 1 [1..n]

  • Lambda-functions:

use (\x -> x*x) 9 ➜ 6 filter (\x -> x*x<10) [1..] [1, 2, 3] ➜ 6

  • (C++11:)

for_each(v.begin(), v.end(), [](int x){cout<<x});

slide-5
SLIDE 5

65

Currying

  • In Haskell all functions have just one

parameter: plus x y = x + y t: plus plus :: a -> a -> a ➜ 6

  • 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)

slide-6
SLIDE 6

66

Currying

  • Currying makes it possible to use only some
  • f the parameters:

plus = x + y plus3 = plus 3 :t plus3 plus3 :: a -> a ➜ 6 plus3 8 11 ➜ 6 map plus3 [2, 4, 6] [5, 7, 9] ➜ 6

slide-7
SLIDE 7

67

I/O in Haskell

  • I/O is a side-effect and demands strictly
  • rdered execution, which contradicts

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...
slide-8
SLIDE 8

68

Logic

programming

  • Logic programming paradigm

– "Logic programming can be viewed as

controlled deduction."

  • Logic program

– expressing facts and rules about some problem domain

  • Execution of a program

– Searching for a result that fulfills the rules

slide-9
SLIDE 9

69

Features of Logic programming

  • Declarative semantics

– Simpler than for imperative languages – (Functional programming also declarative)

  • Programmer describes the result,

not how it is achieved

– e.g., how to sort vs. what does "sorted" mean – Sometimes solving strategy can be given

sort ( old_list, new_list )  permutation ( old_list, new_list )  sorted ( new_list ) sorted ( list )  j: 1  j < n, list ( j )  list ( j + 1 ) ”The challenge of imperative programming is that all details

  • f computation

must be expressed.”

slide-10
SLIDE 10

70

Curry - functional logic language

  • "Modern" research language
  • Combines functional and logic

paradigms

  • Several implementations exist
  • Syntax (almost) from Haskell
  • Adds features for logic programming:

– Free variables – Non-deterministic functions – Logical constraints, built-in search

(with several search strategies)

slide-11
SLIDE 11

71

New in Curry (vs Haskell)

Non-deterministic functions

  • f x = x

f x = x+1 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

slide-12
SLIDE 12

72

New in Curry (vs Haskell)

Partial functions

  • empty [] = []

empty [3]

– Haskell: Run-time error – Curry: "No solution" (continue search

for other solutions)

slide-13
SLIDE 13

73

New in Curry (vs Haskell)

Constraints

  • equality =:=, constraint combinator & (and

&>), anything returning success

  • 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

slide-14
SLIDE 14

74

New in Curry (vs Haskell)

Free variables

  • Value not known beforehand
  • 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]

slide-15
SLIDE 15

75

Curry examples

  • take 3 x =:= [1,2,3] & reverse x

=:= x where x free

  • insert x ys = x:ys

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

slide-16
SLIDE 16

76

Problems with logic paradigm

  • Controlling the order of resolution

– efficiency – Infinite loops possible

  • Closed-world assumption

– A goal can be proven but not disproven

  • Natural limits

– No need to describe computation – However, different solution strategies differ in efficiency

slide-17
SLIDE 17

77

Applications for logic programming

  • Relational databases

– Input (facts) – Database relations (rules) – Queries

  • AI
  • Expert systems

– Fact-based deduction

  • Language processing

– top-down –resolution resembles natural language

  • Teaching