forked from willsam100/Learn-fp-with-fsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSartHere.fs
127 lines (90 loc) · 3.66 KB
/
SartHere.fs
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
namespace LearnFP.Core
open Utils
type ReadingFunctions() =
// THe following is a function that is attached to a class.
// The name of the class is above - ReadingFunctions
// This method below takes in an integer, named x, and returns the integer
// In FP languages, the return type type typically comes last
// the return keyword is also not needed
member this.Method(x:int):int =
x
// Complete the following
// NB: you do not need the return keyword. see the example above
// return x with 1 added to it, as you would in statndard maths.
member this.AddOne (x:int): int =
RemoveAndComplete("AddOne")
// Types and parenthes are options
// For the tests to compile and pass, this should take in an int, and return and int
member this.SubtractOne x =
RemoveAndComplete("SubtractOne")
// Static class
module Functions =
// A method not attached to a module is a function
// the identity function simply returns the input, unchanged
// complete the following, by returning the input
let inline identity x =
RemoveAndComplete("identity")
// Values can be named with let as well
let x: bool = true
// types do not need to be declared. This will still be type checked
let y = true
// Functions can be declared as data too.
let id: 'a -> 'a = identity
// Complete the following
let addTwo x =
RemoveAndComplete("addTwo")
// Complete the following using the id function
// Implement the identity function using the id function created above
let doubleIdentity x =
id (RemoveAndComplete("doubleIdentity"))
module PureFunctions =
open System
// A pure function is a function that will always return the same result, given the same inputs
let pureFunction x y =
x + y
// This is NOT a pure function, the result changes when given the same input
let impure x =
let r = Random()
r.Next()
// Complete the following
// you can use Math.Pow or infix **
let raiseToThePower (x:float) y =
RemoveAndComplete("add") y
let foo () =
DateTime.Now
// Complete the following
// is the funciton abve named `foo` a pure function?
let isFooAPureFunction (): bool =
RemoveAndComplete("isFooAPureFunction")
module HigherOrderFunctions =
// A higher order function, is a function that takes in another function
let f (g: int -> int) (x:int): int =
g x
// We'll use this next with our function above
// F# also defines this function for us, so this code can be commented out.
// In the future, this function will not be declared.
let id x = x
// to use the function f, we supply two values with spaces in between
// in OOP langugaes we would have written f(id,10)
let callingWithTwoArguments =
f id 10
// Complete the following
// the function should return 100
let addOneHundred () =
f id (RemoveAndComplete("addOneHundred"))
// functions do not always need a name.
// They are called annoymous functions or a lamda
// the keyword 'fun' is used to create an annoymous function
let lamdafunction: int -> int = (fun x -> x + 1)
// We can pass a lamda in to a higher order function
let addTen x =
f (fun x -> x + 10) x
// Complete the following
let addTwenty x =
f (RemoveAndComplete("addTwenty")) x
module ParitalApplication =
// a function that takes in two items
let add x y =
x + y
// If a argument is missing, then the result is a function
let addOne: int -> int = add 1