-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAboutLet.fs
More file actions
85 lines (66 loc) · 2.61 KB
/
AboutLet.fs
File metadata and controls
85 lines (66 loc) · 2.61 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
82
83
84
85
namespace FSharpKoans
open FSharpKoans.Core
//---------------------------------------------------------------
// About Let
//
// The let keyword is one of the most fundamental parts of F#.
// You'll use it in almost every line of F# code you write, so
// let's get to know it well! (no pun intended)
//---------------------------------------------------------------
[<Koan(Sort = 2)>]
module ``about let`` =
[<Koan>]
let LetBindsANameToAValue() =
let x = 50
AssertEquality x __
(* In F#, values created with let are inferred to have a type like
"int" for integer values, "string" for text values, and "bool"
for true or false values. *)
[<Koan>]
let LetInfersTheTypesOfValuesWherePossible() =
let x = 50
let typeOfX = x.GetType()
AssertEquality typeOfX typeof<int>
let y = "a string"
let expectedType = y.GetType()
AssertEquality expectedType typeof<FILL_ME_IN>
[<Koan>]
let YouCanMakeTypesExplicit() =
let (x:int) = 42
let typeOfX = x.GetType()
let y:string = "forty two"
let typeOfY = y.GetType()
AssertEquality typeOfX typeof<FILL_ME_IN>
AssertEquality typeOfY typeof<FILL_ME_IN>
(* You don't usually need to provide explicit type annotations types for
local variables, but type annotations can come in handy in other
contexts as you'll see later. *)
[<Koan>]
let FloatsAndInts() =
(* Depending on your background, you may be surprised to learn that
in F#, integers and floating point numbers are different types.
In other words, the following is true. *)
let x = 20
let typeOfX = x.GetType()
let y = 20.0
let typeOfY = y.GetType()
//you don't need to modify these
AssertEquality typeOfX typeof<int>
AssertEquality typeOfY typeof<float>
//If you're coming from another .NET language, float is F# slang for
//the double type.
[<Koan>]
let ModifyingTheValueOfVariables() =
let mutable x = 100
x <- 200
AssertEquality x __
[<Koan>]
let YouCannotModifyALetBoundValueIfItIsNotMutable() =
let x = 50
//What happens if you uncomment the following?
//
//x <- 100
//NOTE: Although you can't modify immutable values, it is possible
// to reuse the name of a value in some cases using "shadowing".
let x = 100
AssertEquality x __