Given the above functions, I'm confused why let x = sumOfSquares . returnGreatest2 returnsbut sumOfSquares $ returnGreatest2 3 5 7 does the right thing. Since the type coming out of returnGreatest2 is the same as the type sumOfSquares expects, I would think I'd be able to compose them.

An exercise in Haskell from First Principles says to implement filter using foldr and this is what I came up with but it feels and looks clunky. Is there a more natural way to implement it with a foldr?

When I call safeHead with the argument (4:5:3:[]:[]) the pattern only evaluates the argument to see if either it is empty or if it has a head. So despite the nonsensical bit "[]:[]" it should not throw an error since this part is not even evaluated.

(Note: I'm phrasing the question using Haskell terminology; answers are welcome to use the same terminology and/or the mathematical language of category theory, including proper mathematical definitions and axioms where I speak of functor and monad laws.)