Вы находитесь на странице: 1из 29

Friendly F#

Functional languages applied to videogames

Introduction
Real (and fun) applications
Physics simulations Videogames ... No factorials!

Problem solution approach

Agenda
Chapter Application 1 Bouncing ball Constructs Control flow, tuples, functions

2
3 4

Rocket Saturn V
Asteroid field Bigger asteroid field

Basic data structures (record), units of measure Lists and sequences


Trees and discriminated unions

Chapter 1
Basic Data Manipulation

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

General discussion (*)


Constructs
Let Functions
Higher order Recursive Partially specialized Generic types

Conditional expressions Imperative capabilities

Let binding and functions (*)


let pi = 3.141592654 let a,b,c = 1,20.0,"hello" let a' = 2+a*3

Let binding and functions (*)


let x = 2 Console.Write(x.ToString()) let y = x*3+1

let x = 4 Console.Write(x.ToString()) let y = x*3+1

let f x = Console.Write(x.ToString()) x*3+1 let y = f 2 let y = f 4

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

Let binding and functions (*)


let f'' g = g 10
(int -> int) -> int

let res1 = f'' (fun x -> x*2+1)

let res2 = f'' incr

Let binding and functions (*)

let rec factorial n = if n = 0 then 1 else n*factorial (n-1)

Let binding and functions (*)

let f() = let x = 10 let x = "hello" x

Let binding and functions (*)


let f'() let x = let g() let x = do g() x = 10 = printf "%d\n" x "hello"

Branching and Matching (*)


Code branches If-then-else Pattern matching

Branching and Matching (*)


if cond then BRANCH1 else BRANCH2

Branching and Matching (*)


Boolean values
True False

Bool type Operators


Or, And, Not

Branching and Matching (*)


Examples
let let let (if i j k i = = = < if if if 20 p then 10 else 20 q || r then "hello" else "ciao" i < 10 then "thank you" else then "grazie" else "merci")

Branching and Matching (*)


Nested ifs Indentation is essential
let k = if i < 10 then "thank you" else if i < 20 then "grazie" else "merci"

Branching and Matching (*)


Elif
let k = if i < 10 then "thank you" elif i < 20 then "grazie" else "merci"

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

Branching and Matching (*)


Pattern Matching
let rec fibonacci n = if n = 0 then 0 elif n = 1 then 1 else fibonacci (n-1)+fibonacci (n-2) let rec fibonacci' n = match n with | 0 -> 0 | 1 -> 1 | v -> fibonacci' (v-1)+fibonacci' (v-2)

Branching and Matching (*)


Function
let f x = match x with ... let f = function ...

Variables and statements (*)


Imperative constructs
Variables References For and while loops ...

Variables and statements (*)


Variables
Local mutable cells Heap-allocated references
let mutable v1 = 0 let v2 = ref 0 do v1 <- v1+10 do v2 := !v2+10

Variables and statements (*)


References: generic type
val v1 : int val v2 : int ref

Assignments: type unit

Variables and statements (*)


For loop
for i = x to y do BODY

NB: extremes included!

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

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)

Generic functions and partial specialization (**)


val quadratic : ('a*'a -> 'a)*('a*'a -> 'a)*'a*'a*'a*'a -> 'a

a = any type
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)

Вам также может понравиться