anchpop / wise_mans_haskell

Free book for learning Haskell
https://andre.tips/wmh/
48 stars 5 forks source link

Feedback on exercise to implement foldl1 #14

Open wongjoel opened 5 years ago

wongjoel commented 5 years ago

Summary

I found the exercise of implementing foldl1' (Functions First Class) has a bit of a trap for the unwary. The function that the reader is expected to produce has the type foldl1' :: (a -> a -> a) -> [a] -> a, but the type of the built-in foldl1 has the type foldl1 :: Foldable t => (a -> a -> a) -> t a -> a. My suggestion is to perhaps put a sidenote to clarify the expectation of the exercise.

User Experience

I initially started out by copying the type signature for foldl1 into my implementation of foldl1'. Looking at hint 2, I started with the lines:

foldl1' :: Foldable t => (a -> a -> a) -> t a -> a
foldl1' f [x] = x

but I then got confused because these two lines would not compile, because the actual type of the expression didn't match the expected type of the expression. I eventually decided to remove the explicit type signature, but I kept thinking that I had missed something until I checked the answer.

It was a good learning experience, if a bit of a frustrating one. But I thought that this might be a good spot for a sidenote, or perhaps a rewording of the exercise.

anchpop commented 5 years ago

Added a note to hopefully address that, thanks!

On Mon, Nov 26, 2018 at 6:35 AM Joel Wong notifications@github.com wrote:

Summary

I found the exercise of implementing foldl1' has a bit of a trap for the unwary. The function that the reader is expected to produce has the type foldl1 :: (a -> a -> a) -> [a] -> a, but the type of the built-in foldl1 has the type foldl1 :: Foldable t => (a -> a -> a) -> t a -> a. My suggestion is to perhaps put a sidenote to clarify the expectation of the exercise. User Experience

I initially started out by copying the type signature for foldl1 into my implementation of foldl1'. Looking at hint 2, I started with the lines:

foldl1' :: Foldable t => (a -> a -> a) -> t a -> a foldl1' f [x] = x

but I then got confused because these two lines would not compile, because the actual type of the expression didn't match the expected type of the expression. I eventually decided to remove the explicit type signature, but I kept thinking that I had missed something until I checked the answer.

It was a good learning experience, if a bit of a frustrating one. But I thought that this might be a good spot for a sidenote, or perhaps a rewording of the exercise.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/anchpop/wise_mans_haskell/issues/14, or mute the thread https://github.com/notifications/unsubscribe-auth/ADigR6JOS8KUGwJpLZ55GG7jrSIutdWUks5uy9INgaJpZM4Yy2Vc .