FSharp eye for the Haskell guy - London 2015
-
Upload
phillip-trelford -
Category
Software
-
view
1.102 -
download
0
Transcript of FSharp eye for the Haskell guy - London 2015
F#
• Statically Typed
• Functional First
• Object Oriented
• Open Source
• Cross Platform
• In Visual Studio
& Xamarin Studio
& Emacs & Vim & Atom & ..
F# Influences
// OCaml + Python like light syntax
let dist (x1,y1) (x2,y2) =
let x = x1 - x2 in
let y = y2 - y1 in
sqrt (x * x + y * y)
// & C# dot-driven development
let trim (s:string) =
s.Trim() F#
OCaml
PythonC#
Miranda Haskell
Haskell
Miranda
Type Classes
Monads
Change of syntax for type declarations — Miranda
bool ::= True | False
string == [char]
becomes in Haskell
data Bool = False | True
type String = [Char]
Some History of Functional Programming Languages, Turner
F#unctional Londoners
• meetup.com/fsharplondon
• 1000+ Members
• Meets every 2 weeks
• Topics• Machine Learning • Finance• Games• Art• Cloud• Web
F# Software Foundation
http://www.fsharp.org
software stacks
trainings teaching F# user groups snippets
mac and linux cross-platform books and tutorials
F# community open-source MonoDevelop
contributions research support consultancy mailing list
F# Testimonials
order of magnitude increase in productivity
GameSys
performance is 10× better than the C++ that it replaces
Aviva
I am still waiting for the first bug to come in
E-On
Source: fsharp.org/testimonials
Functions
let rec fact x =
if x < 1 then 1
else x * fact (x - 1)
let rec fib = function
| 0 -> 0 | 1 -> 1
| n -> fib(n - 1) + fib(n - 2)
[1..3] |> List.map ((+)1)
let inline map xs = List.map xs
map ((+)1) [1..3]
Sum types
Type definition
type Shape =
| Circle of radius:float
| Rectangle of width:float * height:float
Pattern matching
match shape with
| Circle(r) -> 2.0 * r
| Rectangle(_, h) -> h
Active Patterns
let (|FormulaText|_|) (s:string) =
if s.StartsWith "=" then Some(s.Substring(1))
else None
let (|NumberValue|_|) (s:string) =
match System.Decimal.TryParse(s) with
| true, n -> Some(n)
| false, _ -> None
let parse = function
| FormulaText text -> Formula text
| NumberValue n -> Number n
| _ -> invalidOp "Unexpected text"
packages |> map
• Cabal -> Paket
• Shake -> Fake
• Parsec -> FParsec
• QuickCheck -> FsCheck
• HappStack -> Suave
• gchjs -> WebSharper
• Idris -> F*
Turtle Computation Expression
let british = turtle {
LIFT THE PEN UP
WALK 4 STEPS
TURN 3 GRADATIONS TO THE RIGHT
PICK THE GREEN PEN
PUT THE PEN DOWN
DO AS leonardo
LIFT THE PEN UP
WALK 4 STEPS
PICK THE RED PEN
PUT THE PEN DOWN
REPEAT 3 TIMES WHAT leonardo DOES
}
Elm’s Mario Tutorial in F# with FunScript
Pointless free programming
let step dir mario =
mario |> physics |> walk dir |> gravity |> jump dir
let step dir =
physics >> walk dir >> gravity >> jump dir
Type providers: JSON
open FSharp.Data
type Simple = JsonProvider<""" { "name":"John", "age":94 } """>let simple = Simple.GetSample()
simple.Age
#FsAdvent#FSharp