Was looking at some problems to improve my knwoledge on foldable and functors but cannot quite seem to get my head around how to create the toList function using foldable and a functor
This is what i have so far was unsure if i would need to create my own instance for foldable here
I want to create toList explicitly using the type definition below
instance Foldable [] where
fold = undefined
toList :: (Functor c, Foldable c) => c a -> [a]
toList f = undefined
Any help and explanation to what i have to do is appreciated
To write
toList, you actually only need theFoldableconstraint. In fact, you only needfoldr. The key insight is the following law. Whenxsis a list,But the type of
foldr (:) []iswhich means it turns any
Foldableinto a list. And sure enough, this is an acceptable definition oftoList: