1 / 24

An Introduction to F#

An Introduction to F#. Sushant Bhatia @ aboutdev aboutdev.com aboutdev@gmail.com. Why learn F#?. Overview. Tour of F# in Visual Studio 2011 Fundamentals of F# Functional Programming Examples. DEMO. F# in Visual Studio 2011. Fundamentals of F#. Core Types. Modules & Namespaces.

ezhno
Télécharger la présentation

An Introduction to F#

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. An Introduction to F# Sushant Bhatia @aboutdev aboutdev.com aboutdev@gmail.com

  2. Why learn F#?

  3. Overview • Tour of F# in Visual Studio 2011 • Fundamentals of F# • Functional Programming • Examples

  4. DEMO F# in Visual Studio 2011

  5. Fundamentals of F#

  6. Core Types

  7. Modules & Namespaces • Default -> anonymous module • Nest Modules • Namespace

  8. Pipe Forward • Pass results of first function to second • Benefit • Chain functions together • Type inference • [1 .. 10] |> List.map (fun x -> x * x) • |> List.iter (printfn "%i")

  9. Pattern Matching • Series of rules that will execute if a pattern matches the input. • Switch on Steroids

  10. Discriminated Unions • Type • 1 of a set of possible values • Used for complex data structures

  11. Functional Programming

  12. What is Functional Programming? A function is a rule that associates to each x from some set X of values, a unique y from another set Y of values. If f is the name of the function, y = f ( x ) f : X → Y

  13. Functional programming is a programming paradigm that treats computations as the evaluation of mathematical functions and avoids state and mutable data. [Wikipedia]

  14. All programs and procedures are functions and clearly distinguish incoming values from outgoing values • There are no variables or assignments – variables are replaced by parameters • There are no loops – replaced by recursive calls • The value of a function depends only on the value of its parameters and not on the order of evaluation or the execution path that led to the call • Functions are first-class values (viewed as values themselves, computed by other functions and can be parameters to functions) [Programming Languages. Kenneth C Louden]

  15. First Class Functions • Higher Order Functions • Pure Functions • Recursion / Tail Recursion • Currying

  16. 1. First Class Functions a) Bind an identifier to a function definition letsqr = fun n -> n * n b) Store functions in data structures letaTuple = (sqr, fun n -> n + n) • letaList = [sqr, fun n -> n + n]

  17. 2. Higher Order Functions a) Pass function as an argument let data = List.map (fun n -> n * n) [ 1; 2; 3; 4; ] b) Return function as value of function letsqrList = letfunSqr = funlst->List.map (fun a -> a * a) lst funSqr

  18. 3. Pure Functions No Side Effects / Referential transparency Caching optimizations (memoization) No Data Dependency = order of execution is independent and can be parallel (thread-safe)

  19. 4. Recursion / Tail Recursion a) Iteration through recursive invocation let rec factorial x = if x <= 1I then 1I • elseletrecResult = factorial (x-1I) • let result = x * recResult • result b) Tail recursion – Accumulators letfactorialTail x = let rec tailRecFact x acc = if x <= 1I then acc else tailRecFact (x-1I) (acc * x) tailRecFact x 1I

  20. 5. Currying let add x y = x + y letaddToTen = add 10 addToTen 5

  21. Type Provider – Netflix Units of Measure Async / Parallel Demo

  22. Examples – Project Euler

  23. Programming F# - Chris Smith Beginning F# - Robert Pickering Expert F# - Don Syme http://blogs.msdn.com/b/dsyme http://en.wikibooks.org/wiki/Programming:F_Sharp http://projecteuler.net/  http://fdatamining.blogspot.com/2009/12/f-online-videos.html http://www.tryfsharp.org/Tutorials.aspx http://fssnip.net/ How to Learn F#

  24. 2 e-books Giveaway / Raffle

More Related