-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAboutTuples.fs
More file actions
81 lines (57 loc) · 2.47 KB
/
AboutTuples.fs
File metadata and controls
81 lines (57 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
namespace FSharpKoans
open FSharpKoans.Core
//---------------------------------------------------------------
// About Tuples
//
// Tuples are used to easily group together values in F#. They're
// another fundamental construct of the language.
//---------------------------------------------------------------
[<Koan(Sort = 6)>]
module ``about tuples`` =
[<Koan>]
let CreatingTuples() =
let items = ("apple", "dog")
AssertEquality items ("apple", __)
[<Koan>]
let AccessingTupleElements() =
let items = ("apple", "dog")
let fruit = fst items
let animal = snd items
AssertEquality fruit __
AssertEquality animal __
[<Koan>]
let AccessingTupleElementsWithPatternMatching() =
(* fst and snd are useful in some situations, but they only work with
tuples containing two elements. It's usually better to use a
technique called pattern matching to access the values of a tuple.
Pattern matching works with tuples of any arity, and it allows you to
simultaneously break apart the tuple while assigning a name to each
value. Here's an example. *)
let items = ("apple", "dog", "Mustang")
let fruit, animal, car = items
AssertEquality fruit __
AssertEquality animal __
AssertEquality car __
[<Koan>]
let IgnoringValuesWithPatternMatching() =
let items = ("apple", "dog", "Mustang")
let _, animal, _ = items
AssertEquality animal __
(* NOTE: pattern matching is found in many places
throughout F#, and we'll revisit it again later *)
[<Koan>]
let ReturningMultipleValuesFromAFunction() =
let squareAndCube x =
(x ** 2.0, x ** 3.0)
let squared, cubed = squareAndCube 3.0
AssertEquality squared __
AssertEquality cubed __
(* THINK ABOUT IT: Is there really more than one return value?
What type does the squareAndCube function
return? *)
[<Koan>]
let TheTruthBehindMultipleReturnValues() =
let squareAndCube x =
(x ** 2.0, x ** 3.0)
let result = squareAndCube 3.0
AssertEquality result __