This project is read-only.
Using Tuples

Category:
Lists, Tuples and Options
Description: This sample shows simple uses of tuples
Code:
let TupleSample1() =
let data = 1,2,3
printfn "data = \n%A" data
let f (a,b,c) = (a+b,b+c,c+a)
let res = f(f(f(data)))
printfn "res = \n%A" res
let r1,r2,r3 = res
printfn "r1 = %d, r2 = %d, r3 = %d" r1 r2 r3
let r4,r5,r6 = f(res)
printfn "r4 = %d, r5 = %d, r6 = %d" r4 r5 r6

Execution Result:
data = 
(1, 2, 3)
res =
(17, 16, 15)
r1 = 17, r2 = 16, r3 = 15
r4 = 33, r5 = 31, r6 = 32

Tuple to list

Category:
Lists, Tuples and Options
Description: This sample converts a tuple to a list
Code:
let TupleToList() = 
let tupleToList t =
if Microsoft.FSharp.Reflection.FSharpType.IsTuple(t.GetType())
then Some (Microsoft.FSharp.Reflection.FSharpValue.GetTupleFields t |> Array.toList)
else None
let r = tupleToList (1,2,3,4)
match r with
| Some(x) -> x |> Seq.iter (fun n -> printfn "%A" n)
| None -> printfn "no element"

Execution Result:
1
2
3
4

List to Tuple

Category:
Lists, Tuples and Options
Description: This sample convert a list to a tuple
Code:
let ListToTuple() = 
let listToTuple l =
let l' = List.toArray l
let types = l' |> Array.map (fun o -> o.GetType())
let tupleType = Microsoft.FSharp.Reflection.FSharpType.MakeTupleType types
Microsoft.FSharp.Reflection.FSharpValue.MakeTuple (l' , tupleType)
let r = (listToTuple [1,2,3,4]) |> Microsoft.FSharp.Reflection.FSharpValue.GetTupleFields
r.[0] |> printfn "%A"


Execution Result:
(1, 2, 3, 4)

Basic List Construction

Category:
Lists, Tuples and Options
Description: This sample demonstrates basic list construction
Code:
let ListConstruction() =

// Create an empty list
let emptyL = []

// Create a simple floating-point number list
let smallFloatsL = [1.0; 2.0; 3.0; 4.0]

// Create a list using a range expression - same rules as with arrays, step is optional
let listRngExp = [0..2..10]

// Create a list using a function based on index
let listByInit = List.init 5 (fun i -> i*i*i)

// Create a list using list comprehension
let listLC = [ for i in 1..5 do yield (i * 2) ]

// Another example of list comprehension
let listLC2 = [ for p in [(0,0); (1,1); (2,4); (3,9); (4,16); (5,25); (6, 36)] do
match p with
| (a,_) when a%2=0 -> yield p
| (_,_) -> () ]

printfn "emptyL = %A" emptyL
printfn "\nsmallFloatsL = %A" smallFloatsL
printfn "\nlistRngExp = %A" listRngExp
printfn "\nlistByInit = %A" listByInit
printfn "\nlistLC = %A" listLC
printfn "\nlistLC2 = %A" listLC2

Execution Result:
emptyL = []

smallFloatsL = [1.0; 2.0; 3.0; 4.0]

listRngExp = [0; 2; 4; 6; 8; 10]

listByInit = [0; 1; 8; 27; 64]

listLC = [2; 4; 6; 8; 10]

listLC2 = [(0, 0); (2, 4); (4, 16); (6, 36)]

Basic List Construction 2

Category:
Lists, Tuples and Options
Description: This sample demonstrates a simple generative list comprehension to specify lists
Code:
let SequenceExpressionSample1() =

let data1 = [ for x in 0..20 -> x, x * x ]

// This uses a nested loop
let data2 = [ for x in 0..5 do
for y in 0..5 do
yield x, y, x * y ]

// This uses a filter
let data3 = [ for x in 0..5 do
for y in 0..5 do
if x > y then
yield (x, y, x * y) ]

// This uses an internal let-binding
let data4 = [ for x in 0..5 do
for y in 0..5 do
if x > y then
let z = x * y
yield (x, y, z) ]

printfn "data1 = \n%A\n\n" data1
printfn "data2 = \n%A\n\n" data2
printfn "data3 = \n%A\n\n" data3
printfn "data4 = \n%A\n\n" data4

Execution Result:
data1 = 
[(0, 0); (1, 1); (2, 4); (3, 9); (4, 16); (5, 25); (6, 36); (7, 49); (8, 64);
(9, 81); (10, 100); (11, 121); (12, 144); (13, 169); (14, 196); (15, 225);
(16, 256); (17, 289); (18, 324); (19, 361); (20, 400)]


data2 =
[(0, 0, 0); (0, 1, 0); (0, 2, 0); (0, 3, 0); (0, 4, 0); (0, 5, 0); (1, 0, 0);
(1, 1, 1); (1, 2, 2); (1, 3, 3); (1, 4, 4); (1, 5, 5); (2, 0, 0); (2, 1, 2);
(2, 2, 4); (2, 3, 6); (2, 4, 8); (2, 5, 10); (3, 0, 0); (3, 1, 3); (3, 2, 6);
(3, 3, 9); (3, 4, 12); (3, 5, 15); (4, 0, 0); (4, 1, 4); (4, 2, 8); (4, 3, 12);
(4, 4, 16); (4, 5, 20); (5, 0, 0); (5, 1, 5); (5, 2, 10); (5, 3, 15);
(5, 4, 20); (5, 5, 25)]


data3 =
[(1, 0, 0); (2, 0, 0); (2, 1, 2); (3, 0, 0); (3, 1, 3); (3, 2, 6); (4, 0, 0);
(4, 1, 4); (4, 2, 8); (4, 3, 12); (5, 0, 0); (5, 1, 5); (5, 2, 10); (5, 3, 15);
(5, 4, 20)]


data4 =
[(1, 0, 0); (2, 0, 0); (2, 1, 2); (3, 0, 0); (3, 1, 3); (3, 2, 6); (4, 0, 0);
(4, 1, 4); (4, 2, 8); (4, 3, 12); (5, 0, 0); (5, 1, 5); (5, 2, 10); (5, 3, 15);
(5, 4, 20)]



Lists and Pattern Matching

Category:
Lists, Tuples and Options
Description: This sample shows simple uses of lists and functions that pattern match on them.
Code:
let ListSample1() =
let data = [1;2;3;4]
printfn "data = \n%A" data
printfn "List.head data = %d" (List.head data)
printfn "List.tail data = \n%A" (List.tail data)
printfn "List.length data = %d" (List.length data)
let consume data =
match data with
| 1::rest -> printfn "matched a 1"; rest
| 2::3::rest -> printfn "matched a 2 and 3"; rest
| [4] -> printfn "matched a 4"; []
| _ -> printfn "unexpected!"; []
let data = consume data
let data = consume data
let data = consume data
printfn "At end of list? %b" (data = [])

Execution Result:
data = 
[1; 2; 3; 4]
List.head data = 1
List.tail data =
[2; 3; 4]
List.length data = 4
matched a 1
matched a 2 and 3
matched a 4
At end of list? true

Lists and 'map'

Category:
Lists, Tuples and Options
Description: String23'
Code:
let ListSample2() =
let data = [1;2;3;4]
let r1 = data |> List.map (fun x -> x + 1)
printfn "Adding '1' using map = %A" r1
let r2 = data |> List.map string
printfn "Converting to strings using map = %A" r2
let r3 = data |> List.map (fun x -> (x,x))
printfn "Tupling up using map = %A" r3

Execution Result:
Adding '1' using map = [2; 3; 4; 5]
Converting to strings using map = ["1"; "2"; "3"; "4"]
Tupling up using map = [(1, 1); (2, 2); (3, 3); (4, 4)]

Lists and Iteration

Category:
Lists, Tuples and Options
Description: This sample shows simple uses of 'iter'
Code:
let ListSample3() =
let data = ["Cats";"Dogs";"Mice";"Elephants"]
data |> List.iter (fun x -> printfn "item: %s" x)

Execution Result:
item: Cats
item: Dogs
item: Mice
item: Elephants

Lists and Indexed Iteration

Category:
Lists, Tuples and Options
Description: This sample shows simple uses of 'iteri'
Code:
let ListSample4() =
let data = ["Cats";"Dogs";"Mice";"Elephants"]
data |> List.iteri (fun i x -> printfn "item %d: %s" i x)


Execution Result:
item 0: Cats
item 1: Dogs
item 2: Mice
item 3: Elephants

Lists and Folding

Category:
Lists, Tuples and Options
Description: This sample shows simple uses of 'fold' to accumulate a result over a list
Code:
let ListSample5() =
let data = [("Cats",4);
("Dogs",5);
("Mice",3);
("Elephants",2)]
let count = List.fold (fun acc (nm,x) -> acc+x) 0 data
printfn "Total number of animals: %d" count

Execution Result:
Total number of animals: 14

Lists and Filter

Category:
Lists, Tuples and Options
Description: This sample shows how to filter a list
Code:
let ListSample6() =
let data = [("Cats",4);
("Dogs",5);
("Mice",3);
("Elephants",2)]
let res = data |> List.filter (fun (nm,x) -> nm.Length <= 4)
printfn "Animals with short names: %A" res


Execution Result:
Animals with short names: [("Cats", 4); ("Dogs", 5); ("Mice", 3)]

Lists and Choose

Category:
Lists, Tuples and Options
Description: Project from a list
Code:
let ListSample7() =
let data = [("Cats",4);
("Dogs",5);
("Mice",3);
("Elephants",2)]
let res = data |> List.choose (fun (nm,x) -> if nm.Length <= 4 then Some(x) else None)
printfn "Counts of animals with short names: %A" res


Execution Result:
Counts of animals with short names: [4; 5; 3]

Simple options

Category:
Lists, Tuples and Options
Description: Create some optional values, print them and match on them.
Code:
let OptionsSample2() =

let data = Some(1,3)
printfn "data = %A" data;
printfn "data.IsSome = %b" data.IsSome
printfn "data.IsNone = %b" data.IsNone
printfn "data.Value = %A" data.Value

let data2 = None
printfn "data2.IsSome = %b" data2.IsSome
printfn "data2.IsNone = %b" data2.IsNone

Execution Result:
data = Some (1, 3)
data.IsSome = true
data.IsNone = false
data.Value = (1, 3)
data2.IsSome = false
data2.IsNone = true

Using options for data

Category:
Lists, Tuples and Options
Description: Here we return an option from a function indicating the opening hours of a shop, if any.
Code:
let OptionsSample3() =

let openingHours day =
match day with
| DayOfWeek.Monday
| DayOfWeek.Tuesday
| DayOfWeek.Thursday
| DayOfWeek.Friday -> Some(9,17)
| DayOfWeek.Wednesday -> Some(9,19) // extended hours on Wednesday
| _ -> None

let today = DateTime.Now.DayOfWeek

match openingHours today with
| None -> printfn "The shop's not open today"
| Some(s,f) -> printfn "The shop's open today from %02d:00-%d:00" s f

//--------------------------------------------------------

Execution Result:
The shop's open today from 09:00-17:00

Last edited Sep 13, 2011 at 12:38 AM by ttliu2000, version 1

Comments

No comments yet.