-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypedrivendevelopment.txt
125 lines (98 loc) · 3.69 KB
/
typedrivendevelopment.txt
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
For Ocaml, type names and field names are both lowercase.
Make types represent your domain
group together fields in records that are related/consistent/atomic
like
type postalAddress = {
address1: string;
address2: string;
city: string;
state: string;
zip: string;
}
to make instance of this type, you need to specify the field names also
e.g.
#utop> {address1 = "a1"; address2="a2"; city="New York"; state="New York"; zip="12345"};;
Adding meaning to primitive types:
Tiny types and interchangibility, consider following types
emailAddress = string;
state = string;
zip = string;
Now we wouldn't like to interchange these, would we?
Ideally we would have lot of tiny types to not mix them up.
Here's how (also known as single case union types)
type emailAddress = EmailAddress of string
type zipcode = Zipcode of string
type stateCode = StateCode of string
Note: data constructors are capital case for first letter
Note: Ocaml does not promote constructors as functions
It is a good idea to write toString/render functions using pattern matching
like toString functions for java POJOs which clearly specify
the meaning of type with its arguments.
Also unlike java POJOs fp types have explicit public immutable fields
Type-directed programming methodology:
1. write down name of the function and types of its arguments
and results
2. Write a comment that explains its purpose and any preconditions
3. Write several examples of what your function does.
4. Write body of function (hard part)
5. Turn your examples into tests
The place where types really help is hard part,
(Since it is substitution of expressions all the way),
It involves two conceptual activities :
* Deconstruct (tear apart and analyze)
* Construct (build) the output values
Types help because each comes with a predefined set of values.
So that you can pattern match for all cases.
Types help us search for and identify complete solutions
to the programming problem at hand.
If vs pattern matching to deconstruct data:
Pattern matching is a general paradigm
that programmers use to deconstruct data.
Pattern matching on booleans :
(* convert a boolean to an integer *)
let bool_to_int (b: bool) : int =
match b with
| true -> 1
| false -> 0
;;
Pattern matching on tuples :
(* create a string from the first two components, separated by a space *)
let full_name (name_and_age : string * string * int) : string =
match name_and_age with
| (first, last, _) -> first ^ " " ^ last
;;
The unit type :
only one value "unit",
Even thought it has no information content,
it is quite heavily used. Whenever an expression has
an effect on the outside world, but returns no interesting data,
e.g. printing to screen.
Assertions are also expressions with type unit
unit as input type:
Functions that already contain all the data they need
to execute
Pattern match on unit:
Poor style :
let hello_world (x:unit) : unit =
match x with
| () -> print_string "hello world\n"
;;
Since there is only one branch on the match expression,
you can promote it to argument position (like let based tuple pattern matching)
Better style:
let hello_world () : unit =
print_string "hello world\n"
;;
Seperating several unit value expressions in a row,
Instead of successive pattern matching, use semi colon to
seperate one unit value expression from next :
let hello_world () : unit =
print_string "hello";
print_string " ";
print_endline "world"
;;
Option type :
An option type written as t option, contains two sorts of values,
the value None and the value Some v where v is a value of type t.
The reason pattern matching is useful is structural induction on Constructors
and length of the terms