Presentation is loading. Please wait.

Presentation is loading. Please wait.

4. Dividing Computing with Pattern Matching in F#

Similar presentations


Presentation on theme: "4. Dividing Computing with Pattern Matching in F#"— Presentation transcript:

1 4. Dividing Computing with Pattern Matching in F#
Microsoft .NET 4. Dividing Computing with Pattern Matching in F# Pan Wuming 2017

2 Topics F# Types Pattern Matching References: F# Guide.

3 F# Types Primitive types Generic type: list, option…
constructed type (a generic type that has a specific type argument supplied) tuple Function type The .NET Framework types

4 Generic type Type syntax type-parameter generic-type-name
Examples type-parameter generic-type-name generic-type-name<type-parameter-list> 'a list list<'a> Dictionary<'key, 'value>

5 Most Frequently Used Structure: List
Listing out the elements, separated by semicolons and enclosed in square brackets List of integers: using the range operator (..) Empty list

6 Looping Construct of List
i is a variable

7 :: (cons) operator To attach elements to a list

8 List.map To apply a function to each element of a list and put all the results into a new list

9 tuple Type syntax type-name1 * type-name2 ... Examples:
(1,'b',3) has type type-name1 * type-name2 ... int * char * int

10 Higher order function as a parameter
Type syntax Examples List.map has type (function-type) ('a -> 'b) -> 'a list -> 'b list

11 Pattern Matching The match expression: Pattern Matching
Which provides branching control based on pattern match. Pattern Matching To compare data with a logical structure or structures To decompose data into constituent parts To Extract information from data in various ways.

12 Match Expressions

13

14 Patterns used in Pattern Matching
Array pattern Parenthesized pattern Tuple pattern Record pattern Wildcard pattern Pattern together with type annotation Type test pattern Null pattern Constant pattern Identifier pattern Variable pattern as pattern OR pattern AND pattern Cons pattern List pattern

15 Constant Patterns

16 option type and Identifier Patterns
The option type is a discriminated union that has two cases, Some and None.

17

18 Variable Patterns

19 Cons Pattern

20 List Pattern

21 Tuple Pattern

22 vs. Type Pattern


Download ppt "4. Dividing Computing with Pattern Matching in F#"

Similar presentations


Ads by Google