The document discusses F# and the Dynamic Language Runtime (DLR). It provides an overview of F# as a functional-first multi-paradigm language for .NET that allows for imperative and object-oriented programming. It also describes the DLR and how it enables dynamic languages like Python and Ruby to run on .NET and interoperate with existing .NET code. The DLR architecture is explained along with use cases like test-driven development, web development, and embedding dynamic languages in applications.
1. F# AND THE DLR Richard Minerich Senior Researcher at Bayard Rock F# MVP of the Year RichardMinerich.com
2. The tools we use have a profound (and devious!) influence on our thinking habits, and, therefore, on our thinking abilities. -- Edsger Dijkstra
3. Problems in Modern Programming Adding functionality to programs is difficult Exploring an existing code base is very difficult Architecture is hard Multi-threading is very hard Algorithms are difficult to both write and read later Why? Our Object-Oriented heritage.
4. F# vs Dynamic F# Dynamic Languages Constructs conform to types and are fixed at runtime Types conform to constructs and change at runtime Data is immutable by default Generally data is mutable by default Protection from many kinds of mistakes No protection, even from typos Native interop with existing .NET Programs and Libraries Some work required for interop, but it’s not difficult Somewhat difficult to learn Very easy to learn
5. What is F#? F# is a Multi-Paradigm Language Functional with Some Type Extensions Allows for Object Oriented and Imperative Style Code F# is Interactive Command Line, Compile or Interactive Window F# is a .NET CLR Language In the box with Visual Studio 2010 Native Interop with every other .NET Language F# is Open Source But maintained and supported by Microsoft
6. If you love to code, you’ll love F#. Immutable Data Structures Tail Recursion Discriminated Unions Partial Application Function Composition Quotations Computation Expressions Statically Resolved Type Parameters
11. Functional Programming in F# All statements return something let x = if (y > 10) then 10 else y Everything is immutable by default let rootList = [ 1; 2; 3 ] rootList @ [ 4 ] Chains, instead of discrete statements urls |> List.map download |> Async.Parallel
13. Recursion and TCO Using functions which call themselves. Function > let rec badfib n = if n < 3 then 1 else badfib (n - 1) + badfib (n - 2);; val badfib : int -> int > let rec fib n1 n2 c = if c = 1 then n2 else fib n2 (n1 + n2) (c - 1);; val fib: int -> int -> int -> int
14. Higher Order Functions Pass Functions into Functions Return Functions from Functions Functions and Values are Interchangeable > let findBestSort list = if List.length list < 1000 then insertionSort else quickSort;; val findBestSort : 'a list -> ('b list -> 'b list) when 'b : comparison Function Function Value Value Function Function Function Value is a
15. Lambda Expressions A function doesn’t always have a name. > let addTwo = ( fun x -> x + 2);; val addTwo : int -> int > List.map ( fun x -> x * 2) [1; 2; 3; 4; 5] [2; 4; 6; 8; 10] let value = (fun v -> v) myFunc (fun v -> v) arg
16. Pipelining Push data through a series of functions. > [1; 2; 3; 4; 5] |> List.map ( fun x -> x * 2) |> List.filter ( fun x -> x % 3 <> 0);; [2; 4; 8; 10] Data function function function |> |> |> lambda lambda
17. Matching A switch statement on steroids. match num with | 1 -> "One" | x when x = 2 -> "Two" | _ -> "Higher than I can count“ match exact case when | | -> variable with function -> function -> function name | -> function name predicate | active pattern
18. Function Composition Combine functions at runtime let addOne x = x + 1 let addTwo x = x + 2 let addThree x = x + 3 let addSix = addOne >> addTwo >> addThree ‘ a -> ‘b ‘ b -> ‘c ‘ a -> ‘c >> =
19. Discriminated Unions A type that can be one of several other types type name | signature name = name | signature type Option<'a> = | Some of 'a | None let printOutput opt = match opt with | Some (val) -> printfn “%A” val | None -> printfn “No Value” of of
20. And More! Statically Resolved Type Parameters Special Record Types Object Expressions Comprehensions Computation Expressions Quotations
23. Find Some Functional Friends The Community For F# Online www.communityforfsharp.net The NYC F# User Group www.meetup.com/nyc-fsharp The Cambridge F# User Group fsug.org The San Francisco Bay Area F# User Group www.sfsharp.org F#unctional Londoners www.meetup.com/FSharpLondon
25. What is the DLR? The DLR is a layer on top of the CLR Designed for dynamic languages Enables interop with running CLR programs and .NET libraries Compatible Implementations Python, Ruby, Javascript, and Scheme It’s Fast IronPython is about 2x the speed of Python The Languages are Open Source Community maintained and not directly supported
26. The DLR is great for… Test Driven Development Web Development Using Code from Existing Dynamic Programs Writing your own Language Embedding in Applications
29. Metaobject Visualization Users and programs interact with metaobjects, not directly with their backing objects as in C#, VB.NET or F#. control create invoke
32. DLR Books and Resources http://www.silverlight.net/learn/dynamic-languages/ http://is.gd/DLRFAQ
33. Thank You Slides and code will be available on RichardMinerich.com later today. [email_address]
Editor's Notes
C# is OO/Imperative, why is functional important?
F# has a ton of new constructs to explore and will open your mind to completely new ways of doing things. Things you never thought possible before. You don’t need to understand of this stuff to enjoy F#, but it has a lot to explore if you’re into that kind of thing. Even if you don’t use F# professionally, just learning how these things work will make you a better programmer in general.
If you care about getting things done quickly, and programming is just a means to an end F# can get you there quickly and safely.
More, Better, Natively Concurrent Code in Less Time
F# has a lot of great graphics frameworks. You can make beautiful graphics with very little code. FsChart (left), VSLab (middle top), Tomas’s F# Library for Learning Graphics (top right), Jon Harrop F# for Visualization (bottom right) Tomas’s Graphics Lib: https://github.com/tpetricek/Documents/tree/master/Talks%202011/Teaching%20F%23%20(Community%20for%20F%23)
It fits together like puzzle pieces.
You can perform any type of iteration with a recursive function. F# optimizes a special type of recursive functions into loops using a method called tail call optimization. Tail call optimization
Lambda expressions are great because they reduce our need to make imperative style variable assignments. Small functions can just be included inline.
Commonly used with higher order functions and lambda expressions
Sticker question Answer: “Less than ten” Match on types, contents of types, strings, lists…. Very flexible
This is used to avoid the problem of nulls New values not in a match will cause warnings (or even failure if the compiler is told to do so) They can even be recursive and be used to build strongly typed tree structures
I wrote an ant colony AI simulation in Silverlight with uploadable AI. Give it a go when you get some time.
You only need parse and lex your way to an expression tree. The DLR takes care of everything from there.
Slide care of Kevin Hazzard
You can write your whole Silverlight application without a single line of compiled code