25
F# and its 11 features Guanru Li 2011 ACM class

F# and its 11 features Guanru Li 2011 ACM class. F# is …... a programming language

Embed Size (px)

Citation preview

Page 1: F# and its 11 features Guanru Li 2011 ACM class. F# is …... a programming language

F#and its 11 features

Guanru Li2011 ACM class

Page 2: F# and its 11 features Guanru Li 2011 ACM class. F# is …... a programming language

F# is …F# is …

... a programming language.

Page 3: F# and its 11 features Guanru Li 2011 ACM class. F# is …... a programming language

F# is …F# is …

... a functional programming language

Page 4: F# and its 11 features Guanru Li 2011 ACM class. F# is …... a programming language

F# is …F# is …

... a functional programming language for .NET

Page 5: F# and its 11 features Guanru Li 2011 ACM class. F# is …... a programming language

F# is …F# is …

... a functional and object oriented programming language for .NET

Page 6: F# and its 11 features Guanru Li 2011 ACM class. F# is …... a programming language

F# is …F# is …

... a functional, object oriented and imperative programming language

for .NET

Page 7: F# and its 11 features Guanru Li 2011 ACM class. F# is …... a programming language

F# is …F# is …

... a multi-paradigm programming language for .NET

Page 8: F# and its 11 features Guanru Li 2011 ACM class. F# is …... a programming language

F# is …F# is …

...a multi-paradigm programming language for .NET, ideally suited for

technical, symbolic and algorithmic applications

Page 9: F# and its 11 features Guanru Li 2011 ACM class. F# is …... a programming language

F#F#

Page 10: F# and its 11 features Guanru Li 2011 ACM class. F# is …... a programming language

Why named F#?Why named F#?

►C#C#

►Functional programmingFunctional programming

Page 11: F# and its 11 features Guanru Li 2011 ACM class. F# is …... a programming language

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)

Page 12: F# and its 11 features Guanru Li 2011 ACM class. F# is …... a programming language

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

Page 13: F# and its 11 features Guanru Li 2011 ACM class. F# is …... a programming language

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

Page 14: F# and its 11 features Guanru Li 2011 ACM class. F# is …... a programming language

Feature 3 of 11Feature 3 of 11

►Collection types for immutable dataCollection types for immutable data

►Usage: ??Usage: ??

Page 15: F# and its 11 features Guanru Li 2011 ACM class. F# is …... a programming language

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!”

Page 16: F# and its 11 features Guanru Li 2011 ACM class. F# is …... a programming language

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

Page 17: F# and its 11 features Guanru Li 2011 ACM class. F# is …... a programming language

Feature 6 of 11Feature 6 of 11

► Interactive programmingInteractive programming

Page 18: F# and its 11 features Guanru Li 2011 ACM class. F# is …... a programming language

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())

Page 19: F# and its 11 features Guanru Li 2011 ACM class. F# is …... a programming language

Feature 8 of 11Feature 8 of 11

►Object orientedObject oriented

►Class & inheritClass & inherit

►Single inheritanceSingle inheritance

Page 20: F# and its 11 features Guanru Li 2011 ACM class. F# is …... a programming language

Feature 9 of 11Feature 9 of 11

► Imperative programmingImperative programming

►Like C, C++, JavaLike C, C++, Java

Page 21: F# and its 11 features Guanru Li 2011 ACM class. F# is …... a programming language

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

Page 22: F# and its 11 features Guanru Li 2011 ACM class. F# is …... a programming language

Feature 11 of 11Feature 11 of 11►Asynchronous workflowsAsynchronous workflows

►????

Page 23: F# and its 11 features Guanru Li 2011 ACM class. F# is …... a programming language
Page 24: F# and its 11 features Guanru Li 2011 ACM class. F# is …... a programming language

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

Page 25: F# and its 11 features Guanru Li 2011 ACM class. F# is …... a programming language

Q&AQ&A