Teaching F#From numerical expressions to 3D graphicsTomáš PetříčekF# enthusiast and F# book authorPhD student at University of Cambridge
Target audienceFreshman studentsBasic knowledge of math principles(Not too much – they may not like it)Earlier in high-school or later at university should be fine tooPossibly first programming courseControversial topic, but F# has many benefits
Related functional textbooks #1Emphasize theory and abstract thinking
Related functional textbooks #2Functional programming with fun demos
Why F# as a first language?Mathematically oriented languageSupplements math and theory courses Eliminates differences between studentsOpen-source with cross-platform support F# for MonoDevelop on Linux/MacPractical language with some market.NET/Mono skills are valued by industryVery easy transition to C# and other languages
Expressions in F#First functional programming steps
What F# shares with math?Language is exact – we need to precisely say what we want to getComposition does not break things – we can mix various correct factsThings do not change – theorems will always be true, π will not change
Pythagorean theoremMath has equations and expressionsExpressions can be evaluatedUsing F# Interactive> sqrt((pown 3.0 2) + (pown 4.0 2));;val it : float = 5.0
Introducing let declarations and ifSolving quadratic equationsScript for solving written in F#let d = pown b 2 - 4.0 * a * c;;if d < 0.0 then "No real solution"elifd > 0.0 then "Two solutions"else "One solution";;let x1 = (-b + sqrt d) / 2.0 * alet x2 = (-b - sqrt d) / 2.0 * a;;
DEMOWorking with F# expressions
Composing graphics in F#Fun examples for students to play with
Composing graphicsExpressions that have drawing as the resultSame concepts as mathematical expressions Basic shapesFunctions for creating new shapes> Fun.circle100.0f;;val it : Drawing = (Drawing 100x100)> Fun.fillColorColor.Goldenrod    (Fun.circle 100.0f);;val it : Drawing = (Drawing 100x100)
Composing drawings using custom operator ($)Let declarations to reuse drawingsFun.circle200.0f $ Fun.move 0.0f 150.0f (Fun.circle 100.0f)let 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 starWorking with shapes
DEMOCreating drawings using F# expressions
Introducing recursion and 3DDifficult concepts explained using graphics
Explaining recursionRecursion is tricky conceptTwo 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
General recursion for listsNot tail-recursive list lengthEasy visualization of executionNot tail-recursive – work done on the “way back”let rec length list =     match list with     | [] -> 0    | head::tail -> (length tail) + 1
General recursion for listsTail-recursive list lengthAll 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;;
DEMOGenerating drawings from lists
Adding new dimension to drawingsExpressions that define 3D objectsJust like math and 2D graphicsFun.colorColor.DarkRedFun.cone $( Fun.colorColor.Goldenrod    (Fun.translate (0.0, 0.0, 1.0) Fun.cylinder) );;val it : Drawing3D = (...)> Fun.cube;;val it : Drawing3D = (...)
DEMOIntroducing general recursion using 3D objects
Links and Q & AFunctional variations Web SiteCross-platform F# supportTeaching materials for F#, etc…http://functional-variations.netIf you’re interested in more, get in touch!http://tomasp.net | tomas@tomasp.net http://twitter.com/tomaspetricek

Teaching F#

  • 1.
    Teaching F#From numericalexpressions to 3D graphicsTomáš PetříčekF# enthusiast and F# book authorPhD student at University of Cambridge
  • 2.
    Target audienceFreshman studentsBasicknowledge of math principles(Not too much – they may not like it)Earlier in high-school or later at university should be fine tooPossibly first programming courseControversial topic, but F# has many benefits
  • 3.
    Related functional textbooks#1Emphasize theory and abstract thinking
  • 4.
    Related functional textbooks#2Functional programming with fun demos
  • 5.
    Why F# asa first language?Mathematically oriented languageSupplements math and theory courses Eliminates differences between studentsOpen-source with cross-platform support F# for MonoDevelop on Linux/MacPractical language with some market.NET/Mono skills are valued by industryVery easy transition to C# and other languages
  • 6.
    Expressions in F#Firstfunctional programming steps
  • 7.
    What F# shareswith math?Language is exact – we need to precisely say what we want to getComposition does not break things – we can mix various correct factsThings do not change – theorems will always be true, π will not change
  • 8.
    Pythagorean theoremMath hasequations and expressionsExpressions can be evaluatedUsing F# Interactive> sqrt((pown 3.0 2) + (pown 4.0 2));;val it : float = 5.0
  • 9.
    Introducing let declarationsand ifSolving quadratic equationsScript for solving written in F#let d = pown b 2 - 4.0 * a * c;;if d < 0.0 then "No real solution"elifd > 0.0 then "Two solutions"else "One solution";;let x1 = (-b + sqrt d) / 2.0 * alet x2 = (-b - sqrt d) / 2.0 * a;;
  • 10.
  • 11.
    Composing graphics inF#Fun examples for students to play with
  • 12.
    Composing graphicsExpressions thathave drawing as the resultSame concepts as mathematical expressions Basic shapesFunctions for creating new shapes> Fun.circle100.0f;;val it : Drawing = (Drawing 100x100)> Fun.fillColorColor.Goldenrod (Fun.circle 100.0f);;val it : Drawing = (Drawing 100x100)
  • 13.
    Composing drawings usingcustom operator ($)Let declarations to reuse drawingsFun.circle200.0f $ Fun.move 0.0f 150.0f (Fun.circle 100.0f)let 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 starWorking with shapes
  • 14.
  • 15.
    Introducing recursion and3DDifficult concepts explained using graphics
  • 16.
    Explaining recursionRecursion istricky conceptTwo 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
  • 17.
    General recursion forlistsNot tail-recursive list lengthEasy visualization of executionNot tail-recursive – work done on the “way back”let rec length list = match list with | [] -> 0 | head::tail -> (length tail) + 1
  • 18.
    General recursion forlistsTail-recursive list lengthAll 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;;
  • 19.
  • 20.
    Adding new dimensionto drawingsExpressions that define 3D objectsJust like math and 2D graphicsFun.colorColor.DarkRedFun.cone $( Fun.colorColor.Goldenrod (Fun.translate (0.0, 0.0, 1.0) Fun.cylinder) );;val it : Drawing3D = (...)> Fun.cube;;val it : Drawing3D = (...)
  • 21.
  • 22.
    Links and Q& AFunctional variations Web SiteCross-platform F# supportTeaching materials for F#, etc…http://functional-variations.netIf you’re interested in more, get in touch!http://tomasp.net | tomas@tomasp.net http://twitter.com/tomaspetricek