Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Should I Haskell or OCaml?
23 points by catlover76 on Sept 16, 2023 | hide | past | favorite | 50 comments
I am growing dissatisfied with Python, and looking to learn a new language. I’m interested in functional programming, so I think this will be one of Haskell or OCaml.

If anyone can weigh in with some thoughts, it’d be appreciated. I don’t have particularly clear-cut requirements here, but here are some things I have been working on, as well as other thoughts I have been having about this choice, that may influence someone’s advice.

Although I am not motivated primarily by a dislike for Python (I like Python most of the time), here 2 things that genuinely bother me about it:

* Getting tired of the lack of real static typing. I suppose I could/should set up a MyPy daemon for everything I do that’s more than a simple script, or try Pyre? I’m not sure how much of a pain that would be.

In any event, it’s still kind of annoying for the language to not actually include this concept. The type annotations feel tacked on (because they are) and at the end of the day, it’s still possible to get runtime errors that should have been checked at compile-time.

* The import and PYTHONPATH stuff is crazy. In particular, I feel like I am constrained to structure projects in a way that makes sense. I’ve just found a way that works for Flask web applications and I am sticking with it because fiddling with the structure too much leads to import errors.

Maybe I would have more gripes if I were smarter. Things I do like about Python:

Standard library seems pretty good (?), especially in terms of DSA (I’ve been joking lately that solving Leetcode problems is where Python really shines)

The code tends to be readable (or at least, it is possible for someone to write very clean, very readable code).

The main things I have been working on lately have involved PDF/document processing in the context of web apps. I also have some designs to do some ML/NLP stuff in a domain/industry where I have domain expertise.

My current thoughts:

Haskell has a lot of learning materials, and stuff like HaskTorch exists for my ML/NLP interests. Primary downside is that it’s purely functional, which means it can be less flexible and makes it less likely it can replace Python as my go-to language. Of course, that might be a plus from a learning perspective, as it would force me to learn to accomplish things in a purely functional way.

OCaml is more flexible in terms of paradigm, which has pluses and minuses from a learning perspective. Seems more likely to be a practical choice.

I would not say employment prospects are top-of-mind here. I don't think either are widely-used enough for it to matter; C++ would be a better thing to add to my resume for a lot of the jobs I am interested in; and I am of the general opinion and belief that I should prioritize foremost what I enjoy and have genuine interest in, both for QOL and because money will then follow.



Up until a few days ago, I was also vacillating between learning OCaml or Haskell, before deciding on Haskell for reasons.

I surveyed many free and paid-for references to build my foundation, before picking John Whitington's "Haskell From the Very Beginning" (https://www.haskellfromtheverybeginning.com), because it "takes a no-prerequisites approach to teaching the basics of a modern general-purpose programming language".

I am also using Replit's "free-tier" as my Haskell learning environment. Replit let me jump right into the fun of Haskell, while postponing dealing with installation/configuration nuances until absolutely necessary. This is also letting me do learning any time I have a free moment from wherever I last left off on any computing device I have handy, including my tablets or smartphone.

So far, my learning experience has been a joy.

If you decide on OCaml, Whitington also wrote "OCaml From the Very Beginning" (http://ocaml-book.com), which available via free pdf or HTML thanks to the OCaml Software Foundation, and looks to be structured similar to his Haskell book.

Hope this helps.


Learn You a Haskell For Great Good! is also a really good resource:

https://learnyouahaskell.com/


Cool thanks


I would suggest neither in your position. I suggest trying a language like F#. F# started as with the idea of developing a language with OCaml like syntax and semantics for the .NET platform. Since then its evolved to being a mature, elegant and robust language. It can run on Windows and Linux.

Advantages over OCaml is I suspect a much larger footprint and the ability to tap into all of .NET (huge ecosystem). Plus your skills will be more portable than Haskell or OCaml.

Both Haskell and OCaml are exceptional languages but the ecosystem and market footprint is not great for either. Jane Street has done a lot to evangelize OCaml but it hasn't really moved the needle. Haskell is probably the most different being a lazy functional language. Definitely learn it but using it in real life will be harder.


> Advantages over OCaml is I suspect a much larger footprint and the ability to tap into all of .NET (huge ecosystem). Plus your skills will be more portable than Haskell or OCaml.

Although job prospects are not the primary factor, I would like to hear more about this. Is F# widely-used? Are there a bunch of F# jobs and not quite enough people to fill them?

Thanks

edit: also, if you can recommend any physical books on F#, I would appreciate it


If you're not so much interested in using the language you learn in a job, I would recommend learning a proof assistant like Coq (basically Ocaml on steroids) or Lean (newer toy originally by Microsoft). The functional programming is fully there and you can even formally prove things about your program. The type systems these languages use are the richest out there (enough to encode basically any mathematical statement as a type)


I've heard of those, but I don't think I quite know what they are for. Formally proving things about a program sounds cool, but what are some practical applications of that?

Thanks


For example, making "exhaustive tests" (as if you're able to check every single possible user input), eliminating a lot of security vulnerabilities, provably correct implementations of cryptographic algorithms (DJB is playing with Lean a lot right now and wrote a paper on what it's like), being able to reason better about the program, composing different programs while maintaining these advantages.

The "reason" why people ask "what's the use of that" is that they just assume that writing bug-free and correct software is impossible and not worth trying.


Oh cool


Perhaps at this point you have made up your mind, I haven't tried OCaml, but I can recommend you Haskell. I am not sure what approach you'd like to take by learning it, but typing code isn't enough, I mean, what's the point of knowing too much syntax? In the era of Massive Information Availability. I also don't know at what stage of your journey you are related to the functional programming paradigm but still I do have two related recommendations about it: FP101x (Provided by DelftX through edX [Now archived but still doable])[1] and the book Programming in Haskell by Graham Hutton.[2] Remember what Mr. Leslie Lamport says: "If people is trying to learn programming by being taught to code, well they are being taught writing by being taught how to type. And that doesn't make much sense." [3]

Python? You may be surprised to know one or two pending tricks about that programming language, take a look at Design of Computer Programs by Peter Norvig at Udacity [4]

Scala is a good option too, since you mentioned getting tired from lack of real statically typing. EPFL Extension School has a good one: Effective Programming in Scala (Also available for free at Coursera).

Hope that helps, and happy learning!

[1] https://www.edx.org/learn/computer-programming/delft-univers...

[2] http://www.cs.nott.ac.uk/~pszgmh/pih.html

[3] https://www.youtube.com/watch?v=rkZzg7Vowao

[4] https://www.udacity.com/course/design-of-computer-programs--...


Thanks man!

I figure I'll need to try all of them (OCaml, Haskell, and F#) lol, will do a bit of learning and then build the same small test project in each probably. Starting with Haskell.

Thanks for the thorough response, will check out those resources


F# might give you an experience closer to Python but that is still functional programming. This is not just the syntax, but also the community has a pragmatic bent. It also has the biggest ecosystem, since it can easily use any .NET library.

For ML, none of these are popular options. However, F# has bindings for Torch that are maintained by Microsoft.

All 3 are great choices though.


I think OCaml is more practical and actually has some parallels to Python. You may find these resources interesting:

- Library to manipulate PDF documents: https://github.com/johnwhitington/camlpdf

- Web framework: https://aantron.github.io/dream/

- Scientific computing library: https://ocaml.xyz/

Last, I wrote a couple of practical-focused OCaml guides:

- https://dev.to/yawaramin/practical-ocaml-314j

- https://dev.to/yawaramin/practical-ocaml-multicore-edition-3...


Awesome thanks!


You are going to do the same things in a new language whatever you were doing in Python. I understand you are working on web and data science domains.

So I would recommend you check out F# which is like OCaml but runs on the Common Language Runtime. While it is really good on web platform, you might want to investigate the state of data science domain in F#. There are some guides here[1] and here[2].

1. https://fsharp.org/guides/data-science/

2. https://github.com/fsprojects/awesome-fsharp#data-science


Oh yeah, I wouldn't mind learning F# either I suppose.

Not that this probably matters/holds any relevance, but I worked at a startup using C#, and I liked the language itself.


I was in a similar position using Python at work. At first I learnt Ocaml on the side, which was really fun, and taught me to work with types instead of avoiding thinking about them as I did with C++. Then I got a job where I got to write "simple" Haskell – picking up Haskell after Ocaml was no problem, mostly just felt like a change of syntax. It's been fun, and for the domains I've used it for (mainly combining data from various sources, analysing, predicting), a very good fit. Also used it for web stuff with IHP lately, which has also been a success.

The Haskell ecosystem is somewhat larger than Ocaml's (more support, users, learning materials, libraries). The difficulty with Haskell is mostly that it makes abstraction so easy that you can be tempted to make your code too abstract. But if you can manage to KISS, it can feel like a safer, cleaner, faster Python. Haskell doesn't require you to code in pure functions, but it will let you know through the types that a function is not pure / is "in IO", and it has lots of features to let you easily do things purely that you would otherwise do in IO. That may lead to more of your code being pure in the end. F#'s main advantage is access to the C# ecosystem, though at the cost of possibilities for null pointers when calling C# libraries / .NET API's.


My 2 cents.

I only played with haskell, but liked a lot the language. The only downside was the 'cabal' versus 'stack' build tools unclear status. The official haskell.org site recommends installing both but I had incompatibility issues at the time.


Good to know, esp about IHP

Thanks

> F#'s main advantage is access to the C# ecosystem, though at the cost of possibilities for null pointers when calling C# libraries / .NET API's.

Yeah this sounds like it would be really useful in a lot of situations


I'm learning PROLOG for the funsies. It's really easy for making things like expert systems, and it's reasonably performant, has lots of libraries, including native ones, good FFI binding.

Care to join?


Do you have learning resources you are using?


What are expert systems?


A class of old-school AIs: https://en.wikipedia.org/wiki/Expert_system

I made a small one that classified dinosaurs in school.


I know you said you want something with real static typing, but I think it would still be worth it too check/try out Elixir.

IMO, Elixir is the functional Python. The lack of types in Elixir feels very similar to current Python. Pattern matching and guards instead of type annotations. No compile time checking. Dialyzer instead of MyPy. But unlike Python, Elixir is actively working on adding static typing that will be checked at compile time.

The language tooling, documentation, and testing is miles ahead of Python.

The language is enjoyable, easy to understand and read. It's very simple syntax. (only 15 reserved words)

You can find a lot of similar libraries in Elixir.

Django -> Phoenix

SQLAlchemy -> Ecto

Jupyter -> Livebook

Polars/Pandas -> Explorer

Numpy -> Nx

PyTorch -> Axon

SKLearn -> Scholar

XGBoost -> EXGBoost

Transformers -> Bumblebee

Prefect/ETL -> Broadway and Flow

PyO3 -> Rustler

All of this and you also get the benefits of BEAM VM. Which makes Elixir faster and more scalable than Python in majority of cases.


Thanks

Yeah, yeah I was thinking about Elixir too, I like that it has a specialty (real-time, highly concurrent). It sounds like it also has a more developed ML/NLP ecosystem than I realized.


I wouldn't necessarily recommend Elixir simply due to not having static types. You should continue deliberating on whether to use OCaml or Haskell without throwing Elixir into the mix.


Haskell is purely functional, but if you make ample use of do-notation it's very easy to write in a familiar imperative(ish) style for day-to-day tasks. Most learning materials won't emphasize this but large production Haskell codebases that do a lot of IO usually end up looking a lot more familiar than you might expect.

If you have interest in both OCaml and Haskell, you might also consider just learning Rust which borrows aspects from both of them. Rust traits aren't quite as powerful as Haskell typeclasses and its module system is definitely more limited than OCaml's, but if you write it in a more functional style you'll get exposed to a lot of similar patterns.

A lot of things in Haskell never really clicked for me until I spent some time getting comfortable with Rust.


Yeah, I was considering Rust, and actually originally thought I'd be asking this question about the 3 of them.

I guess I figured I wanted to do a more academic language first, and that there may be a chance I'd have a practical use-case actually crop up for Rust at some point (in particular if I want/need to write/generate some WebAssembly, which may be the case for a project I may embark on).

Thanks for your input


A lot of commenters have already suggested F#, I'll just add this resource as a good way to dive in: https://fsharpforfunandprofit.com/


Really great resource.

Along with his book: "Domain Modeling Made Functional" by Scott Wlaschin


I was about to suggest either, but then I read:

> Primary downside is that it’s purely functional, which means it can be less flexible and makes it less likely it can replace Python as my go-to language.

Learn Haskell so that you can dispel this notion.


Haha, fair, thanks


Why aren't you using Pyre? Don't blame the tools when you're not utilizing them. I hate to state the obvious, but discontent is unlikely to be solved by either novelty or becoming a student novice again.

OCaml is not more flexible than Haskell. The primary differences are greedy vs. lazy evaluation.

Haskell is used in production in finance and military applications. It was previously used at Twitter and Facebook. Haskell lets you create heavily-abstracted, curried monads and define custom operators. Basically, every mathematician's dream-kitchen sink-swiss army knife-footgun. Code so readily esoteric than 5 people on the planet will be able to maintain it. ;)

OCaml is used in a smattering of niche MAANG backend services and in XenAPI and Coq.

Rust or Elixir+Erlang are more useful alternatives because they translate to job skills. If you want speed, resource efficiency, correctness, concurrency, and maintainability then Rust. If you don't care about correctness or speed, but want apparent simplicity and rapid development then use Go. ;)


> Why aren't you using Pyre?

Partly because I hadn't even heard of it until yesterday lol

> Don't blame the tools when you're not utilizing them. I hate to state the obvious, but discontent is unlikely to be solved by either novelty or becoming a student novice again.

True, but I think one also must learn new things, and it's normal to get bored of a language want to see what else is out there. Obviously, I will learn some new things about programming and software by learning FP, even if I never directly utilize one of the languages in a job.

> Rust or Elixir+Erlang are more useful alternatives because they translate to job skills. If you want speed, resource efficiency, correctness, concurrency, and maintainability then Rust. If you don't care about correctness or speed, but want apparent simplicity and rapid development then use Go. ;)

Yeah, as stated in one of my other comments, if I was building something for production, I think Go would be my default choice. But that's because it's practical; it's a boring and tedious language, in my admittedly minimal experience.


How did you select those two as your options?

I'm just a hobbyist that enjoys programming, and I eventually wanted to expand beyond python. I looked at Haskell and read Learn You a Haskell and did some Exercism exercises but never got anywhere close to being able to use it for real projects. Have been trying to learn about Lisp lately and feel like I've come to a similar dead end.

On the other hand, both Go and Rust have felt fulfilling and practical, with static typing and solid tooling, cross compilations, static binaries, and dependency management that is just a huge breath of fresh air coming from python.

The ML / data science scene is nowhere near as developed as in Python, and I still lean on jupyter/polars/PyTorch here, but I think the candle project[0] seems very interesting. Compiling whisper down to a single CUDA-leveraging binary for fast local transcription is pretty cool!

[0]: https://github.com/huggingface/candle


I chose them mostly based on pedigree, reputation, and vibe. For OCaml in particular, I have been listening to Jane Street's Signals & Threads podcast, and I figure that if these incredibly smart and accomplished people really like OCaml, it must be pretty cool and worth checking out.

Elixir was also something that seemed interesting. I like that it has a focus/specialty (real-time, highly concurrent systems), and its specialty is relevant to several of my interests.

> I looked at Haskell and read Learn You a Haskell and did some Exercism exercises but never got anywhere close to being able to use it for real projects. Have been trying to learn about Lisp lately and feel like I've come to a similar dead end.

Yeah, I did some Learn You a Haskell a while back and similarly felt it was too incoherent and disjointed. I intend to buy a physical book or two this time around.

> On the other hand, both Go and Rust have felt fulfilling and practical, with static typing and solid tooling, cross compilations, static binaries, and dependency management that is just a huge breath of fresh air coming from python.

If I was going to start a startup, or was tasked with building a new service at an existing company, Go would be my leading candidate. However, I view that choice as simply very practical; it's a reliable language that is in widespread production use, performs well, etc. But this is more about interest/fun. I haven't written much Go, but insofar as I have, I found it very tedious and somewhat awkward. And it's not a functional programming language, as far as I am aware.

Yeah, candle is pretty cool, and is one reason why Rust was also a candidate for me. Dropped it out of consideration because I basically am not motivated by any of the things that, to my understanding, motivate Rust, such as low memory footprint.


All totally fair! And thanks for your response.

I have to admit that I've really enjoyed rust, and I think it actually pairs well with python. But I'm sure you've heard plenty of rust evangelism, and as it wasn't one of your two candidates for this thread, I won't go any further.


I haven't had that much Rust evangelism, lol, though I've seen it on HN from time to time. I'd be curious how it feels to build the standard parts of a web app in Rust versus Go (API/routing, basic CRUD stuff, DB connections, etc).


FYI I'll mention it here. Haskell is the single worst introduction to functional programming anyone can try. It's absolutely terrible resources for learning it (Learn You a Haskell is a disaster of a "book"), and the language's documentation and on ramp are really poor.

If you want a statically typed functional language learn F#. And can easily leverage the .net ecosystem for easy productiity. Or if you want something that introduces the concepts that you can learn in a weekend learn Elm. It's front end only, it's very opinionated, but it's tiny and a great langauge to learn to think in FP.

If you want a non statically typed functional language learn Clojure. Not ideal on ramp, if has the java ecosystem, and great concepts. If you really don't want to do that learn a Scheme or Common Lisp although ecosystem isn't as large.

Once you've learned one (or two) of those, then start exploring things like Haskell. But before then it's a terrible waste of time, and almost guaranteed you'll be turned off from learning it before you feel productive in it or that you got your money's worth from your effort.


I didn't really like "Learn You a Haskell". "Haskell Programming from First Principles" or "Haskell from the Very Beginning" might be more accessible.


Oh ok, good to know, thanks

I'm surprised to see all the F# recommendations! Definitely on the radar now.


Reading about programming makes you good at reading about programming. If you want a "guided project" to learn from instead, I can recommend https://lhbg-book.link/


I think this is a good suggestion; I was pondering what simple thing I could build in OCaml, Haskell, and F# to compare them.


> wanted to expand beyond python. [...] Have been trying to learn about Lisp lately and feel like I've come to a similar dead end.

If you're already comfortable with Python, check out Hissp's tutorials (in the docs). It's a Lisp hosted on Python, so you can focus on the Lisp concepts without getting so bogged down by learning a new standard library at the same time.

https://github.com/glich/hissp


I loved learning Haskell and wish Haskell and its ecosystem long life and prosperity.

But I find it hard to choose Haskell for practical projects because there are so many options so many cool ways of doing things and there is always that latest better approach and I find myself into a permanent research mode where I learn a lot and get not much done.

Ymmv


Ah interesting. Yeah I think I'd be the same way lol


I went from Python to F# but now I think I should have gone to Rust instead. My reasoning has to do with making standalone executables and being able to call C libraries. That can be done in F# but is easier in other languages.


Haskell. It has a bigger ecosystem, although nowhere close to python’s


For a job - neither.

For personal interest - try both and see which one you like more.


Thanks lol, that's probably what will happen in the end, I suppose




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: