en - chapter 1 - general discussion
TRANSCRIPT
-
8/21/2019 En - Chapter 1 - General Discussion
1/29
Friendly F#
Functional languages applied to
videogames
-
8/21/2019 En - Chapter 1 - General Discussion
2/29
Introduction
Real (and fun) applications
Physics simulations
Videogames
... No factorials!
Problemsolution approach
-
8/21/2019 En - Chapter 1 - General Discussion
3/29
Agenda
Chapter Application Constructs
1 Bouncing ball Control flow, tuples, functions
2 Rocket Saturn V Basic data structures (record),units of measure
3 Asteroid field Lists and sequences
4 Bigger asteroid field Trees and discriminated unions
-
8/21/2019 En - Chapter 1 - General Discussion
4/29
Chapter 1
Basic Data Manipulation
-
8/21/2019 En - Chapter 1 - General Discussion
5/29
Preliminar notes
Indentation is part of the code
Spaces matter!
Modules and namespaces
Block of names with the same prefix
Modules also contain values, namespaces only
type declarations
Can be nested
-
8/21/2019 En - Chapter 1 - General Discussion
6/29
General discussion (*)
Constructs
Let
Functions
Higher order
Recursive
Partially specialized
Generic types Conditional expressions
Imperative capabilities
-
8/21/2019 En - Chapter 1 - General Discussion
7/29
Let binding and functions (*)
let pi = 3.141592654
let a,b,c = 1,20.0,"hello"
let a' = 2+a*3
-
8/21/2019 En - Chapter 1 - General Discussion
8/29
Let binding and functions (*)
let f x =
Console.Write(x.ToString())
x*3+1
let y = f 2
let y = f 4
let x = 2
Console.Write(x.ToString())
let y = x*3+1
let x = 4
Console.Write(x.ToString())
let y = x*3+1
-
8/21/2019 En - Chapter 1 - General Discussion
9/29
Let binding and functions (*)
let FUNCTION-NAME PARAMS = BODY
let incr x = x+1
fun PARAMS -> BODY
let res = (fun x -> x*2+1) 10
-
8/21/2019 En - Chapter 1 - General Discussion
10/29
Let binding and functions (*)
let f'' g = g 10 (int -> int) -> int
let res1 = f'' (fun x -> x*2+1)
let res2 = f'' incr
-
8/21/2019 En - Chapter 1 - General Discussion
11/29
Let binding and functions (*)
let rec factorial n =
if n = 0 then 1
else n*factorial (n-1)
-
8/21/2019 En - Chapter 1 - General Discussion
12/29
Let binding and functions (*)
let f() =
let x = 10let x = "hello"
x
-
8/21/2019 En - Chapter 1 - General Discussion
13/29
Let binding and functions (*)
let f'() =
let x = 10
let g() = printf "%d\n" xlet x = "hello"
do g()
x
-
8/21/2019 En - Chapter 1 - General Discussion
14/29
Branching and Matching (*)
Code branches
If-then-else Pattern matching
-
8/21/2019 En - Chapter 1 - General Discussion
15/29
Branching and Matching (*)
if cond then
BRANCH1
else
BRANCH2
-
8/21/2019 En - Chapter 1 - General Discussion
16/29
Branching and Matching (*)
Boolean values
True
False
Bool type
Operators
Or, And, Not
-
8/21/2019 En - Chapter 1 - General Discussion
17/29
Branching and Matching (*)
Examples
let i = if p then 10 else 20let j = if q || r then "hello" else "ciao"
let k = if i < 10 then "thank you" else
(if i < 20 then "grazie" else "merci")
-
8/21/2019 En - Chapter 1 - General Discussion
18/29
Branching and Matching (*)
Nested ifs
Indentation is essential
let k = if i < 10 then"thank you"
else
if i < 20 then
"grazie"
else"merci"
-
8/21/2019 En - Chapter 1 - General Discussion
19/29
Branching and Matching (*)
Elif
let k = if i < 10 then"thank you"
elif i < 20 then
"grazie"
else
"merci"
-
8/21/2019 En - Chapter 1 - General Discussion
20/29
Branching and Matching (*)
Types
If has the same type of its branches
Both branches must have the same type
Not allowed
if i > 10 then "hello" else 20
-
8/21/2019 En - Chapter 1 - General Discussion
21/29
Branching and Matching (*)
Pattern Matching
let rec fibonacci n =
if n = 0 then 0
elif n = 1 then 1else fibonacci (n-1)+fibonacci (n-2)
let rec fibonacci' n =
match n with| 0 -> 0
| 1 -> 1
| v -> fibonacci' (v-1)+fibonacci' (v-2)
-
8/21/2019 En - Chapter 1 - General Discussion
22/29
Branching and Matching (*)
Function
let f x =
match x with...
let f =
function...
-
8/21/2019 En - Chapter 1 - General Discussion
23/29
Variables and statements (*)
Imperative constructs
Variables
References
For and while loops
...
-
8/21/2019 En - Chapter 1 - General Discussion
24/29
Variables and statements (*)
Variables
Local mutable cells
Heap-allocated references
let mutable v1 = 0
let v2 = ref 0
do v1
-
8/21/2019 En - Chapter 1 - General Discussion
25/29
Variables and statements (*)
References: generic type
Assignments: type unit
val v1 : int
val v2 : int ref
-
8/21/2019 En - Chapter 1 - General Discussion
26/29
Variables and statements (*)
For loop
NB: extremes included!
for i = x to y do BODY
-
8/21/2019 En - Chapter 1 - General Discussion
27/29
Generic functions and partial
specialization (**)
Partially specialized functions
Some parameters are stored in advance
Result: function with less parameters
Generic functions
Parameters of (almost) any type
-
8/21/2019 En - Chapter 1 - General Discussion
28/29
Generic functions and partial
specialization (**)
Ref is a generic type
let quadratic_int (a,b,c,x) = a*x*x+b*x+c
let quadratic_float (a:float,b,c,x) =
a*x*x+b*x+c
let quadratic (sum,prod,a,b,c,x) =let x_square = prod(x,x)
let t1 = prod(x_square,a)
let t2 = prod(x,b)
sum(sum(t1,t2),c)
-
8/21/2019 En - Chapter 1 - General Discussion
29/29
Generic functions and partial
specialization (**)
a = any type
val quadratic : ('a*'a ->
'a)*('a*'a -> 'a)*'a*'a*'a*'a -> 'a
let y1 = quadratic
((fun (x,y) -> x+y),(fun (x,y) -> x*y),10,4,-2,3)
let y2 = quadratic
((fun (x,y) -> x+y),(fun (x,y) -> x*y),
10 0 4 0 -2 0 3 0)