Skip to content

Comment on Haskell for Beginners

Comments

I'll add my two cents. Over the past year or so I went from being completely overwhelmed by Haskell to feeling pretty comfortable reading / writing Haskell, even enough to make my first PR to a Haskell package! Here's what worked for me:

* I went through LYAH, but at a certain point I felt like it wasn't doing me any favors by sweeping some of the inherent complexity under the rug. So I started listening to talks by SPJ, Wadler, et al. and reading the papers they wrote in the 1990s-2000s. See for instance the SPJ talk on Typeclasses [1], the Beautiful Concurrency paper [2], and the one about Monadic Parser Combinators [3].

* Don't just read stuff, run it in GHCI, or better, learn how to set up a minimal project with Cabal/Stack.

* I spent a lot of time reading about type theory and category theory. This isn't strictly necessarily, but it gives me context and perspective on the language.

* Haskell Language Server integration with VS Code was a lifesaver. Being able to hover over an expression and see the inferred type makes it so much easier to reason about Haskell code as a beginner.

* To understand monads, it was most helpful to see how do-notation and the >>= and >> operations desugar into a sequence of lambda abstractions. In general, I tried to find as many different examples of monads as possible. Seeing monad comprehension syntax really helped as well.

* After a while, I could read Haskell, but felt absolutely lost when trying to write it myself. So, I started working on a small cli that forced me to learn stuff like: how to manage a monad transformer stack, how to make http requests, how to parse and serialize json, how to write to a database, etc.. It took months and months, and I would often get stuck and have to go and spend a few weeks learning something else. But I always had a project to come back to and apply what I learned.

[1] https://www.youtube.com/watch?v=tqx9_MQbQ_c

[2] https://www.microsoft.com/en-us/research/wp-content/uploads/...

[3] https://www.cs.nott.ac.uk/~pszgmh/monparsing.pdf

The main reason to learn category theory for Haskell is to learn how much of the Haskell meme complex is wrong. Otherwise, it won't help the typical newcomer. I think it's important to explain that Hask isn't a category, that Haskell's type system is unsound, that Haskell doesn't have sum types, etc.

There are powerful applications of category theory to other parts of computer science. If one keeps in mind that Haskell is Just Another Programming Language, then one can apply category theory more generally and fully.

Agreed. Let me drop some references about the relationship between Haskell and Category Theory that really helped me untangle these concepts, in case it might help others.

[1] Haskell/Category Theory https://en.wikibooks.org/wiki/Haskell/Category_theory#cite_n...

[2] Bauer 2016, "Hask is Not a Category" http://math.andrej.com/2016/08/06/hask-is-not-a-category/

[3] A reddit comment in response to #2, https://www.reddit.com/r/haskell/comments/4wk0gs/hask_is_not...

[4] StackOverflow, "Is Hask even a Category?" https://stackoverflow.com/questions/48485660/is-hask-even-a-...

Haskell doesn't have sum types

Say what?..

I think it's because `Either a b` contains all of `undefined`, `Left undefined` and `Right undefined`. In other words, a technicality.

And this is not even the bottom of Haskell's unsoundness.

Don't just read stuff, run it in GHCI

Newcomers should absolutely use ghci commands like :t :type https://downloads.haskell.org/ghc/latest/docs/html/users_gui... to tinker and experiment. Also :info and :instances https://downloads.haskell.org/ghc/latest/docs/html/users_gui... once they start learning about typeclasses.

Typed holes can also be really helpful https://downloads.haskell.org/ghc/latest/docs/html/users_gui...

forced me to learn stuff like: how to manage a monad transformer stack, how to make http requests, how to parse and serialize json, how to write to a database, etc..

I am currently just before this stage. I can use Haskell for stuff like projecteuler.net problems but I haven't written a single useful, real-world program without copy-pasting some code.

I tried doing something with http requests to an API for stock trading but I failed to understand how to make a request. There are a few packages but they all have their flaws (e.g. no https) and aren't really developed that much.

My question is: did you write all these things with mostly only the Prelude of Haskell or did you use packages for all those things you listed (http requests, json data, databases etc.)?

I'd love some pointers in the right direction for http requests as well as maybe ideas for initial real-world project ideas that are exciting (I don't care for the standard ones you find online).

Yeah, the goal was (rather, is---I'm still working on it!) to learn more about real-world Haskell by actually learning how to use packages and forcing myself to read the Hackage docs, so for this project I pretty liberally import a new library whenever I need something.

The "req" package is very intuitive to use for requests, and plays nicely with "aeson" for parsing JSON. I'm also using "persistent" to manage a sqlite database.

For project ideas, try to scratch a personal itch. If you're solving a problem (or a daily frustration) that's important to you, you'll be more motivated to follow through! For me, I had been using the beets (https://beets.io/) command line tool to manage tags for my music library, but it isn't really optimized for my use case, and I thought I could do better. So I decided to write a cli [1] with Haskell to tag all my local music files with metadata from Discogs/MusicBrainz, modeled after the beets tagging loop. I'm also working on a more advanced tagging system that uses Datalog inference rules, inspired by a blog post [2].

[1] https://github.com/benrbray/borscht-hs [2] https://dodisturb.me/posts/2018-12-25-The-Essence-of-Datalog...

Try using wreq[0] instead. Here's also a post[1] which describes someone else facing the same pains earlier that you're currently having. For anything to do with JSON, you need Aeson[2]. For working with databases, Persistent[3] and Esqueleto[4] (they're designed to work together) work great.

[0]: https://hackage.haskell.org/package/wreq

[1]: https://bitemyapp.com/blog/haskell-is-not-trivial-not-unfair...

[2]: https://artyom.me/aeson

[3]: https://hackage.haskell.org/package/persistent

[4]: https://hackage.haskell.org/package/esqueleto

Persistent is truly amazing. I don't think I've ever had such a smooth experience working with databases in any other language.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.