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:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:

(*
Functional Programming at St Matthews.
A 1hr crash course in functional programming. Taught, as "Programming 101",
during Science Week at St Matthews Primary School, Cambridge, UK to Years 5
and 6 (1011 year olds).
History:
Samin Ishtiaq, 10/08/2013.
Camb Science Centre. Avoid duplicate names for tryfsharp.
Tomas Petricek, 30/November/2010.
Minor naming and indentation changes
(to comply with the usual formatting style)
Samin Ishtiaq, {9,10}/March/2010.
Dropped areas for doubling, is_even.
Thanks to Tim Morley for helping out.
Samin Ishtiaq, 9/March/2009.
First version.
*)
(*
Part 1: Calculating
Programming is calculating. We're going to begin by using F#, literally, as a
calculator:
1. Doubleclick the F# icon to start up the F# programming environment.
2. This gives a you a new window with a prompt, ">". You can type
arithmetic expressions at this prompt, like you would into any calculator.
3. Try typing in a few expressions to see what answers you get. You have to
end every expression with a ";;" and then press Enter:
*)
2+3 ;;
3+2 ;;
3*4 ;;
1*2*3*4*5 ;;
(4/2) ;;
(*
4. Try typing in other expressions to evaluate. Use brackets to get the right
order of calculation:
*)
(2 + 3) * 4 ;;
2 + (3 * 4) ;;
2 + 3 * 4 ;;
(*
Part 2: Variables
1. You can give a "name" to a particular expression. You know about this idea
already: in Excel spreadsheets, you might have cell A2 hold the expression
=(2+3); you can think of "A2" as the name that holds the value 5.
2. In F#, like in maths, you use "let" to give a name to an expression. Try
this:
*)
let z = 42;;
(*
This binds z to 42, and you can now use z in your calculations:
*)
z  40 ;;
z + z ;;
z ;;
(*
In fact, you don't have to use "z". You can use any word, like your own
name:
*)
let samin = 42;;
samin  40 ;;
samin  50 ;;
(*
But it's always a good idea to use a name that makes sense for the
context. For instance, if you're calculating with the length of a rectangle,
you should use "length" rather than "foo".
*)
(*
Part 3: Doubling/Halving
You know what doubling and halving numbers means. Say you had a number 4,
then it's double is 8 and it's half is 2.
*)
let x = 4 ;;
let doubleX = x * 2 ;;
let halfX = x / 2 ;;
(*
Danger: function ahead
2. The answer "8" is all very well for a number which is 4. But
the function you know of that doubles the number (x*x) actually
applies to any number. The function says "give me a number and
I'll give you back it's double", and this is how you write
exactly that in F#:
*)
let double n = n * n ;;
(*
On the lefthand side of the "=" sign is the "give me the"
parameter. On the righthand side, is the "I'll give you back"
result.
3. Try running this program now. Here, we use it to double various
numbers:
*)
double 3 ;;
double 5 ;;
(*
4. Now you have to do a bit thinking: Try writing down the function to
cube a number by filling out the "..." in the definition below:
*)
//let cube n = ... ;;
(* We can now use the [cube] function to generate the first 10
cubic numbers:
First, some wrappers to make printing easier.
*)
let printString s =
printf "%s" s ;;
let printStringNewline s =
printfn "%s" s ;;
let printNum n =
printf "%d" n ;;
(* Now the first 12 cubic numbers. *)
for y = 1 to 12 do
printNum y
printString " * "
printNum y
printString " * "
printNum y
printString " = "
// printNum (cube y)
printStringNewline "" ;;
(*
1. You know what odd and even numbers are. Here's a function to test whether a
number, x, is even:
*)
let isEven x =
if ((x % 2) = 0) then
printStringNewline "x is even"
else
printStringNewline "x is not even" ;;
(*
"%" is the "remainder" operation: x%y it returns the remainder after we divide
x by y.
The isEven function uses "ifthenelse", a programming language construct
that tests a condition; if the condition holds then it does the "then" part of
the program; if the condition doesn't hold, it does the "else" part of the
program.
Can you write isOdd, the function that takes a number and tests for whether
number is odd? Think how you'd modify isEven to write isOdd:
> let isOdd x = ...
*)
(*
2. Let's write a program to generate the times tables. First, a very simple
version, that only multiplies two numbers x and y:
> let multiply x y = ...
To generate the times table for 4, say, we actually need to multiply 4 by 1,
by 2, by 3,.... all the way to 12. We can do things stupidly like this:
> (multiply 4 1) ;;
> (multiply 4 2) ;;
> (multiply 4 3) ;;
...
> (multiply 4 12) ;;
Or we can be smart and realize that we're doing the same thing over and over
again. In such a case, we can use a "for" loop to repeatedly do (almost) the
same thing:
*)
let timesTable x =
for y = 1 to 12 do
printNum (x*y)
printStringNewline "" ;;
(*
Try it:
*)
timesTable 4 ;;
timesTable 7 ;;
timesTable 100 ;;
(*
And my favourite:
*)
timesTable 0 ;;
(*
Here's a much prettier version of timesTable:
*)
let timesTablePretty x =
for y = 1 to 12 do
printNum x
printString " x "
printNum y
printString " = "
printNum (y*x)
printStringNewline "" ;;
(*
5. Write a function to calculate the Fibonacci series.
The nth fibonacci number is the sum of the previous two fibonacci numbers:
fib 0 = 0
fib 1 = 1
fib n = fib n1 + fib n2
*)
let rec fib n =
if (n=0) then 0
else if (n=1) then 1
else (fib (n1)) + (fib (n2))
(*
Finding out more:
F# is a programming language in the MLtradition. Look at:
http://fsharp.net
http://caml.inria.fr
There are lots of programming languages out there. Take a look at python,
smalltalk, JavaScript, C, ARM Assembler.
*)
