functional programming with and without haskell

27
Functional Programming — Alcides Fonseca with and without Haskell

Upload: alcides-fonseca

Post on 16-Apr-2017

265 views

Category:

Software


2 download

TRANSCRIPT

Page 1: Functional Programming with and without Haskell

Functional Programming

— Alcides Fonseca

with and without Haskell

Page 2: Functional Programming with and without Haskell

Functional Programming\ˈfəŋ(k)-shnəl prō-ˌgrammiŋ\

The art of writing programs that function as expected.noun.

Page 3: Functional Programming with and without Haskell
Page 4: Functional Programming with and without Haskell
Page 5: Functional Programming with and without Haskell

int ageOf(Person p);

Page 6: Functional Programming with and without Haskell

int ageOf(Person p) {return today() - birthdate[d];

}

Page 7: Functional Programming with and without Haskell
Page 8: Functional Programming with and without Haskell
Page 9: Functional Programming with and without Haskell

double :: Int -> Intdouble x = x + x

Page 10: Functional Programming with and without Haskell

times :: Int -> Int -> Inttimes 0 _ = 0times 1 x = xtimes n x = x + (times (n-1) x)

double = times 2double :: Int -> Int

Page 11: Functional Programming with and without Haskell
Page 12: Functional Programming with and without Haskell

times 1 n = ntimes a b = a + (times (b-1) a)

power 1 n = npower a b = a * (power (b-1) a)

recursiveOp :: (Int -> Int -> Int) -> Int -> Int -> IntrecursiveOp op a 1 = arecursiveOp op a b = op a (self a (b-1))

where self = recursiveOp op

times = recursiveOp (+)double = times 2

Page 13: Functional Programming with and without Haskell

recursiveOp :: Num t => (t -> t -> t) -> t -> Int -> trecursiveOp op a 1 = arecursiveOp op a b = op a (self a (b-1))

where self = recursiveOp op

Page 14: Functional Programming with and without Haskell
Page 15: Functional Programming with and without Haskell

ones :: [Int]ones = 1:ones

Page 16: Functional Programming with and without Haskell

doubleList :: [Int] -> [Int]doubleList [] = []doubleList (d:ds) = (double d):(doubleList ds)

:1 :2 :3 :4 []

applyOverList :: (Int -> Int) -> [Int] -> [Int]applyOverList f xs = [ f x | x <- xs ]

map :: (a -> b) -> [a] -> [b]foldr :: (a -> b -> b) -> b -> [a] -> b

Page 17: Functional Programming with and without Haskell
Page 18: Functional Programming with and without Haskell

quicksort :: [Int] -> [Int]quicksort [] = []quicksort (x:xs) = (quicksort (filter (<= x) xs)) ++ [x] ++ (quicksort (filter (> x) xs))

Page 19: Functional Programming with and without Haskell
Page 20: Functional Programming with and without Haskell

main :: IO ()main = do putStr "double: " putStrLn (show (double 3))

main :: (RealWorld ->) ((),RealWorld)main = return a where a = putStr "double: "

>>= putStrLn (show (double 3))

(>>=) :: IO a -> (a -> IO b) -> IO b

return :: a -> IO a

Page 21: Functional Programming with and without Haskell

Resumo

Page 22: Functional Programming with and without Haskell

The Good• Modularity of Patterns

(1001 classes = 3 lines of code)

• Signatures express all effects (longer, but makes dependencies and bugs easier to find)

• Speeds in the order of magnitude of C

• Hackage, Cabal & Hoogle

Page 23: Functional Programming with and without Haskell

The Great

• loc(f) <= 5

• If it compiles, it will most surely work

• Free parallelization

• Can be proved correct

Page 24: Functional Programming with and without Haskell

The Bad

Page 25: Functional Programming with and without Haskell

“Lisp is worth learning for the profound enlightenment experience you will have when you finally get it; That experience will make you a better programmer for the rest of your days, even if you never actually use Lisp itself a lot.”

— Eric Raymond

“Ditto for Haskell.” — Alcides Fonseca

Page 26: Functional Programming with and without Haskell

Learn Haskell, so you can improve at X

trips = [ (from_, to) for from_ in cities for to in cities if not to == from_ ]

def parseArgument(arg: String):String = arg match { case "-h" | "--help" => displayHelp case "-v" | "--version" => displayVersion case _ => "RTFM" }

double integral(double (*f)(double x), double a, double b);

var files = from file in DirInfo.EnumerateFiles()where file.CreationTimeUtc > dt1 &file.CreationTimeUtc < dt2 select file;

Page 27: Functional Programming with and without Haskell

Thank you

[email protected]

https://github.com/alcides/haskell_tutorial

— Alcides