function theoryF#
type inferenceF#
//Brackets aren't mandatory:
let f(x,y) = x+ylet f x y = x+y
//Function type:
let I x = x// x: 'a// I: 'a -> 'a
//Example 2:
let K x y = x// x: 'a// y: 'b// K: 'a -> 'b -> 'a
//Partial application
let constHi = K "Hello"let hi = constHi 123
val constHi : (int -> string)//hi : string ="Hello"
//Example 3:
let S x y z = x(z)(y(z))// x: f(a b)// x: a -> b -> c// z: a// y: a -> bS x y z: (a -> b -> c) -> (a -> b) -> a -
> c
//Exercise: What are these functions?
val it : (('a -> 'b) -> 'a list -> 'b list)
val it : (('a -> 'b -> 'a) -> 'a -> 'b list -> 'a)
val it : ('a * 'b -> 'a)
function composition
F#
f(g(x))
//function composition
let combine f g x = g(f(x)) Let combine f g x = (f>>g)x// f: 'a -> 'b// g: 'b -> 'c// x: 'acombine f g x: (a -> b) -> (b -> c) -> a -
> c
//You can remove the parameter from the last one!
let combine f g= (f>>g)// combine: ('a -> 'b) -> ('b -> 'c) -> ('a -> 'c)
result: generic abstraction whitout re- ference to
concrete types
let handle = (save >> validate >> send)
monadsF#
thought behind LINQ
“list monad”IEnumerable<T>
“maybe monad”Nullable<T>
…
three types of operations
1.
2.
3.'a -> M<’a>
M<'a> -> M<'b>
M<'a> -> 'b
typical example
2.
3.
M<'a> -> ('a -> M<'b>) -> M<'b>
M<'a> -> 'b -> ('b -> 'a -> 'b) -> 'b
combinatorsF#
S- K- I- combinators
Like we started:I x = x
K x y = x
S x y z = x z (y z)
y-combinator
Y = S (K (S I I)) (S (S (K S) K) (K (S I I)))
f(x) when x=f?f(f) = f and f=f(f)
f(g) = g(f(g))
references / links• http://en.wikipedia.org/wiki/SKI_combinator_calculus
• http://en.wikipedia.org/wiki/Fixed_point_combinator
• 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
• http://blogs.msdn.com/b/madst/archive/2007/05/11/recursive-lambda-expressions.aspx
• http://www.madore.org/~david/programs/unlambda/
• http://channel9.msdn.com/Shows/Going+Deep/Brian-Beckman-Dont-fear-the-Monads
• http://channel9.msdn.com/Shows/Going+Deep/C9-Lectures-Greg-Meredith-Monadic-Design-Patterns-for-the-Web-Introduction-to-Monads
• http://blogs.msdn.com/b/wesdyer/archive/2008/01/11/the-marvels-of-monads.aspx
• http://channel9.msdn.com/Shows/Going+Deep/Bart-De-Smet-MinLINQ-The-Essence-of-LINQ
• http://ttic.uchicago.edu/~dreyer/course/papers/wadler.pdf
• http://www.meetup.com/FSharpHelsinki/messages/boards/forum/1395955