What the math geeks don't want you to know about F#
-
Upload
kevin-hazzard -
Category
Technology
-
view
4.001 -
download
2
description
Transcript of What the math geeks don't want you to know about F#
What the Math Geeks Don't Want You to Know about F#
Tips for OvercomingObstacles to Learning F#
August 2010Kevin Hazzard, C# MVP
What This Talk Is & Is Not
• This talk does not intend to teach F# deeply
• This talk is for C# / VB.NET programmers trying to answer the questions:
– Why should I care about functional languages?
– What makes F# worth learning?
– What's going to slow me down on the journey?
What makes a language functional?
• Compositional and often expression-oriented
• Supports functions as first-class types
• Often promotes lazy evaluation
• Extensive use of higher-order functions
• Avoids memory and I/O side-effects
• Prefers recursion over iteration
• Excellent for concurrent programming
• May have polymorphic data types withextensive pattern matching capabilities
A Teaching Example
type BinaryTree<'a> =
| Leaf of 'a
| Node of BinaryTree<'a> * BinaryTree<'a>
Obstacle #1
• Pattern matching is at the core of F#
• When learning F#, focus on every pattern matching example you can find
• Start with the simple ones, e.g. discriminated unions, move up and out
• Treat this learning as a block and learn it deeply, in a very focused way
Pattern matching is foreign and pervasive.
Obstacle #2
• If you're a Pythonista, you will feel at home writing F# - the types get out of your way
• If not…
– Watch carefully for inferred types, especially while you are learning – use the FSI REPL a lot
– Use automatic generalization to reduce code volume and increase simplicity
Type inference makes F# feel like a scripting language.
Automatic Generalization
• No obstacle but something to be aware of
• What type does this operate on?
let max a b = if a >b then a else b
• F# automatically generalizes complete expressions when it can
Recursion – Port This to F#
public inthcf( intx, inty )
{
int result = 0;
while (result == 0) {
if (x<y) {
y %= x;
if (y == 0)
result = x;
}
else {
x %= y;
if (x == 0)
result = y;
}
}
return result;
}
Isn't This Succinct?
let rechcf a b =if a = 0 then belifb = 0 then aelif a <b then hcf a (b % a)else hcf (a % b) b
Tail Recursion Optimization
• Recursive functions that do no extra work after the recursion can often be converted to while loops
• F# does this automatically if the code is structured to allow for it
• The JITter will sometimes do this on compiled code
• No guarantees though
Obstacle #3
• Many of us were taught to fear recursion
• Stop thinking that way
• Every time you write an iteration think,"Could this be done with recursion instead?"
• Write your F# code so that it is tail optimizable
Recursion may be at odds with your belief system.
Workflows (Computation Expressions)
• Bind
• Delay
• Return
• ReturnFrom
• Combine
• For
• TryFinally
• TryWith
• Using
• While
• YieldFrom
• Zero
Obstacle #4
• Think of workflows like little Domain Specific Languages (DSL)
• Builders are like AOP-enabled classes, allowing you to hook and define those 12 core functions
• When the F# code in the computation expression is evaluated, your code gets to direct the work
• Focus on understanding the built-in asyncworkflow
Workflows are central to F# and tough to understand.
VS Gallery F# Templates
• When you first look at the F# templates in VS 2010, you may be underwhelmed
• The F# team and others are always adding F# templates to ease development
• Daniel Mohl (@dmohl) has written some excellent templates that are available in the online gallery
Obstacle #5
• F# is part of the .NET ecosystem– Great tooling in Visual Studio 2010
– Full access to all .NET types and members
– Seamless access to & from C# and VB.NET
• F# is multi-paradigm– Emphasizes data immutability and the evaluation
of expressions
– But also supports traditional OOP concepts
You must rewire your brain a bit to learn F#.
Don't Get Hung Up On Terms
• Lambda Calculus
• Turing Completeness
• Universal Computer
• Endofunctors
• Cata/Ana-morphism
• Co-algebras
• Category Theory
Stuff That Is Important
• Composition / Currying
• Pattern Matching
• Type inference
• Immutability
• Side-effects
• Recursion
• Workflows
Obstacle #6
• Anyone want to guess how long functional programming has been around?
• OOP dominated because it was the best way to reduce complexity and promote reuse in CPU-bound systems
• Functional languages are leader languages– Data structures– Message and event passing– Dynamic typing and type inferencing– Generics– Garbage collection
Functional programming is a new fad. It will pass.
The History of F#
• Started in 2002 in Microsoft Research
• Based on languages like ML and OCaml
• Influenced great stuff in .NET
– Generics
– LINQ
• Visual Studio support in versions 2005 & 2008
• Became a first-class language citizen inVisual Studio 2010
Demo
• Check out the F# Samplescode.msdn.microsoft.com/fsharpsamples
– Explore Tutorial.fs with Alt + Enter
– Samples101