haskell overview ii (2a)aug 23, 2016  · haskell overview ii 17 young won lim 8/23/16 lambda...

44
Young Won Lim 8/23/16 Haskell Overview II (2A)

Upload: others

Post on 23-Jul-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Young Won Lim8/23/16

Haskell Overview II (2A)

Page 2: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Young Won Lim8/23/16

Copyright (c) 2016 Young W. Lim.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".

Please send corrections (or suggestions) to [email protected].

This document was produced by using OpenOffice.

Page 3: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 3 Young Won Lim8/23/16

Based on

Haskell Tutorial, Medak & Navratilftp://ftp.geoinfo.tuwien.ac.at/navratil/HaskellTutorial.pdf

Yet Another Haskell Tutorial, Daumehttps://www.umiacs.umd.edu/~hal/docs/daume02yaht.pdf

Page 4: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 4 Young Won Lim8/23/16

Solving a list of quadratic equations

roots :: (Float, Float, Float) -> (Float, Float)roots (a,b,c) = if d < 0 then error "sorry" else (x1, x2)

where x1 = e + sqrt d / (2 * a)x2 = e - sqrt d / (2 * a)d = b * b - 4 * a * ce = - b / (2 * a)

real :: (Float, Float, Float) -> Boolreal (a,b,c) = (b*b - 4*a*c) >= 0

p1 = (1.0,2.0,1.0) :: (Float, Float, Float)p2 = (1.0,1.0,1.0) :: (Float, Float, Float)ps = [p1,p2]newPs = filter real psrootsOfPs = map roots newPs

Page 5: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 5 Young Won Lim8/23/16

User Defined Types (1)

data Bool = True | False

data Color = Red | Green | Blue

data Point a = Pt a a

https://www.haskell.org/tutorial/goodies.html

Type Constructor Data ConstructorThe type being defined here is Bool, and it has exactly two values: True and False.

A nullary constructor:takes no arguments

A multi-constructor

A single constructorA unary constructor(one argument a)

Pt :: a -> a -> Point a

Pt 2.0 3.0 :: Point FloatPt 'a' 'b' :: Point CharPt True False :: Point Bool

Type Constructor Data Constructor

Page 6: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 6 Young Won Lim8/23/16

User Defined Types (2)

data Polynom = Poly Float Float Float

data the keyword

Polynom the name of the data type

Poly the constructor function (:t Poly)

Float the three arguments to the Poly constructor

Page 7: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 7 Young Won Lim8/23/16

User Defined Types (3)

data Polynom = Poly Float Float Float

roots2 :: Polynom -> (Float, Float)

roots (Poly a b c) = … function definition …

p1, p2 :: Polynom

P1 = Poly 1.0, 2.0, 3.0

P2 = Poly 1.0, 3.0, (-5.0)

Page 8: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 8 Young Won Lim8/23/16

Recursive Definition of Lists

data [a] = [ ] | a : [a]

List = [ ] | (a : List)

https://www.haskell.org/tutorial/goodies.html

Type Constructor Data Constructor

an empty list

a list with at least one element

[ ] (x:xs)

Any type is ok but The type of every element in the list must be the same

Page 9: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 9 Young Won Lim8/23/16

Parameterized Data Types

data List a = L a (List a) | Empty

L1, L2, L3 :: List Integer

L1 = Empty

L2 = L 1 L1

L3 = L 5 L2

L4 = L 1.5 Empty :: List Double

Parameter

Constructor a (a)

Page 10: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 10 Young Won Lim8/23/16

Polymorphic Type

types that are universally quantified in some way over all types

essentially describe families of types

(forall a) [a] is the family of types consisting of,

for every type a, the type of lists of a.

● lists of integers (e.g. [1,2,3])

● lists of characters (['a','b','c'])

● lists of lists of integers, etc.

● [2,'b'] is not a valid example

https://www.haskell.org/tutorial/goodies.html

Page 11: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 11 Young Won Lim8/23/16

Subset Polymorphism

roots :: (Floating a) => (a, a, a) -> (a, a)

https://www.haskell.org/tutorial/goodies.html

Page 12: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 12 Young Won Lim8/23/16

Parameterized Polymorphism

plus :: a -> a -> a,

plus :: Int -> Int -> Int,

plus :: Rat -> Rat -> Rat,

data List a = L a (List a) | Empty

listlen :: List a -> Int

listlen Empty = 0

listlen (L _ list) = 1 + listlen list

https://www.haskell.org/tutorial/goodies.html

Page 13: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 13 Young Won Lim8/23/16

Counting functions

l1 [] = 0l1 (x:xs) = 1 + l1 xs

l2 xs = if xs == [] then 0 else 1 + l2 (tail xs)

l3 xs | xs == [] = 0 | otherwise = 1 + l3 (tail xs)

l4 = sum . map (const 1)

l5 xs = foldl inc 0 xs where inc x _ = x+1

l6 = foldl’ (\n _ -> n + 1) 0

Pattern Matching

if-then-else

guard notation

replace and sum

local function, local counter

lambda expression

Page 14: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 14 Young Won Lim8/23/16

Guard Notation

sign x | x > 0 = 1 | x == 0 = 0| x < 0 = -1

if (x > 0) sign = +1;else if (x == 0) sign = 0;else sign= -1

https://www.haskell.org/tutorial/patterns.html

Page 15: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 15 Young Won Lim8/23/16

Anonymous Function

https://www.haskell.org/tutorial/patterns.html

λ x → x2

\ x -> x*x

In Lambda CalculusLambda ExpressionLambda AbstractionAnonymous Function

Input

Prompt> (\x -> x + 1) 45 :: Integer

Prompt> (\x y -> x + y) 3 58 :: Integer

addOne = \x -> x + 1

Page 16: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 16 Young Won Lim8/23/16

Naming a Lambda Expression

inc x = x + 1

add x y = x + y

https://www.haskell.org/tutorial/patterns.html

inc = \x -> x + 1

add = \x y -> x + y

Input

Lambda Expression

Page 17: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 17 Young Won Lim8/23/16

Lambda Calculus

The lambda calculus consists of a language of lambda terms, which is defined by a certain formal syntax, and a set of transformation rules, which allow manipulation of the lambda terms.

These transformation rules can be viewed as an equational theory or as an operational definition.

All functions in the lambda calculus are anonymous functions, having no names.

They only accept one input variable, with currying used to implement functions with several variables.

Page 18: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 18 Young Won Lim8/23/16

Composite Function (1)

(.) :: (b -> c) -> (a -> b) -> (a -> c)

f . g = \ x -> f (g x)

(f . g) x = f (g x)

https://www.haskell.org/tutorial/patterns.html

f g f(g(x))

Page 19: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 19 Young Won Lim8/23/16

Composite Function (2)

p1 = (1.0,2.0,1.0) :: (Float, Float, Float)p2 = (1.0,1.0,1.0) :: (Float, Float, Float)ps = [p1,p2]newPs = filter real psrootsOfPs = map roots newPs

RootsOfPs2 = (map roots . filter real) ps

Page 20: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 20 Young Won Lim8/23/16

Local Variables

lend amt bal = let reserve = 100 newBal = bal - amt in if bal < reserve then Nothing else Just newBal

lend2 amt bal = if amt < reserve * 0.5 then Just newBal else Nothing where reserve = 100 newBal = bal - amt

http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html

Page 21: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 21 Young Won Lim8/23/16

Local Function

pluralise :: String -> [Int] -> [String]pluralise word counts = map plural counts where plural 0 = "no " ++ word ++ "s" plural 1 = "one " ++ word plural n = show n ++ " " ++ word ++ "s"

http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html

Page 22: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 22 Young Won Lim8/23/16

Local Function

roots :: (Float, Float, Float) -> (Float, Float)

type PolyT = (Float, Float, Float)type RootsT = (Float, Float)

roots :: PolyT -> RootsT

http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html

typedef

Page 23: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 23 Young Won Lim8/23/16

Infix operators as functions

(x+) = \y -> x + y

(+y) = \x -> x + y

(+) = \x y -> x + y

https://www.haskell.org/tutorial/functions.html

Input

Input

Inputs

partial application of an infix operator

Page 24: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 24 Young Won Lim8/23/16

Infix operators as function values

inc = (+1)

add = (+)

map (+) [1,2,3]

https://www.haskell.org/tutorial/functions.html

[(+1),(+2),(+3)]

Page 25: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 25 Young Won Lim8/23/16

Sections

(+), (*) :: Num a => a -> a -> a

? 3 + 4

? (+) 3 4

http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html

a belongs to the Num type class

(+) :: Num a => a -> a -> a (*) :: Num a => a -> a -> a

Page 26: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 26 Young Won Lim8/23/16

Sections

(3 +) :: Num a => a -> a

? map (3 +) [4, 7, 12]

? filter (==2) [1,2,3,4] [2]? filter (<=2) [1,2,3,4] [1,2]

? filter (2<) [1,2,3,4] [3,4]? filter (<2) [1,2,3,4] [1]? filter (2>) [1,2,3,4] [1]? filter (>2) [1,2,3,4] [3,4]

http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html

Page 27: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 27 Young Won Lim8/23/16

List Comprehensions

[x | x <- [0..100], odd x]

? f [2,3,4] [5,6] where f xs ys = [x*y | x <- xs, y <- ys][10,12,15,18,20,24] :: [Integer]

2 53 64

http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html

{x ∣ x ∈ [0,100] ,odd x}

Page 28: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 28 Young Won Lim8/23/16

Map as a list comprehension

map f xs = [ f x | x <- xs]

http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html

Page 29: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 29 Young Won Lim8/23/16

Filter as a list comprehension

filter p xs = [x | x <- xs, p x]

http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html

Page 30: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 30 Young Won Lim8/23/16

Currying

f x y = blabla

f x = \y -> blabla

f = \x -> (\y -> blabla)

(f x) y

http://stackoverflow.com/questions/3794371/haskell-currying-need-further-explanation

f is a function with one argument, x, and returns another function with one argument, y, and then returns the actual result blabla. This is known as currying.

Page 31: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 31 Young Won Lim8/23/16

Curry & Uncurry

f :: a -> b -> c curried form

g :: (a -> b) -> c uncurried form

f = curry g f x y g (x, y) curryingg = uncurry f g(x, y) f x y uncurrying

f x y = g (x, y)

https://wiki.haskell.org/Currying

Currying is the process of transforming a function that takes multiple arguments into a function that takes just a single argument and returns another function if any arguments are still needed.

Page 32: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 32 Young Won Lim8/23/16

Functional & Imperative Programming

c := 0for i:=1 to n do c := c + a[i] * b[i]

inn2 :: Num a => ([a], [a]) -> a

Inn2 = foldr (+) 0 . map (uncurry (*)) . uncurry zip

(1) uncurry zip(2) map (uncurry (*))(3) foldr (+) 0

http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html

a belongs to the Num type class

uncurried form

Page 33: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 33 Young Won Lim8/23/16

Simple List Functions – zip and unzip functions

zip [a1, a2, a3] [b1,b2,b3]

[(a1,b1),(a2,b3),(a3,b3)]

uncurry zip ( [a1, a2, a3], [b1,b2,b3] )

[(a1,b1),(a2,b2),(a3,b3)]

f x y g (x, y) curryingg(x, y) f x y uncurrying

Page 34: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 34 Young Won Lim8/23/16

Simple List Functions – zip and unzip functions

map (uncurry (*) ) [(a1,b1),(a2,b2),(a3,b3)]

[a1*b1, a2*b2, a3*b3]

map foldr (+) 0 [a1*b1, a2*b2, a3*b3]

a1*b1 + a2*b2 + a3*b3

Page 35: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 35 Young Won Lim8/23/16

Functional & Imperative Programming

inn2 :: Num a => ([a], [a]) -> a

inn2 = foldr (+) 0 . map (uncurry (*)) . uncurry zip

inn2 x = (foldr (+) 0 . map (uncurry (*)) . uncurry zip) x inn2 x = (foldr (+) 0 (map (uncurry (*)) (uncurry zip x)))

testvec = inn2 ([1,2,3], [4,5,6])

http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html

a belongs to the Num type class

uncurried form

Page 36: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 36 Young Won Lim8/23/16

foldr, foldl, fold1

foldr (-) 1 [4,8,5]

4 - (foldr (-) 1 [8,5])4 - (8 - foldr (-) 1 [5])4 - (8 - (5 - foldr (-) 1 []))4 - (8 - (5 - 1))4 - (8 - 4)4 - 40

(4 (8 (5)))(4 (8 (5)))(4 (8 (5)))

foldl (-) 1 [4,8,5]

(foldl (-) 1 [4,8]) - 5((foldl (-) 1 [4]) - 8) – 5((1 – 4) – 8) – 5((-3) – 8) – 5-11 -5 -16

(((4) 8) 5)(((4) 8) 5)(((4) 8) 5)

foldl (+) 0 [4,8,5]foldl1 (+) [4,8,5]

foldl (*) 1 [4,8,5]foldl1 (*) [4,8,5]

No starting value argument

Page 37: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 37 Young Won Lim8/23/16

Fold function applications

ft = foldr (*) 7 [2,3]ut = uncurry (+) (5,6)zt = zip “Haskell” [1,2,3,4,5,6,7]

innXa, innXb : [[Integer]] -> Integer

innXa = foldr (+) 0 . map (foldr (*) 1) . transpose

innXb = foldr1 (+) . map (foldr1 (*)) . transpose

http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html

(2 (3 * 7))(2 * 3 *7)

(+) 5 6

uncurry (+) (5, 6)

Page 38: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 38 Young Won Lim8/23/16

Transpose

transpose :: [[a]] -> [[a]]

transposes the rows and columns of its argument.

transpose [[a,b,c],[1,2,3]] == [[a,1],[b,2],[c,3]]

elements may be skippe

transpose [[10,11],[20],[],[30,31,32]] == [[10,20,30],[11,31],[32]]

https://hackage.haskell.org/package/base-4.9.0.0/docs/Data-List.html

Page 39: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 39 Young Won Lim8/23/16

Transpose

transpose :: [[a]] -> [[a]]transpose [] = []transpose ([]:xss) = transpose xsstranspose ((x:xs) : xss) = (x : [h | (h:t) <- xss]) :

transpose (xs : [t | (h:t) <- xss])

https://hackage.haskell.org/package/base-4.9.0.0/docs/Data-List.html

Page 40: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 40 Young Won Lim8/23/16

Inner Product

innXa, innXb : [[Integer]] -> Integer

innXa = foldr (+) 0 . map (foldr (*) 1) . transpose

innXb = foldr1 (+) . map (foldr1 (*)) . transpose

http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html

[[1, 2, 3], [10, 20, 30]]

[[1, 10], [2, 20], [3, 30]]

[1*10, 2*20, 3*30]

1*10 + 2*20 + 3*30

Page 41: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 41 Young Won Lim8/23/16

Type Class

Defining a type class by specifying a set of function or constant names, together with their respective types, that must exist for every type that belongs to the class

https://en.wikipedia.org/wiki/Type_class

Page 42: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 42 Young Won Lim8/23/16

Type Class Definition

a type class Eq intended to contain types that admit equality would be declared in the following way:

class Eq a where (==) :: a -> a -> Bool (/=) :: a -> a -> Bool

type a belongs to type class Eq if functions named (==) and (/=) are defined

https://en.wikipedia.org/wiki/Type_class

Page 43: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Haskell Overview II 43 Young Won Lim8/23/16

Class Constraint

define a function elem to determines if an element is in a list

elem :: (Eq a) => a -> [a] -> Boolelem y [] = Falseelem y (x:xs) = (x == y) || elem y xs

the function elem has the type a -> [a] -> Bool with the context (Eq a),

constrains the types which a can range over to those a which belong to the Eq type class

(Note: Haskell => can be called a 'class constraint'.)https://en.wikipedia.org/wiki/Type_class

Page 44: Haskell Overview II (2A)Aug 23, 2016  · Haskell Overview II 17 Young Won Lim 8/23/16 Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined

Young Won Lim8/23/16

References

[1] ftp://ftp.geoinfo.tuwien.ac.at/navratil/HaskellTutorial.pdf[2] https://www.umiacs.umd.edu/~hal/docs/daume02yaht.pdf