FSharp eye for the Haskell guy - London 2015

35
F# eye for the Haskell weenie Phillip Trelford, @ptrelford London Haskell, 2015

Transcript of FSharp eye for the Haskell guy - London 2015

F# eye for the Haskell weeniePhillip Trelford, @ptrelford

London Haskell, 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# CommunityPhillip Trelford, @ptrelford

London Haskell, 2015

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

F# startups: Jet.com $570M raised

Onikira – Demon Killer: available on Steam

Live Demos: Bread & Butter stuffPhil Trelford, @ptrelford

F# eye for the Haskell weenie

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"

Live Demos: Familiar conceptsPhillip Trelford, @ptrelford

London Haskell, 2015

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

}

M-Brace cloud Computation Expression

Operator overloading

let (<*>><) a b = a + " approves " + b

"Fish" <*>>< "custom operators!"

Pointless free programming

let step dir mario =

mario |> physics |> walk dir |> gravity |> jump dir

let step dir =

physics >> walk dir >> gravity >> jump dir

Live Demos: Type ProvidersPhillip Trelford, @ptrelford

London Haskell, 2015

Type providers: JSON

open FSharp.Data

type Simple = JsonProvider<""" { "name":"John", "age":94 } """>let simple = Simple.GetSample()

simple.Age

R – TYPE PROVIDER

World bank data with FunScript

Live Demos: Apps & GamesPhillip Trelford, @ptrelford

London Haskell, 2015

Cross Platform Games

Path to Go | XBLA Pissed off Owls| iOS Pool | WebGL

http://tinyurl.com/funbasic

FUN BASIC - WINDOWS STORE APP

Live Debugging

Profiling with YourKit

ResourcesPhillip Trelford, @ptrelford

London Haskell, 2015

Meet the F#ers on Twitter: #fsharp

@rickasaurus

@tomaspetricek

@jonharrop

Buy the Book

Questions or Pub?

• Twitter• @ptrelford

• Blog• http://trelford.com/blog