Hacker News new | ask | show | jobs
by itishappy 522 days ago
`map` aint so bad...

    map :: (a -> b) -> [a] -> [b]
I suppose an absolute beginner would need someone to explain that Haskell type signatures can be read by slicing at any of the top level arrows, so that becomes either:

> Given a function from `a` to `b`, return a function from a `list of as` to a `list of bs`.

or:

> Given a function from `a` to `b` and a `list of as`, return a `list of bs`.

I find the first to be the more intuitive one: it turns a normal function into a function that acts on lists.

Anecdotally, I've actually found `map` to be one of the most intuitive concepts in all of programming. It was only weird until I'd played around with it for about 10m, and since then I've yet to be surprised by it's behavior in any circumstance. (Although I suppose I haven't tried using it over tricky stuff like `Set`.)

`fmap` is admittedly a bit worse...

    fmap :: Functor f => (a -> b) -> f a -> f b
But having learned about `map` above, the two look awfully similar. Sure enough the same two definitions above still work fine if you replace `list` with this new weird `Functor` thing. Then you look up `Functor` you learn that it's just "a thing that you can map over" and the magic is mostly gone. Then you go to actually use the thing and find that in Haskell pretty much everything is a `Functor` that you can `fmap` over and it starts feeling magical again.
1 comments

You and I have a math part of our brain that appreciate the elegance from the algebraic structure.

I’m saying that thing you did where you start representing concepts by letters which can be populated by concrete objects is not a skill most people have.

Oh. Yeah that's a good point.