Will reading a Haskell book help adopt a functional programming mindset?

I’m not trying to discourage learning Haskell, far from it and haskellbook is fantastic. But learning Haskell is completely different from learning lets say Java - especially if your brain has been “shaped” by Java. Maybe you’ll be fortunate and it will be easy peasy for you - but it may also take a long drawn out effort of constantly chipping away at it. I get that the whole “functional purity” thing has some definite appeal.

My argument is that there may be less taxing ways to get the “functional programming mindset”.

I seem to remember that Katie Miller used Haskell at Facebook but Facebook also uses OCaml in Flow and of course is working on ReasonML (and Jane Street uses OCaml).

Interested in ReasonML?

As for Haskell - remember to just keep at it.

2 Likes

Yes I agree Haskell is not language you will learn in 3 weeks like GO :slightly_smiling_face:
You will need to reserve some time on it.

But as I said there are nice people that will help you especially on slack channel.
https://fpchat-invite.herokuapp.com channel #haskellbook

Not really. Haskell focuses on the HKT’s and typeclasses and so forth mindset, which I find very limiting. HPT’s (like OCaml) can do all of the prior mentioned with a singular syntax while being far more powerful as well.

SICP and Okasaki are really good books, but for different things. There is also the set of MIT lectures given by Abelson and Sussman based on their book which are well worth the time, though they are not easy.

3 Likes

Ok I don’t know OCaml .So what functional concepts does have OCaml that is missing in Haskell?

HPT’s via its module system is the big one. In contrast Haskell focuses on HKT’s.

HKT’s are Higher Kinded Types, this is what lets you thread a type through a program by using the type arguments. This is convenient to use but is a massive detriment to compilation time.

HPT’s, which are Higher Polymorphic Types, can do everything HKT’s can, and more, though it tends to be a little bit more verbose (though there are incoming PR’s to shorten it as well, though it’s often short enough to not worry about anyway).

In short though, OCaml’s modules are more like records, but can have a variety of interfaces defined for a given module (think of it kind of like Rust’s interface system or using Java/C# with a single class implementing many interfaces), you can define a module and ‘expose’ it via a variety of typed interfaces for the module. Just like those, in OCaml modules are first-class, you can ‘pack up’ an entire module into a variable and unpack and use it as you wish. You can even generate new module instances at run time, so you could have a function return a different result, or do something different entirely, as long as it follows the defined interface (or new one returned out), all entirely type safe.

Now going beyond the ‘interface’ style is that you can actually ‘do work’ on the modules at compile time, which could of course do stuff, even generate new modules with new types and interfaces and all, it would be like generating new interfaces at compile time based on other types in Java/C#, of which you cannot do.

And other smaller things that either Haskell does not have or is only accessible via extensions (on say GHC) are things like Row-Typing (OCaml objects), GADT’s, a unified string type (lacking that is hell on Haskell at times…), among far more, all built in. As well as OCaml has an entire ‘macro’ / pipeline-transforming-system called PPX’s that allow for new code constructs to be defined, kind of like Macro’s in Elixir except more powerful, however they are not as easy to write as Elixir macro’s. ^.^

In general:

  • Haskell was designed by researchers to think about research problems, and it shows as it has 50 ways of doing anything with many inconsistencies.
  • OCaml started from a research lab that was backed by businesses to get ‘real work done’ (very much like Erlang) and it even has a way to break it’s purity (exclusively via Obj.magic) if absolutely necessary (of which it never really is to be honest unless you are doing assembly-level optimizations), it was designed to be ‘useful’ and it’s feature-set reflects that by being easy to use, easy to write, easy to read, fast code that is near C in speed, the fastest optimizing compiler of any language, etc…
3 Likes

Hi, thx for great explanation :wink:
I do not negate that OCaml can be more practical …

My goal to learn Haksell is to learn modern type system specially:

  • Algebraic Type System (ADT)
  • Functor
  • Monad
  • State Monad
  • Free Monad

Then I can use this knowledge in other languages like Scala, Java Script, F#

I assume that OCaml modules are specific to this language. Question is what I can learn from OCaml that I can use in other languages.
Any way from your description OCaml looks to be great language.

You can learn all those concepts (ADT, Functors, Monads) from OCaml as well.

ADTs are the basic building blocks of types there as well. Only the syntax differs in constructing them.

Functors and Monads and other such stuff is available in many other functional languages as well, but under some disguise, or not as explicit.

State Monads are just an abstraction. You can achieve basically the same with recursively passing the state around. f :: s -> a -> b is very similar to f :: a -> State s b, but in my opinion the former is easier to write and understand.

A “free” monad is as every other monad, but the way you implement it differs, its not a generalisation or specialisation of a Monad as State is, but its an abstraction that provides a monadic interface for your data types without you to having to think about monad laws and stuff (but you have to think about all the “free” stuff then, it trades one for the other…)

The only thing that might help you learning about Functors, Monads and all the other typeclasses in Haskell, is that their names are written out in Typeclasses and especially that there are virtually no escape hatches.

But to be honest. Once you understand that Functor and Monad are just typeclasses with a set of rules, its so easy to use them. In elixir parlance, they are just protocolls.

2 Likes

Something from lamdadays 2018

2 Likes