42
Learning FP Lev Walkin @levwalkin

Diving into Functional Programming

Embed Size (px)

Citation preview

Page 1: Diving into Functional Programming

Learning FPLev Walkin@levwalkin

Page 2: Diving into Functional Programming

Myself

Founder, Chief Editor of a peer-reviewed «Practice of Functional Programming» journal http://fprog.ru/

CTO Echo, a venture backed companyhttp://aboutecho.com/

Proficient in N languages, M of them FP

Page 3: Diving into Functional Programming

Echo

50,000+ HTTP requests per second

500,000+ simultaneous users on site

1,527,721,774 search queries last month

JavaScript, Clojure, Haskell, C, LAMP, RoR, OCaml, Python, Perl, Visual Basic, C++

Page 4: Diving into Functional Programming

What’s your reason to learn FP?

1.You already suspect that FP approach works best for your domain

2.You want to learn FP to expand your tool set

Page 5: Diving into Functional Programming

Mature options

Haskell (OCaml, F#)

Scala

Clojure (CL, Scheme)

Erlang

Page 6: Diving into Functional Programming

Statically typed

Haskell, OCaml, F# — part of ML family

Utilize Hindley—Milner type inference

You might not even SEE any types

Types provide useful documentation

Page 7: Diving into Functional Programming

Statically typed

Scala

Very sophisticated type system

Type inference fails often (you have to annotate with types)

Lots of syntax sugar, lots of ways to do things

Page 8: Diving into Functional Programming

Type inference

-- Haskell program

main = print “Hello, world!”

(* OCaml program *)

let main = print_string “Hello, world!”

Page 9: Diving into Functional Programming

Explicit types

-- Haskell programmain :: IO ()main = print “Hello, world!”

(* OCaml program *)val main : unitlet main = print_string “Hello, world!”

Page 10: Diving into Functional Programming

Dynamically typed

LISP (type annotations are NOT static types)

Erlang (Dialyzer helps a bit)

Page 11: Diving into Functional Programming

Pure FP training

. . .requires pure language.

Haskell is the only viable option.

Page 12: Diving into Functional Programming

Purity in Haskell

Function have no side effects

All state changes are only through specially constructed monads (IO, ST)

Static types force isolation

Lazyness thrown in for a deeper fun

Page 13: Diving into Functional Programming

HaskellMedium-size syntax, relatively easy to learn, but...

Simple & interesting things are EASY

Yet, there are LOTS of abstractions and approaches to learn: Type classes, Monads, Arrows, parsing, pretty-printing.

This is why we chose Haskell, right?

Page 14: Diving into Functional Programming

“One”

“example.”“silly”

Page 15: Diving into Functional Programming

http://fprog.ru/2009/issue2/roman-dushkin-algebraic-data-types/

Haskell, OCaml, F#

data Tree a =! Leaf a! | Node (Tree a) (Tree a)

type ‘a tree =! | Leaf of ‘a! | Node of ‘a tree * ‘a tree

type Tree<'a> = | Leaf of ‘a | Branch of ‘a Tree * ‘a Tree

Page 16: Diving into Functional Programming

“One”

“example.”“silly”

3

85

Page 17: Diving into Functional Programming

Haskelldata Tree String =! Leaf String! | Node (Tree String) (Tree String)

strToLenTree :: Tree String ! Tree Int

strToLenTree (Leaf s) = Leaf (length s)strToLenTree (Node left right) =! ! ! Node (strToLenTree left)! ! ! ! ! (strToLenTree right)

Page 18: Diving into Functional Programming

Haskelldata Tree a =! Leaf a! | Node (Tree a) (Tree a)

mapTree :: (a ! b) ! Tree a ! Tree b

mapTree f (Leaf x) = Leaf (f x)mapTree f (Node left right) =! ! ! ! Node (mapTree f left)! ! ! ! ! ! (mapTree f right)

strToLenTree :: Tree String ! Tree IntstrToLenTree = mapTree length

Page 19: Diving into Functional Programming

Prototyping[~]> ghci tree.hs GHCi, version 7.0.4 :? for help[1 of 1] Compiling Main ( tree.hs, interpreted )Ok, modules loaded: Main.*Main> let tree = Node (Leaf "An") (Leaf "example")*Main> mapTree (\s -> length s) treeNode (Leaf 2) (Leaf 7)*Main> mapTree (\s -> filter Data.Char.isUpper s) treeNode (Leaf "A") (Leaf "")*Main> mapTree (filter Data.Char.isLower) treeNode (Leaf "n") (Leaf "example")

*Main> :t mapTreemapTree :: (a -> b) -> Tree a -> Tree b*Main> :t mapTree lengthmapTree length :: Tree [a] -> Tree Int

Page 20: Diving into Functional Programming

LISPClojure: immutability, persistent data structures, JVM

Scheme: very small language, suitable for teaching/learning

Common Lisp: it is a fat multiparadigm language and has everything (though parts may be rotten a bit)

Page 21: Diving into Functional Programming

Erlang

Simple language

Telecom systems, 24/7, системы массового обслуживания (web?)

Hot code reload, deep introspection, embedded facilities for easier clustering

Page 22: Diving into Functional Programming

Erlang

% Reverse a stringreverse(String) -> reverse2(String, []).

% Reverse with an accumulatorreverse2([ ], Acc) -> Acc;reverse2([Head | Tail], Acc) ->! reverse2(Tail, [Head | Acc]).

Page 23: Diving into Functional Programming

Erlang

% Sends request to the [remote] system% and waits for the response backsend_and_wait(Pid, Message, Timeout) ->! Pid ! Message,! receive! ! Response -> {ok, Response}! after! ! Timeout -> {error, timeout}! end.

Page 24: Diving into Functional Programming

http://tonsky.livejournal.com/tag/clojure

Clojure vs LISP*NEW language, with sound idioms

Immutable data structures are idiomatic

Fast pace of development

JVM may be a big bon for some

Learn it if you want sound, practical language

Page 25: Diving into Functional Programming

Scheme vs LISP*

Small, simple language (R5RS)

A basis for SICP — Structure and Interpretation of Computer Programs

Learn SICP if you want to study Programming

Page 26: Diving into Functional Programming

CL vs LISP*

Many complex ideas mixed in

No enforcement of a good style — you have to do it on your own

Learn it if you want unrestricted power

Page 27: Diving into Functional Programming

http://www.ozon.ru/context/detail/id/8696277/

Haskell vs ML*

More sound type system

LOTS of language research happen on Haskell

Learn it if you want to learn FP

Learn it if it fits your domain well

Page 28: Diving into Functional Programming

http://mirror.ocamlcore.org/ocaml-tutorial.org/the_basics.html

OCaml

Several mostly compatible syntaxes

You can always create a mutable variable or class field, though not idiomatic

Clearer semantics and computation model (straightforward translation to assembly)

Page 30: Diving into Functional Programming

http://www.ozon.ru/context/detail/id/6151130/

F# vs ML*

Works under Microsoft .Net

F# on Mono is somewhat usable

Learn it if you want to tinker with FP on MS platform

Page 31: Diving into Functional Programming

Erlang vs *SMALL language, somewhat bigger OTP

Great support for concurrency (Actors), and parallelism

Hot code reload & run time introspection

OOP on the outside; FP on the inside

Learn it if you build 24/7 production system

Page 32: Diving into Functional Programming

Scala vs *

Big language, a functional C++ of sorts

JVM

Learn it if you want a great tool, not satisfied with Java, yet have no appreciation for true elegance (Clojure)

Page 33: Diving into Functional Programming

Learning FP

Haskell provides models and abstractions (read papers) — pure FP

Scheme teaches you Programming (read SICP)

Everything else is too practical

Page 34: Diving into Functional Programming

Practicing FP.Net → F#

JVM → Clojure (elegance and simplicity), Scala (less restrictions)

!{.Net|JVM} → OCaml (if you do not need libraries), Clojure, Haskell (if you have balls)

24/7 → Erlang

Page 35: Diving into Functional Programming

For individuals

You want flexible system which allows you to cut corners

Common Lisp

Scala

F#

Page 36: Diving into Functional Programming

For teamsYou want to think about lifecycle, support, maintenance, group dynamics

OCaml (static typing provides some guarantees)

Erlang (simple and straightforward, designed to withstand errors, good for novices)

Page 37: Diving into Functional Programming

http://ru.xored.com/2012/12/02/scala/

For teams

Scala gets increasingly popular because people do not appreciate elegance and sound restrictions

People will throw up in a few years working with accumulated Scala code

...like C++

Page 38: Diving into Functional Programming

http://www.rsdn.ru/article/haskell/haskell_part1.xml

Haskell resources

Мягкое введение в Haskell

Изучай Haskell во имя добра!

Page 39: Diving into Functional Programming

http://tonsky.livejournal.com/tag/clojure

Clojure resources

1.Learn spoken English

2.Журнал Никиты Прокопова (tonsky@LJ) содержит ссылки и рекомендации

Page 40: Diving into Functional Programming

http://www.ozon.ru/context/detail/id/3645143/

Erlang resources

1.Programming Erlang: Software for a Concurrent World

2.http://learnyousomeerlang.com

Page 41: Diving into Functional Programming

Shameless plug

Журнал «Практика функционального программирования»

fprog.ru

@fprogru

Page 42: Diving into Functional Programming

Thank you!

[email protected](мы пишем на Руби!)