1 / 18

Cody Coleman, Matt Davis, and Mel Green

F#. Cody Coleman, Matt Davis, and Mel Green. History of F#. Started in 2002 Had it’s first major release in 2005 Created by Don Syme, and backed by Microsoft Research Group. Sought to combine the power of typed functional programming with the strengths of .Net. Influenced By.

zihna
Download Presentation

Cody Coleman, Matt Davis, and Mel Green

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. F# Cody Coleman, Matt Davis, and Mel Green

  2. History of F# • Started in 2002 • Had it’s first major release in 2005 • Created by Don Syme, and backed by Microsoft Research Group. • Sought to combine the power of typed functional programming with the strengths of .Net

  3. Influenced By • It’s predecessors include, OCaml, ML, Haskell, and .Net languages. • It’s considered OCaml for .Net. • F# has encapsulated and build upon many of OCaml’s strengths • Haskell influenced Sequence Expressions and Workflows. • F# inherited hundreds of important and major implementation stack libraries like LINQ.

  4. Types • Tuples string * int • Records type Person = { Name:string Age:int } • Lists [ 2; 3; 4; 5 ] • Unions type PayType = | Salary of float | Wage of int

  5. Functions let rec fib n = match n with | 1 | 2 -> 1 | _ -> fib(n-2) + fib(n-1) • Lambda fun x -> x + 1 • Currying let add x y = x + y

  6. Asynchronous Workflows let task1 = async { return factorial 10 } let task2 = async { return factorial 5 } Async.Run (Async.Parallel [ task1; task2 ]) Result: [ 3628800; 120; ]

  7. Piping let algorithm n = n |> (fun x -> x + 1) |> (fun x -> x * 2) |> (fun x -> [0 .. x]) |> List.map (fun x -> x.ToString()) let result = List.nth (algorithm 3) 4 result: “4”

  8. Pattern Matching • Uses the matchvariablewithsyntax. • let rec factorial n = matchn with | 0 -> 1 | _ -> n * factorial(n-1) • Very useful with user defined Types and Unions in functions type PayType = | Salary of float | Wage of int let eval x = match x with | Salary y -> printf "Makes %.2f Yearly\r\n" y | Wage z -> printf "Makes %d Yearly\r\n" (z*40*52)

  9. Scope • An identifier is valid from after its definition until the end of the section it appears in • Top level identifiers therefore exist until the end of the program • Top level Identifiers cannot be redefined in their scope without a compile error • Identifier cannot be used in its own definition:let x = x + 1 • Nested inside a function, identifiers are valid from after their definition until the end of the containing function, and cannot be accessed outside of their containing function • When nested, identifier names can be reused. • Identifiers used this way will revert to their old values once out of scope

  10. Scope • let f = let n = 3 //initial value of nlet n = n * 2 //n is rebound to 3*2let n = n * 7 //n is rebound to 6*7let n = (n, "The Answer to it all")//n is rebound to a tuple n //n is returned to caller of f

  11. Binding • All bindings handled with the let keyword • Binds some name to some value regardless of value type • Uses pattern matching to assign to more then one identifier • let (a,b) = (1,2) • let mutable for assigning a value to an identifier instead of a binding. • let mutable bob = 7bob <- bob + 35 • <- signifies a value being pushed into the identifier

  12. Interactive Scripting • Copy code from the IDE into the Console by pressing Alt+Enter • Advantage of having the IDE InteliSense and Syntax Highlighting. • Test blocks of code • Entering a commands in the Interactive Console requires a double semicolon • Advantage when making functions, types, or long lines of code over just the console.

  13. Examples: Currying • Simple Curried Function the 2001 Space odyssey could have used to track it’s HAL 9000 Computer Automated System.

  14. Examples: IsPrime • Simple yet effective • Takes a grand total of about 20 seconds

  15. Examples: IsPrime(optimized) • The largest factor of a number is its square root. So we only need the numbers from 2 to sqrt n. • Takes only .20 seconds

  16. Examples: The OptimusPrime • Who needs evens? 2 is the only even prime, so why test all the rest. Yield to the rescue. • This one takes a mind numbing, earth shattering .11 seconds.

  17. Examples: SumOfSquares • Sum of Squares

  18. Benchmarks

More Related