What the math geeks don't want you to know about F#

20
What the Math Geeks Don't Want You to Know about F# Tips for Overcoming Obstacles to Learning F# August 2010 Kevin Hazzard, C# MVP

description

Microsoft’s recently launched F# programming language is only for math geeks, right? If you don’t know what Turing Completeness is all about or why the Lambda Calculus is so important, that invitation you’ve anxiously been waiting on to the secret F# meeting in your town just isn’t coming. Sorry. At least that’s what it seems like. But there’s a whole different story about F# just waiting to be told: a story that includes all of us just trying to solve hard problems with the tools we have. At the end of the day, F# is just another great tool. You already have a lot of tools in the bag today: C#, VB.NET, Java, JavaScript, CSS, HTML, PL/SQL and T-SQL, maybe even some Python or Ruby. Building a modern software application, there are so many choices. A few of them are right and many of them are disasters just waiting to happen. F# can help you solve some problems in elegant ways where languages like C# and Java might make you struggle. Of course, you don’t have to write your entire application in F# any more than you would trying writing a web application without HTML, CSS and JavaScript. Come learn how to weave little bits of F# code into the fabric of your larger application to make it scalable, highly performant and expressive. Apologies to the math geeks ahead of time but we won’t be discussing monads, universal computers or anything else that might distract us from learning about this new language or how to use it during this talk.

Transcript of What the math geeks don't want you to know about F#

Page 1: 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

Page 2: What the math geeks don't want you to know about F#

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?

Page 3: What the math geeks don't want you to know about F#

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

Page 4: What the math geeks don't want you to know about F#

A Teaching Example

type BinaryTree<'a> =

| Leaf of 'a

| Node of BinaryTree<'a> * BinaryTree<'a>

Page 5: What the math geeks don't want you to know about F#

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.

Page 6: What the math geeks don't want you to know about F#

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.

Page 7: What the math geeks don't want you to know about F#

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

Page 8: What the math geeks don't want you to know about F#

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;

}

Page 9: What the math geeks don't want you to know about F#

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

Page 10: What the math geeks don't want you to know about F#

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

Page 11: What the math geeks don't want you to know about F#

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.

Page 12: What the math geeks don't want you to know about F#

Workflows (Computation Expressions)

• Bind

• Delay

• Return

• ReturnFrom

• Combine

• For

• TryFinally

• TryWith

• Using

• While

• YieldFrom

• Zero

Page 13: What the math geeks don't want you to know about F#

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.

Page 14: What the math geeks don't want you to know about F#

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

Page 15: What the math geeks don't want you to know about F#

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

Page 16: What the math geeks don't want you to know about F#

Don't Get Hung Up On Terms

• Lambda Calculus

• Turing Completeness

• Universal Computer

• Endofunctors

• Cata/Ana-morphism

• Co-algebras

• Category Theory

Page 17: What the math geeks don't want you to know about F#

Stuff That Is Important

• Composition / Currying

• Pattern Matching

• Type inference

• Immutability

• Side-effects

• Recursion

• Workflows

Page 18: What the math geeks don't want you to know about F#

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.

Page 19: What the math geeks don't want you to know about F#

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

Page 20: What the math geeks don't want you to know about F#

Demo

• Check out the F# Samplescode.msdn.microsoft.com/fsharpsamples

– Explore Tutorial.fs with Alt + Enter

– Samples101