4 ms·
Hey there! I'm the post's author. If you are stumbling on a specific point, let me know...maybe I can improve this post.
by egonschiele 13y ago
Hey there! I'm the post's author. If you are stumbling on a specific point, let me know...maybe I can improve this post.
- ww520 13y agoI think one of the problems of most Haskell tutorials for beginners is assuming well known facts or knowledge in Haskell is also known to the readers, which is a pretty wrong assumption. Haskell has its own set of vocabulary that first timers would have difficult to grasp. A simple example in the blog, it talks about Just 2 or Just a but don't explain what Just is. Also 'a' as a parameter for type is not explained. Leaving the readers to wonder whether a is argument to a function or it is a value since Just 2 and Just a are mentioned in the same section. Also a lot of tutorials are afraid to draw parallel concepts from other languages that would be familiar with the readers, instead preferring to invent abstract definition to build things up. Most people learn by relating and inferring from familiar concepts. E.g. Functor is just like template in C++ for constructing type. As a template it has placeholders to let the programmer to fill in to build a concrete type. These placeholders are the parameters to the functor. The programmer supplies a type as the parameter to the functor to construct a concrete type. E.g. "List a" is a functor which is called List and its parameter is a. "List int" would build a concrete type for values of list of int. Maybe and Just are not a good introduction functor for beginners. Few other languages have a similar concept and people just roll their eyes when they learn what it is since it's not as useful in general. List is a better introduction to functor. Most programmers have work with List and know what it is and its problems relating to its element type. Seeing "List int" people immediately know it's defining a list of int, and they know right the way what functor is and what it is for. Doing a couple more examples of List float, List string, and people will see why functor is very useful. May be adding Tree or some other functor operator like Eq or Show to how to have generic behaviors applying to any types.
- danieldk 13y agoE.g. Functor is just like template in C++ for constructing type. No, a type variable is! For instance, if we have a data type: data MyBox a = MyBox a a is the type variable, and is pretty much the same thing as a C++ class or struct template that stores a (const) value: template <class T> struct MyBox { MyBox(T const &nv) : v(nv) {} T const v; }; This has nothing to do with functors (yet). MyBox has a so-called kind of MyBox :: * -> * meaning that MyBox takes a type (namely a) and returns a type. For instance, MyBox Int has the kind: MyBox Int :: * Now, a Functor is any data type of kind * -> * for which we can implement the function fmap fmap :: (a -> b) -> f a -> f b Such that: fmap id = id fmap (p . q) = (fmap p) . (fmap q) hold. We can implement this function for MyBox and show that the laws hold (left as an exercise to the reader): instance Functor MyBox where fmap f (MyBox v) = MyBox (f v) In other words, fmap applies a function within a context. And the fact that a particular context (such as MyBox) is a Functor states that the functor laws hold.
- egonschiele 13y agoYup, this post assumes some knowledge of Haskell. Writing a post about monads that assumes no knowledge of Haskell is like writing a post about polymorphism that assumes no knowledge of OO :)