KEMBAR78
Thinking in F# | PPTX
Thinking in F#
Mike Clement
@mdclement
mike@softwareontheside.com
http://blog.softwareontheside.com
XP Simple Design
•Passes all tests
•Clear, expressive, consistent
•No duplication
•Minimal
FizzBuzz
• If multiple of 3, get “Fizz”
• If multiple of 5, get “Buzz”
• If not, return input int as string
• Rules are in order
FizzBuzz Code
Currying/Partial function application
let Multiples divisor result x s = if x % divisor =
0 then s + result else s
let Fizzy = Multiples 3 "Fizz"
val Multiples : divisor:int -> result:string ->
x:int -> s:string -> string
val Fizzy : (int -> string -> string)
let Fizzy x s = if x % 3 = 0 then s + "Fizz" else s
Bowling Game Kata
The game consists of 10 frames as shown above. In each frame the player has two
opportunities to knock down 10 pins. The score for the frame is the total number of pins
knocked down, plus bonuses for strikes and spares.
A spare is when the player knocks down all 10 pins in two tries. The bonus for that frame is
the number of pins knocked down by the next roll. So in frame 3 above, the score is 10 (the
total number knocked down) plus a bonus of 5 (the number of pins knocked down on the
next roll.)
A strike is when the player knocks down all 10 pins on his first try. The bonus for that frame
is the value of the next two balls rolled.
In the tenth frame a player who rolls a spare or strike is allowed to roll the extra balls to
complete the frame. However no more than three balls can be rolled in tenth frame.
Bowling Game – The OO Way
Bowling Game
How do we get more functional?
Lists
[1, 2, 3, 4]
Head
Tail
Lists
[1, 2, 3, 4].Head = 1
[1, 2, 3, 4].Tail = [2, 3, 4]
[1, 2, 3, 4].Tail.Tail = [3, 4]
F# Resources
• http://en.wikibooks.org/wiki/F_Sharp_Programming
• F# for C# Developers by Tao Liu
• http://www.tryfsharp.org/
• https://github.com/ChrisMarinos/FSharpKoans
Mike Clement
• @mdclement
• mike@softwareontheside.com
• http://blog.softwareontheside.com
• https://github.com/mdclement
• http://agilecodegames.com
• Utah Software Craftsmanship
• http://utahsc.org
• @utahsc
• We meet the first Wednesday at SLCC

Thinking in F#

  • 1.
    Thinking in F# MikeClement @mdclement mike@softwareontheside.com http://blog.softwareontheside.com
  • 2.
    XP Simple Design •Passesall tests •Clear, expressive, consistent •No duplication •Minimal
  • 3.
    FizzBuzz • If multipleof 3, get “Fizz” • If multiple of 5, get “Buzz” • If not, return input int as string • Rules are in order
  • 4.
  • 5.
    Currying/Partial function application letMultiples divisor result x s = if x % divisor = 0 then s + result else s let Fizzy = Multiples 3 "Fizz" val Multiples : divisor:int -> result:string -> x:int -> s:string -> string val Fizzy : (int -> string -> string) let Fizzy x s = if x % 3 = 0 then s + "Fizz" else s
  • 6.
    Bowling Game Kata Thegame consists of 10 frames as shown above. In each frame the player has two opportunities to knock down 10 pins. The score for the frame is the total number of pins knocked down, plus bonuses for strikes and spares. A spare is when the player knocks down all 10 pins in two tries. The bonus for that frame is the number of pins knocked down by the next roll. So in frame 3 above, the score is 10 (the total number knocked down) plus a bonus of 5 (the number of pins knocked down on the next roll.) A strike is when the player knocks down all 10 pins on his first try. The bonus for that frame is the value of the next two balls rolled. In the tenth frame a player who rolls a spare or strike is allowed to roll the extra balls to complete the frame. However no more than three balls can be rolled in tenth frame.
  • 7.
    Bowling Game –The OO Way
  • 8.
    Bowling Game How dowe get more functional?
  • 9.
    Lists [1, 2, 3,4] Head Tail
  • 10.
    Lists [1, 2, 3,4].Head = 1 [1, 2, 3, 4].Tail = [2, 3, 4] [1, 2, 3, 4].Tail.Tail = [3, 4]
  • 11.
    F# Resources • http://en.wikibooks.org/wiki/F_Sharp_Programming •F# for C# Developers by Tao Liu • http://www.tryfsharp.org/ • https://github.com/ChrisMarinos/FSharpKoans
  • 12.
    Mike Clement • @mdclement •mike@softwareontheside.com • http://blog.softwareontheside.com • https://github.com/mdclement • http://agilecodegames.com • Utah Software Craftsmanship • http://utahsc.org • @utahsc • We meet the first Wednesday at SLCC

Editor's Notes

  • #3 Start here. Do the simplest thing that works.Can F# help me with this?
  • #4 Have the room count off
  • #6 Currying is built on the principle that each argument actually returns a separate function, which is why calling a function with only part of its parameters returns another function.