F#<br />function theory<br />
F#<br />type inference<br />
//Brackets aren't mandatory:<br />let f(x,y) = x+y<br />let f x y  = x+y<br />
//Function type:<br />let I x = x<br />//    x: 'a<br />//    I: 'a -> 'a<br />
//Example 2:<br />let K x y = x<br />//      x: 'a<br />//      y: 'b<br />//      K: 'a -> 'b -> 'a<br />
//Partial application<br />let constHi = K "Hello"<br />let hi = constHi 123<br />val constHi : (int -> string)<br />//hi ...
//Example 3:<br />let S x y z = x(z)(y(z))<br />//        x:  f(a  b)<br />//        x:  a -> b -> c<br />//        z:  a<...
//Exercise: What are these functions?<br />val it : <br />(('a -> 'b) -> 'a list -> 'b list)<br />val it : <br />(('a -> '...
f(g(x))<br />F#<br />function composition<br />
//function composition<br />let combine f g x = g(f(x)) <br />Let combine f g x = (f>>g)x<br />//        f:  'a -> 'b<br /...
//You can remove the parameter from the last one!<br />let combine f g= (f>>g)<br />// combine: ('a -> 'b) -><br />       ...
     result: generic abstraction   whitout re-        ference to concrete              types<br />let handle = (save >> va...
F#<br />monads<br />
thought behind LINQ<br />“list monad”IEnumerable<T><br />“maybe monad”Nullable<T><br />…<br />
three types of operations<br />1.<br />3.<br />'a -> M<’a><br />M<'a> -> 'b<br />2.<br />M<'a> -> M<'b><br />
typical example<br />2.<br />M<'a> -> ('a -> M<'b>) -> M<'b><br />3.<br />M<'a> -> <br />'b -> ('b -> 'a -> 'b) -> 'b<br />
F#<br />combinators<br />
S- K- I- combinators<br />Like we started:<br />I x = x<br />K x y = x<br />S x y z =<br />    x z (y z)<br />
y-combinator<br />f(x) when x=f?<br />f(f) = f  and  f=f(f)<br />f(g) = g(f(g))<br />Y = S (K (S I I)) (S (S (K S) K) (K (...
references / links<br />http://en.wikipedia.org/wiki/SKI_combinator_calculus<br />http://en.wikipedia.org/wiki/Fixed_point...
Upcoming SlideShare
Loading in …5
×

Function therory

655 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
655
On SlideShare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
11
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • When functions don’t have side effects we can actually define what they do (and correctness) purely by looking the types.
  • “function composition”: To combine two functions into a new one.“For X, first apply g, then f.”, Or “f of g of x”
  • No stiffness of object-oriented programming Possibility to make top-down design
  • Often we want to combine things that are not so directly linked to each other… Monad term is from Haskell where it is actually the only way to make side effects
  • Actually Nullable&lt;T&gt; is just a list with zero or one items.
  • Add things to thedomainModify: Handle the domainGet items from the domain
  • 2: (a -&gt; M&lt;b&gt;) is a transformation function (formap/select/…)3: (b -&gt; a -&gt; b) function is a collector/selector function and b is kind of an accumulator to collect the output result.
  • By chaining these we can create anything (without named parameters) I is unnecessary as I 5 = S (K) (K) (5) One ”Church-man&quot; e.g. proved that we can make numbers and boolean logic just with functions. There is even a programming language based only combining these functions.
  • What if f(x) function would get input itself, I mean the f? Will become an recursion and infinite loop… The loop and the ending-condition-function can be separated to different functions Y can of course be made just with SKI-calculus
  • Function therory

    1. 1. F#<br />function theory<br />
    2. 2. F#<br />type inference<br />
    3. 3. //Brackets aren't mandatory:<br />let f(x,y) = x+y<br />let f x y = x+y<br />
    4. 4. //Function type:<br />let I x = x<br />// x: 'a<br />// I: 'a -> 'a<br />
    5. 5. //Example 2:<br />let K x y = x<br />// x: 'a<br />// y: 'b<br />// K: 'a -> 'b -> 'a<br />
    6. 6. //Partial application<br />let constHi = K "Hello"<br />let hi = constHi 123<br />val constHi : (int -> string)<br />//hi : string ="Hello"<br />
    7. 7. //Example 3:<br />let S x y z = x(z)(y(z))<br />// x: f(a b)<br />// x: a -> b -> c<br />// z: a<br />// y: a -> b<br />S x y z: (a -> b -> c) -> (a -> b) -> a -> c<br />
    8. 8. //Exercise: What are these functions?<br />val it : <br />(('a -> 'b) -> 'a list -> 'b list)<br />val it : <br />(('a -> 'b -> 'a) -> 'a -> 'b list -> 'a)<br />val it : ('a * 'b -> 'a)<br />
    9. 9. f(g(x))<br />F#<br />function composition<br />
    10. 10. //function composition<br />let combine f g x = g(f(x)) <br />Let combine f g x = (f>>g)x<br />// f: 'a -> 'b<br />// g: 'b -> 'c<br />// x: 'a<br />combine f g x: (a -> b) -> (b -> c) -> a -> c<br />
    11. 11. //You can remove the parameter from the last one!<br />let combine f g= (f>>g)<br />// combine: ('a -> 'b) -><br /> ('b -> 'c) -><br /> ('a -> 'c)<br />
    12. 12. result: generic abstraction whitout re- ference to concrete types<br />let handle = (save >> validate >> send) <br />
    13. 13. F#<br />monads<br />
    14. 14. thought behind LINQ<br />“list monad”IEnumerable<T><br />“maybe monad”Nullable<T><br />…<br />
    15. 15. three types of operations<br />1.<br />3.<br />'a -> M<’a><br />M<'a> -> 'b<br />2.<br />M<'a> -> M<'b><br />
    16. 16. typical example<br />2.<br />M<'a> -> ('a -> M<'b>) -> M<'b><br />3.<br />M<'a> -> <br />'b -> ('b -> 'a -> 'b) -> 'b<br />
    17. 17. F#<br />combinators<br />
    18. 18. S- K- I- combinators<br />Like we started:<br />I x = x<br />K x y = x<br />S x y z =<br /> x z (y z)<br />
    19. 19. y-combinator<br />f(x) when x=f?<br />f(f) = f and f=f(f)<br />f(g) = g(f(g))<br />Y = S (K (S I I)) (S (S (K S) K) (K (S I I)))<br />
    20. 20. references / links<br />http://en.wikipedia.org/wiki/SKI_combinator_calculus<br />http://en.wikipedia.org/wiki/Fixed_point_combinator<br />http://community.bartdesmet.net/blogs/bart/archive/2009/08/17/mis-using-c-4-0-dynamic-type-free-lambda-calculus-church-numerals-and-more.aspx<br />http://blogs.msdn.com/b/madst/archive/2007/05/11/recursive-lambda-expressions.aspx<br />http://www.madore.org/~david/programs/unlambda/<br />http://channel9.msdn.com/Shows/Going+Deep/Brian-Beckman-Dont-fear-the-Monads<br />http://channel9.msdn.com/Shows/Going+Deep/C9-Lectures-Greg-Meredith-Monadic-Design-Patterns-for-the-Web-Introduction-to-Monads<br />http://blogs.msdn.com/b/wesdyer/archive/2008/01/11/the-marvels-of-monads.aspx<br />http://channel9.msdn.com/Shows/Going+Deep/Bart-De-Smet-MinLINQ-The-Essence-of-LINQ<br />http://ttic.uchicago.edu/~dreyer/course/papers/wadler.pdf<br />http://www.meetup.com/FSharpHelsinki/messages/boards/forum/1395955<br />

    ×