You are on page 1of 26

Haskellintroduction

ErikDominikus
20150407

1/26

Fundamentalconcepts
Everyvaluehasatype.
Everyfunctionhasoneargument.
Normalorderevaluationgivesprogramsmeaning.
Valuesandtypeshavedifferentnamespaces.
Haskellforcesyoutothinkclearly.

2/26

Valuedefinition
Thedefinitionx = ymeansthatwecanreplaceallxwithy
withoutchangingthemeaningoftheprogram.
Examplesofvaluedefinitions:
x :: Int
x=1
f :: Int -> Int
fx=x+1

Youcanomittypeannotations.Ifyoudo,thecompilerwillinfer
theprincipaltype(themostgeneraltype)forthevalue.

3/26

Functions
Everyfunctionhasoneargument.
f :: (Int, Int) -> Int
f (x, y) = x + y
g :: (Int, Int, Int) -> Int
g (x, y, z) = x * y + z

Afunctiontaking'manyarguments'isafunctionreturninga
function.
f :: Int -> Int -> Int
fxy=x+y
g :: Int -> Int -> Int -> Int
gxyz=x*y+z

4/26

Functions
(->)associatestotheright:
t -> u -> v = t -> (u -> v)

Thatisfunctionthattakestandgivesafunctiontakingu
givingv.
Int -> Int -> Int -> Int = Int -> (Int -> (Int -> Int))

5/26

Functionsarevalues
apply :: (a -> b) -> a -> b
apply f x = f x
leftToRightComposition :: (a -> b) -> (b -> c) -> (a -> c)
leftToRightComposition f g x = g (f x)

6/26

Functionapplication
(+)
(+) 5
(+) 5 7

:: Int -> Int -> Int


:: Int -> Int
:: Int

7/26

Typeinference
g :: Int -> Int
g x = square x + 1
where
square y = y * y

ThecompilerinfersthetypeofsquaretobeInt -> Int.


g x = square x + 1
where
square :: Int -> Int
square y = y * y

ThecompilerinfersthetypeofgtobeInt -> Int.

8/26

Typesynonyms
type A = B

ThismeanswecanreplaceallAwithBwithoutchangingthe
meaningoftheprogram:
WesaythatAisatypesynonymforB.

9/26

Typefunctions
type A a b = a

ThismeanswecanreplaceallA a bwithawithoutchanging
themeaningoftheprogram,forallaandb.
Wecallaandbtypeparameters.

10/26

Typeinhabitants
type A a b = a

A Int Intisinhabitedby0,1,-1,andothers.
A Char Intisinhabitedby'a','b',andothers.
A (A Int Char) IntisthesameasInt.
Aisnotatype.Aisatypefunction.Itdoesnotmakesenseto
talkabouttheinhabitantsofA.

11/26

Datatypes
data A = B | C | D

Thisdefinesfourthings:
thetypeA.
thevalueB,
thevalueC,and
thevalueD.
Allthosevalueshavethesametype:A.
Wesaythosevaluesinhabittheirtype.
WesaythatB,C,andDarethedataconstructorsofA.
12/26

Moredatatypes
data A = B Int

Thisdefinestwothings:
thetypeA,
thevalueBwhosetypeisInt -> A.
IfyougiveanInttoB,yougetanA.
SomeinhabitantsofA:
B0
B1
B (-1)

13/26

Moredatatypes
data A = B Int Int

SomeinhabitantsofA:
B00
B12

14/26

Datatypeswithparameters
data A a = B a a

Thisdefinesmanythings:
thetypefunctionA,
thevalueBhavingtypea -> a -> A a.
Ifaisatype,thenA aisatype.
SomeinhabitantsofA (A Int):
B (B 0 0) (B 1 1)
B (B 1 1) (B 2 2)
B (B 2 (-1)) (B 3 4)

15/26

Patternmatching
Ifyouhave:
data A = B | C Int

thenyoucanwrite:
fun :: A -> String
fun B = "I don't have an Int."
fun (C x) = (++) "I have an Int: " (show x)

whichisthesameas:
fun thing =
case thing of
B -> "I don't have an Int."
C x -> "I have an Int: " ++ show x

16/26

Evaluationstrategy
Haskellusesnormalorderevaluationstrategy.
Supposewehave:
f :: a -> b -> a
fxy=x

Let'stryevaluating:
f (1 + 2) (3 + 4)

17/26

Evaluationstrategy
Normalorder
Headfirst.
f (1 + 2) (3 + 4)
-> 1 + 2
-> 3

-- f x y = x

Applicativeorder
Argumentsfirst.
f (1 + 2) (3 + 4)
-> f 3 (3 + 4)
-> f 3 7
-> 3

-- f x y = x

18/26

Normalvsapplicativeorder
Theydifferwhenanargumentdiverges.

Normalorder
f (1 + 2) (error "foo")
-> 1 + 2
-> 3

Applicativeorder
f (1 + 2) (error "foo")
-> f 3 (error "foo")
-> <error>

19/26

Lists
data [] a = [] | a : [] a
-- (:) a ([] a)
[]
0 : [] -- (:) 0 []
0 : 1 : []
0 : 1 : 2 : []
0 : (1 : (2 : []))

(:)associatestotheright.
Syntacticsugar:
[] a = [a]
[0, 1, 2] = 0 : 1 : 2 : []

-- type
-- value

[0, 1, 2] :: [a]

20/26

Normalorder
Normalorderevaluationenableswritingthislist:
x=1:x
natFrom n = n : natFrom (n + 1)
fib :: [Int]
fib = 1 : 1 : zipWith (+) fib (tail fib)
fib !! 4 = 5
[0,1,2,3] !! 2 = 2
zipWith f (x:xs) (y:ys) = f x y : zipWith f xs ys
zipWith (+) [1,2,3] [4,5,6] = [5,7,9]
zipWith max [1,2,3] [4,3,2] = [4,3,3]
tail (x : y) = y
tail [0,1,2,3] = [1,2,3]

21/26

Inputoutput
getLine
putStrLn
(>>=)
(>>)
return
fmap

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

myRepeat 10 (putStrLn "Hi")


getInt :: IO Int
getInt = fmap read getLine
sumTwo :: IO ()
sumTwo =
getInt >>= \ x ->
getInt >>= \ y ->
putStrLn (show (x + y))
data Either a b = Left a | Right b
data (,) a b = (,) a b

22/26

data Maybe a = Just a | Nothing


fromMaybe :: a -> Maybe a -> a
fromMaybe def (Just val) = val
fromMaybe def Nothing = def
maybe :: b -> (a -> b) -> Maybe a -> a
maybe def fun (Just val) = fun val
maybe def fun Nothing = def
fmap :: (a -> b) -> Maybe a -> Maybe b
fmap f (Just x) = Just (f x)
fmap f Nothing = Nothing

23/26

divide :: Int -> Int -> Maybe Int


divide x 0 = Nothing
divide x y = Just (div x y)
woo :: Maybe Int
woo = do
x <- divide 5 0
y <- divide x 1
return (x + y)
woo :: Maybe Int
woo =
divide 5 0 >>= \ x ->
divide x 1 >>= \ y ->
return (x + y)
return = Just
Just x >>= f = f x
Nothing >>= x = Nothing

24/26

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


sumTree :: Tree Int -> Int
sumTree Leaf = 0
sumTree (Node x l r) = x + sumTree l + sumTree r

25/26

Furtherreading
RealworldHaskell
LearnHaskellfastandhard
AtasteofHaskell
ThePythonparadox
WhatIwishIknewwhenlearningHaskell

26/26

You might also like