Slide 21
Slide 21 text
ℎ𝑒𝑎𝑑𝑜𝑟 99 𝑖𝑛𝑓𝑖𝑛𝑖𝑡𝑦
= { definition of ℎ𝑒𝑎𝑑𝑜𝑟 }
𝑓𝑜𝑙𝑑𝑟 const 99 𝑖𝑛𝑓𝑖𝑛𝑖𝑡𝑦
= { definition of 𝑖𝑛𝑓𝑖𝑛𝑖𝑡𝑦 }
𝑓𝑜𝑙𝑑𝑟 const 99 (1 : 𝑖𝑛𝑓𝑖𝑛𝑖𝑡𝑦 )
= { definition of 𝑓𝑜𝑙𝑑𝑟 }
𝑐𝑜𝑛𝑠𝑡 1 (𝑓𝑜𝑙𝑑𝑟 𝑐𝑜𝑛𝑠𝑡 99 𝑖𝑛𝑓𝑖𝑛𝑖𝑡𝑦 )
= { definition of 𝑐𝑜𝑛𝑠𝑡 }
1
const :: 𝛼 → 𝛽 → 𝛼
const x y = x
𝑓𝑜𝑙𝑑𝑟 ∷ 𝛼 → 𝛽 → 𝛽 → 𝛽 → 𝛼 → 𝛽
𝑓𝑜𝑙𝑑𝑟 𝑓 𝑒 = 𝑒
𝑓𝑜𝑙𝑑𝑟 𝑓 𝑒 𝑥: 𝑥𝑠 = 𝑓 𝑥 𝑓𝑜𝑙𝑑𝑟 𝑓 𝑒 𝑥𝑠
ℎ𝑒𝑎𝑑𝑜𝑟 ∷ 𝛼 → 𝛼 → 𝛼
ℎ𝑒𝑎𝑑𝑜𝑟 𝑎 = 𝑓𝑜𝑙𝑑𝑟 𝑐𝑜𝑛𝑠𝑡 𝑎
𝑖𝑛𝑓𝑖𝑛𝑖𝑡𝑦 :: [Integer ]
𝑖𝑛𝑓𝑖𝑛𝑖𝑡𝑦 = 1 ∶ 𝑖𝑛𝑓𝑖𝑛𝑖𝑡𝑦
λ> infinity = 1 : infinity
λ> const x y = x
λ> heador a = foldr const a
λ> heador 99 (1 : undefined)
1
λ> heador 99 infinity
1
λ> const 99 undefined
99
$ infinity
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 etc, etc
Lazy evaluation causes
just enough of 𝑖𝑛𝑓𝑖𝑛𝑖𝑡𝑦
to be valuated to allow
𝑓𝑜𝑙𝑑𝑟 to be invoked.
Neither 𝑐𝑜𝑛𝑠𝑡 nor ℎ𝑒𝑎𝑑𝑜𝑟 are
strict in their second argument.
@philip_schwarz