mySumV1 [] = 0
mySumV1 (x:xt) = x + mySumV1 xt
myProductV1 [] = 1
myProductV1 (x:xt) = x * myProductV1 xt
— This function is also in the standard library, under the name “map”.
— We write it again to make a bigger point.
— E.g., myMap f [1, 2, 3] = [f 1, f 2, f 3]
myMap f [] = []
myMap f (x:xt) = f x : myMap f xt
— Or imagine g = myMap f:
— myMap f = g
— where
— g [] = []
— g (x:xt) = f x : g xt
g [] = z
g (x:xt) = a function of x and g xt
g = mySumV1: z =0
function: x + g xt — the function is (+)
g = myProductV1: z =1
function: x * g xt — the function is (*)
g = myMap f:
z = []
function: f x : g xt
= (\x r -> f x : r) x (g xt)
myFoldr op z = g
where
g [] = z
g (x:xt) = op x (g xt)
— the function is \x r -> f x : r
Table of Contents
:)suoivbo ssel( elpmaxe erom enO .tuo rotcafer ot gnihtemos si ereht yleruS ?niaga edoc emas eht gnitirw peek ew oD ?rotarepo yranib rehtona tey esu ot tnaw uoy fi tahW
.tniop reggib a ekam ot tub ,)snoisrev retteb sah yrarbil dradnats eht( esu laer rof ton si edoc sihT .tsil a ylpitlum ro mus ot yaw enO
:ot derotcafer era eseht llA
ekil skool taht noitcnuf a si emeht nommoc ehT sh.dloFlleksaH ni
rdlof ldlof dna rdlof
sh.dloFlleksaH ni
foldr
g myFoldr op z
myFoldr :: (a -> b -> b) -> b -> [a] -> b
myFoldr op z [] = z
myFoldr op z (x:xt) = op x (myFoldr op z xt)
— So here are mySumV1, myProductV1, myMap re-expressed as foldr’s:
mySumFoldr xs = foldr (+) 0 xs
myProductFoldr xs = foldr (*) 1 xs
myMapFoldr f xs = foldr (\x r -> f x : r) [] xs
foldr
myMap
myMap f xs = foldr op z xs
xs = []
LHS = myMap f [] = []
RHS = foldr op z [] = z
Define z = [], then
LHS = [] = RHS
op z
by myMap code
by foldr code
xs = x:xt
myMap f xt = foldr op [] xt
myMap f (x:xt) = foldr op [] (x:xt)
LHS
= f x : myMap f xt
RHS
= op x (foldr op [] xt)
= op x (myMap f xt)
by myMap code
by foldr code
by IH
op op x (myMap f xt) = f x : myMap f xt myMap f xt
r op
op op x r = f x : r
(\x r -> f x : r)
op
Table of Contents
.yawyna llet t’nac edoc s’
. yrartibra ot
.yltcerid
. fo noitinfied yb SHR = SHL nehT esu rO !taht tsuj od ot po enfieD :od ot deeN morf ezilareneG :KCIRT TNATROPMI :od ot deeN
).CRI morf mih wenk I .8102 ,21 yaM deid rohtua eht yletanutrofnU( .setre yb ,rdlof gniretsaM morF !foorp etelpmoc a teg dna z dna po rof evlos ot noitcudni dneterp gnisu yaw rehtona si woleB . esu nac uoy taht tceted ot yaw eno swohs evoba ehT
. dna
:esac esaB
dnfi dna noitcudni dneterp yb
:PTW :sisehtopyh noitcudnI :pets noitcudnI
evorP . no gnikrow wohs ll’I
: ot kcab dnapxe ew ylnommoc eroM .siht rof sah yrarbil dradnats ehT
sh.dloFlleksaH ni
mySumV2 xs = g 0 xs
where
— If you want to use induction to prove g, use this specification:
— for all xs, for all a, g a xs = a + sum of xs
g accum [] = accum
— Induction step: The list is x:xt
— Induction hypothesis: for all a, g a xt = a + sum of xt
—
— How to compute accum + sum of (x:xt)?
—
— accum + sum of (x:xt)
— = accum + x + sum of xt
— = (accum + x) + sum of xt
— = g (accum + x) xt
g accum (x:xt) = g (accum + x) xt
by IH
slowReverse [] = []
slowReverse (x:xs) = slowReverse xs ++ [x]
++
myReverse xs = g [] xs
where
++
— If you want to use induction, the specification is:
— for all xs, for all a, g a xs = (reversal of xs) ++ a
g accum [] = accum
— Induction step: The list is x:xt
— IH: for all a: g a xt = (reversal of xt) ++ a
—
— How to compute (reversal of (x:xt)) ++ accum ?
—
— (reversal of (x:xs)) ++ accum
Table of Contents
.)kcats rof taerg si tsil deknil -ylgnis dna( rotalumucca kcats OFIL a htiw ,pool a rof noitcnuf repleh a erujnoc ot si kcirt ehT
).emit citardauq sekat esreveRwols ,emit raenil sekat :rewsnA(
)?ekat seod emit hcum woH( ?yhW .wols oot si siht ,tsriF .esoprup a rof nwo ym gnitirw m’I niaga tub ,ti sah ydaerla yrarbil dradnats ehT .tsil a esreveR
.rotalumucca eht rof retemarap artxe na sekat ti dna ,pool eht rof noitcnuf repleh a erujnoc ot si kcirt ehT
.etirw yllamron uoy rotalumucca-dna-pool eht ot resolc si noisrev sihT .tsil a gnimmus fo noisrev rehtonA
.noitcudni dneterp eht od ro emeht nommoc eht rof kool ,daetsnI .STNEDUTS TSAP ROF DEKROW REVEN .NOITIUTNI ESU TON OD .KNIHT TON OD :ecivdA
sh.dloFlleksaH ni
ldlof
— = (reversal of xt) ++ [x] ++ accum
— = (reversal of xt) ++ ([x] ++ accum)
— = g ([x] ++ accum) xt
— = g (x : accum) xt
g accum (x:xt) = g (x : accum) xt
g
by IH
g accum [] = accum
g accum (x:xs) = g (a function of accum and x) xs
mySumV2:
initial accum: 0
function: (+)
myReverse:
initial accum: []
function: (\a x -> x : a) = flip (:)
myFoldl op z xs = g z xs
where
g accum [] = accum
g accum (x:xt) = g (op accum x) xt
foldl
g foldl op
myFoldl op z [] = z
myFoldl op z (x:xs) = myFoldl op (op z x) xs
mySumFoldl xs = myFoldl (+) 0 xs
myReverseFoldl xs = myFoldl (flip (:)) [] xs
op z
z op
g
Table of Contents
.yfitnedi ot ysae era dna ,taht evah uoy ecnO ).noitcnuf repleh siht tnaw uoy taht gnidiced tsuj si eldruh tseggib eht spahrep rO( .retemarap rotalumucca artxe
na sekat taht
noitcnuf repleh eht gnirujnoc si eldruh tseggib eht taht si ereh sneppah tahW
.pleh t’ndid ,deirt I ? dna
rof evlos ot noitcudni dneterp esu uoy naC sh.dloFlleksaH ni
:
ot kcab .siht rof
dnapxe ew ylnommoc eroM sah yrarbil dradnats ehT
ekil skool taht
noitcnuf a :emeht nommoC sh.dloFlleksaH ni
:otni detcartsba si sihT