22
Teaching F# From numerical expressions to 3D graphics Tomáš Petříček F# enthusiast and F# book author PhD student at University of Cambridge

Teaching F#

Embed Size (px)

DESCRIPTION

One of the key concepts of functional programming is compositionality – the fact that we can build complex software using several basic “building blocks” and just a few ways of combining them. In this talk, I’ll demonstrate how we can teach this concept using F#. We’ll start with a basic example of this approach that even high-school students can easily understand – numerical expressions. Then we’ll move to more exciting examples of using library for composing 3D graphics. We’ll see that creating a fractal tree is not much more complicated than calculating the area of a triangle. The recording is available online at: http://www.communityforfsharp.net/

Citation preview

Page 1: Teaching F#

Teaching F#From numerical expressions to 3D

graphics

Tomáš Petříček

F# enthusiast and F# book authorPhD student at University of Cambridge

Page 2: Teaching F#

Target audience

Freshman students Basic knowledge of math principles

(Not too much – they may not like it) Earlier in high-school or later at

university should be fine too

Possibly first programming course Controversial topic, but F# has many

benefits

Page 3: Teaching F#

Related functional textbooks #1

Emphasize theory and abstract thinking

Page 4: Teaching F#

Related functional textbooks #2

Functional programming with fun demos

Page 5: Teaching F#

Why F# as a first language?

Mathematically oriented language Supplements math and theory courses Eliminates differences between students

Open-source with cross-platform support F# for MonoDevelop on Linux/Mac

Practical language with some market .NET/Mono skills are valued by industry Very easy transition to C# and other languages

Page 6: Teaching F#

Expressions in F#First functional programming steps

Page 7: Teaching F#

What F# shares with math?

Language is exact – we need to precisely say what we want to get

Composition does not break things – we can mix various correct facts

Things do not change – theorems will always be true, π will not change

Page 8: Teaching F#

Pythagorean theorem

Math has equations and expressions

Expressions can be evaluated

Using F# Interactive

ca

b

ac

b

𝑐2 = 𝑎2 + 𝑏2 ඥ𝑎2 + 𝑏2

ඥ32 + 42 = ξ9+ 16 = ξ25 = 5

> sqrt((pown 3.0 2) + (pown 4.0 2));;val it : float = 5.0

Page 9: Teaching F#

Introducing let declarations and if

Solving quadratic equations

Script for solving written in F#

−𝑏± ξ𝑏2 − 4𝑎𝑐2𝑎 −𝑏± ξ𝐷2𝑎 where 𝐷= 𝑏2 − 4𝑎𝑐

let d = pown b 2 - 4.0 * a * c;;

if d < 0.0 then "No real solution"elif d > 0.0 then "Two solutions"else "One solution";;

let x1 = (-b + sqrt d) / 2.0 * alet x2 = (-b - sqrt d) / 2.0 * a;;

Page 10: Teaching F#

DEMOWorking with F# expressions

Page 11: Teaching F#

Composing graphics in F#Fun examples for students to play with

Page 12: Teaching F#

Composing graphics

Expressions that have drawing as the result

Same concepts as mathematical expressions

Basic shapes

Functions for creating new shapes

> Fun.circle 100.0f;;val it : Drawing = (Drawing 100x100)

> Fun.fillColor Color.Goldenrod (Fun.circle 100.0f);;val it : Drawing = (Drawing 100x100)

Page 13: Teaching F#

Composing drawings using custom operator ($)

Let declarations to reuse drawingslet plus = Fun.lineStyle 2.0f Color.SteelBlue ( Fun.line -10.0f 0.0f 10.0f 0.0f $ Fun.line 0.0f -10.0f 0.0f 10.0f )

let star = plus $ (Fun.rotate 45.0f plus)let bigStar = Fun.scale 2.0f 2.0f star

Working with shapes

Fun.circle 200.0f $ Fun.move 0.0f 150.0f (Fun.circle 100.0f)

Page 14: Teaching F#

DEMOCreating drawings using F# expressions

Page 15: Teaching F#

Introducing recursion and 3D

Difficult concepts explained using graphics

Page 16: Teaching F#

Explaining recursion

Recursion is tricky concept

Two types of recursions (from HTDP) Structural – follows recursive data

structureFor example lists, trees, expressions etc.Such functions must terminate in F#

General – arbitrary recursive callsMay not terminate (if it is not well written)For example generating fractals

Page 17: Teaching F#

General recursion for lists

Not tail-recursive list length

Easy visualization of execution

Not tail-recursive – work done on the “way back”

let rec length list = match list with | [] -> 0 | head::tail -> (length tail) + 1

3 4 5

0+1 +1 +1

len[3;4;5] len[3;4] len[4] len[]

Page 18: Teaching F#

General recursion for lists

Tail-recursive list length

All work done on the way “forward” This is what tail-recursive means!

let rec length acc list = match list with | head::tail -> let newacc = acc + 1 in length newacc tail | [] -> acc;;

3 4 5

3

len 0 [3;4;5] len 1 [3;4] len 2 [4] len 3 []

Page 19: Teaching F#

DEMOGenerating drawings from lists

Page 20: Teaching F#

Adding new dimension to drawings

Expressions that define 3D objects

Just like math and 2D graphics> Fun.cube;;val it : Drawing3D = (...)

Fun.color Color.DarkRed Fun.cone $( Fun.color Color.Goldenrod (Fun.translate (0.0, 0.0, 1.0) Fun.cylinder) );;val it : Drawing3D = (...)

Page 21: Teaching F#

DEMO

Introducing general recursion using 3D objects

Page 22: Teaching F#

Links and Q & A

Functional variations Web SiteCross-platform F# supportTeaching materials for F#, etc… http://functional-variations.net

If you’re interested in more, get in touch!

http://tomasp.net | [email protected] http://twitter.com/tomaspetricek