-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02-Functions.fsx
89 lines (64 loc) · 2.08 KB
/
02-Functions.fsx
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
(*
Functions!
Functions are a basic building block in F#.
You can define functions with 'let' too.
*)
let increment i =
i + 1
// Calling a function is easy.
let two = increment 1
printfn "%d" two
let three = increment two
printfn "%d" three
let div a b =
a / b
// printfn is a built-in function that takes the format and arguments.
(*
Function Types:
Notice the type signature of the function is 'int -> int'
This indicates that it's a function that takes an int (lhs)
and returns an int (rhs).
We did not explicitly mention this, but the compiler still figured it out.
Can anyone guess how?
*)
// Creating functions that take multiple arguments is easy too.
let addMod100 (a : int) (b : int) : int =
let sum = a + b
sum % 100
// We explicitly added type signatures here.
// When you have a multi-line function, you always return the last line.
// There is no need to add a 'return' keyword.
// Functions can also be defined as a 'lambda expression'
// This form is useful when you want to define a function on the rhs
// Example: when passing a function to another function
let increment' = fun (i : int) -> i + 1
// The 'plus' keyword here is actually a function.
// It's a special type of function called an 'infix' function,
// which is basically syntactic sugar.
// Instead of calling an infix function like this: (+ 1 2)
// You can call it like this: (1 + 2)
// You can actually call '+' like any other function like this
let total = (+) 1 2
// Since a function is like any other value,
// you can pass a function to another function, of course.
// (Contrived example)
let twice (fn : int -> int) (value : int) =
let first = fn value
fn first
let three' = twice increment 1
(*
Recursion
You need to use the `rec` keyword if your function is recursive.
*)
let rec factorial a =
if a = 0 then
1
else
a * (factorial (a - 1))
(*
Exercises:
1. Write a hello world function:
Given a name, return "Hello <name>"
2. Write a recursive function to implement
multiplication with addition.
*)