<div dir="ltr">I see this on the <a href="https://wiki.haskell.org/99_questions/Solutions/2">Haskell 99 questions</a> which will return the second-from-last element of a list<div><br></div><div>lastbut1 :: Foldable f => f a -> a<br>lastbut1 = fst . foldl (\(a,b) x -> (b,x)) (err1,err2)<br>  where<br>    err1 = error "lastbut1: Empty list"<br>    err2 = error "lastbut1: Singleton"<br></div><div><br></div><div>I understand how the code works, but not the significance of the type declaration. That looks like a type class. It works without it, I believe. Why have we used Foldable type class?</div><div><br></div><div>Likewise with this</div><div><br></div><div>lastbut1safe :: Foldable f => f a -> Maybe a<br>lastbut1safe = fst . foldl (\(a,b) x -> (b,Just x)) (Nothing,Nothing)<br></div><div><br></div><div>What's happening with the type definition?</div><div><br></div><div>LB</div></div>