f# and its 11 features
DESCRIPTION
F# and its 11 features. Guanru Li 2011 ACM class. F# is …. ... a programming language. F# is …. ... a functional programming language. F# is …. ... a functional programming language for .NET. F# is …. ... a functional and object oriented programming language for .NET. - PowerPoint PPT PresentationTRANSCRIPT
F# is …F# is …
...a multi-paradigm programming language for .NET, ideally suited for
technical, symbolic and algorithmic applications
Hello F#, Hello n Hello F#, Hello n factorialfactorial
printfn "Hello F#!“
let rec factorial n =
match n with
| 0 -> 1
| _ -> n * factorial(n - 1)
Feature 1 of 11Feature 1 of 11
►Functional programmingFunctional programminglet f (g: int -> int) y = g y
let increment x = x + 1
let a = f increment 100
►Lambda expressionLambda expressionlet f (g: int -> int) y = g y
let a = f (fun x -> x + 1) 100
Feature 2 of 11Feature 2 of 11
►Compose the functions!Compose the functions!
let function1 x = x + 1
let function2 x = x * 2
let h = function1 >> function2
let a = h 100
Feature 3 of 11Feature 3 of 11
►Collection types for immutable dataCollection types for immutable data
►Usage: ??Usage: ??
Feature 4 of 11Feature 4 of 11
►Pattern matchingPattern matching(x is a list)
let length x =
match x with
| [] -> “0”
| [ _ ] -> “1”
| [ _; _ ] -> “2”
| _ -> “Too Long!”
Feature 5 of 11Feature 5 of 11
►Discriminated unionsDiscriminated unionstype Shape =
| Circle of float
| Square of double
let area myShape =
match myShape with
| Circle r -> 3.14*r*r
| Square r -> s*s
Feature 7 of 11Feature 7 of 11
►Lazy computations & lazy evaluationLazy computations & lazy evaluation
let x = 10
let result = lazy (x + 10)
do something
printfn "%d" (result.Force())
Feature 8 of 11Feature 8 of 11
►Object orientedObject oriented
►Class & inheritClass & inherit
►Single inheritanceSingle inheritance
Feature 9 of 11Feature 9 of 11
► Imperative programmingImperative programming
►Like C, C++, JavaLike C, C++, Java
Feature 10 of 11Feature 10 of 11
►Generics and type inferenceGenerics and type inference
let makeList a b = [a; b]
makeList<int> 1 2
let function1 a = a + 1
ReferenceReference
►http://en.wikipedia.org/wiki/F_Sharp_(prhttp://en.wikipedia.org/wiki/F_Sharp_(programming_language)ogramming_language) ---- wikipedia F sharp ---- wikipedia F sharp
►http://msdn.microsoft.com/zh-cn/libraryhttp://msdn.microsoft.com/zh-cn/library/ dd233181/ dd233181 ---- MSDN F# reference ---- MSDN F# reference
►http://developer.51cto.com/art/201004/1http://developer.51cto.com/art/201004/192870.htm92870.htm ---- 51CTO.com F# new features ---- 51CTO.com F# new features