22
Introduction to Functional Programming with Haskell Mohammed Al Rujayi Works @ Wasphi Twitter, Github: @mohalrej

Introduction to FP with Haskell - Parmg · Introduction to Functional Programming with Haskell Mohammed Al Rujayi Works @ Wasphi Twitter, Github: @ mohalrej

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to FP with Haskell - Parmg · Introduction to Functional Programming with Haskell Mohammed Al Rujayi Works @ Wasphi Twitter, Github: @ mohalrej

IntroductiontoFunctionalProgrammingwithHaskell

MohammedAlRujayiWorks@Wasphi

Twitter,Github:@mohalrej

Page 2: Introduction to FP with Haskell - Parmg · Introduction to Functional Programming with Haskell Mohammed Al Rujayi Works @ Wasphi Twitter, Github: @ mohalrej

WhatisFunctionalProgramming?

Page 3: Introduction to FP with Haskell - Parmg · Introduction to Functional Programming with Haskell Mohammed Al Rujayi Works @ Wasphi Twitter, Github: @ mohalrej

CC++

Python

C#

RubyPHP

Swift

Perl

Smalltalk

Erlang

Haskell

ML

AdgaElm

F#

Lisp

Ocaml

Imperative(HOW?)

Functional(What?)

Page 4: Introduction to FP with Haskell - Parmg · Introduction to Functional Programming with Haskell Mohammed Al Rujayi Works @ Wasphi Twitter, Github: @ mohalrej

FunctionalProgramminghasthefollowing…

• Highorderfunctions,functionsthattakeotherfunctions

• Purefunctions,nosideeffects

• Nomutations

• Recursionnotloops

Page 5: Introduction to FP with Haskell - Parmg · Introduction to Functional Programming with Haskell Mohammed Al Rujayi Works @ Wasphi Twitter, Github: @ mohalrej

It’sjustlikeLegos!

Page 6: Introduction to FP with Haskell - Parmg · Introduction to Functional Programming with Haskell Mohammed Al Rujayi Works @ Wasphi Twitter, Github: @ mohalrej

Anexample

Thefactorialfunctions

5!=5*4*3*2*1

3!=3*2*1

Page 7: Introduction to FP with Haskell - Parmg · Introduction to Functional Programming with Haskell Mohammed Al Rujayi Works @ Wasphi Twitter, Github: @ mohalrej

FactorialinPython…

def factorial(n):result = 1while n >= 1: # loop

result = result * n # mutationprint (result) # side effects n = n – 1 # mutation

return result

Page 8: Introduction to FP with Haskell - Parmg · Introduction to Functional Programming with Haskell Mohammed Al Rujayi Works @ Wasphi Twitter, Github: @ mohalrej

FactorialinHaskell…

Factorial 0 = 1Factorial 1 = 1Factorial n = n * Factorial (n-1)

Page 9: Introduction to FP with Haskell - Parmg · Introduction to Functional Programming with Haskell Mohammed Al Rujayi Works @ Wasphi Twitter, Github: @ mohalrej

Haskelldemo

Page 10: Introduction to FP with Haskell - Parmg · Introduction to Functional Programming with Haskell Mohammed Al Rujayi Works @ Wasphi Twitter, Github: @ mohalrej

We’llcover

• Lists

• Application

• Lambdafunctions

• Maps,folds,filters

• Patternmatching

• Composition

Page 11: Introduction to FP with Haskell - Parmg · Introduction to Functional Programming with Haskell Mohammed Al Rujayi Works @ Wasphi Twitter, Github: @ mohalrej

Fibonacciseries

0,1,1,2,3,5,8,13…

Startingwith0,and1

Fib(n)=Fib(n-1)+Fib(n-2)

Page 12: Introduction to FP with Haskell - Parmg · Introduction to Functional Programming with Haskell Mohammed Al Rujayi Works @ Wasphi Twitter, Github: @ mohalrej

Fibonacciseries

0,1,1,2,3,5,8,13…

Startingwith0,and1

Fib(n)=Fib(n-1)+Fib(n-2)

Page 13: Introduction to FP with Haskell - Parmg · Introduction to Functional Programming with Haskell Mohammed Al Rujayi Works @ Wasphi Twitter, Github: @ mohalrej

Notagooddefinition!

F(5)

F(4)

F(3)

Page 14: Introduction to FP with Haskell - Parmg · Introduction to Functional Programming with Haskell Mohammed Al Rujayi Works @ Wasphi Twitter, Github: @ mohalrej

Notagooddefinition!

F(5)

F(4)

F(3)

F(3)

F(2)

F(2)

F(1)

Page 15: Introduction to FP with Haskell - Parmg · Introduction to Functional Programming with Haskell Mohammed Al Rujayi Works @ Wasphi Twitter, Github: @ mohalrej

Notagooddefinition!

F(5)

F(4)

F(3)

F(3)

F(2)

F(2)

F(1)

Page 16: Introduction to FP with Haskell - Parmg · Introduction to Functional Programming with Haskell Mohammed Al Rujayi Works @ Wasphi Twitter, Github: @ mohalrej

Abetteridea

(x,y)->(x+y,x)

Page 17: Introduction to FP with Haskell - Parmg · Introduction to Functional Programming with Haskell Mohammed Al Rujayi Works @ Wasphi Twitter, Github: @ mohalrej

Abetteridea

(x,y)->(x+y,x)

At(0,1):

(0,1)->(1,0)

(1,0)->(1,1)

(1,1)->(2,1)

(2,1)->(3,2)

Page 18: Introduction to FP with Haskell - Parmg · Introduction to Functional Programming with Haskell Mohammed Al Rujayi Works @ Wasphi Twitter, Github: @ mohalrej

AllstandardfunctionsareimplementedinHaskell!!

map f [] = []map f (x:xs) = (f x) : map f xs

Page 19: Introduction to FP with Haskell - Parmg · Introduction to Functional Programming with Haskell Mohammed Al Rujayi Works @ Wasphi Twitter, Github: @ mohalrej

ThecoreofHaskell

Lambdas

fold

map

filteriterate

:

mod

++

Factorial

Fibonacci

Yourprogram

Page 20: Introduction to FP with Haskell - Parmg · Introduction to Functional Programming with Haskell Mohammed Al Rujayi Works @ Wasphi Twitter, Github: @ mohalrej

EasiestwaytogetHaskell

• Haskellplatform

https://www.haskell.org/platform/

• Haskellformac

http://haskellformac.com

Page 21: Introduction to FP with Haskell - Parmg · Introduction to Functional Programming with Haskell Mohammed Al Rujayi Works @ Wasphi Twitter, Github: @ mohalrej

Appendix:Democodemodule Ksu where

-- First factorial, implemented with recursionfac 0 = 1fac 1 = 1fac n = n * fac(n-1)

-- Composition of two functionsksu_01 = map (\ x-> x^2) . map (\ x -> x^2)

-- Sum implemented in terms of foldsum_01 list = foldl (\ x y -> x + y) 0 list

-- The fib function implemented with recursionfib 0 = 0fib 1 = 1fib n = fib (n-1) + fib (n-2)

-- The fib function implemented in terms of iteratefib_01 n = take n $ iterate (\(x, y) -> (x+y, x)) (0, 1)fib_02 = last . map (\(x, y) -> y) . fib_01

Page 22: Introduction to FP with Haskell - Parmg · Introduction to Functional Programming with Haskell Mohammed Al Rujayi Works @ Wasphi Twitter, Github: @ mohalrej

يعطیكم العافیة