4 ms·
There are two major differences between OCaml and Haskell. First: Haskell is a pure functional language, meaning that side effects, IO, keeping state, all that
by andrepd 8y ago
There are two major differences between OCaml and Haskell.
First: Haskell is a pure functional language, meaning that side effects, IO, keeping state, all that must be dealt with inside the type system, which while undoubtly clean and powerful, it makes it often cumbersome to do things that should be quick and simple. OCaml, on the other hand, is pragmatic. It promotes and facilitates functional programming but it also let's you easily "drop down" (so to speak) to an imperative way to write code. You have mutable variables, imperative loops, you can perform side effects anywhere, etc. As a rule of thumb, you can write fully functional code, yet sometimes when the best way to write a certain thing is imperatively, you can do so with no hassle.
Second: Haskell is lazy, and OCaml is strict. This makes it much easier to reason about performance and makes for much more predictable code. At the same time, it has first-class support for laziness when you do need it, but you have to explicitly "opt in", so to speak. Haskell also lets you force strict evaluation, but you will find it's much easier to build laziness in a strict language than the other way around.
There are also other important differences (like typeclasses vs functors) but I feel these two are the biggest ones.
- k__ 8y agoYes, I think the lazy evaluation was big pain point.
- TuringTest 8y agoIn Haskell, you have to opt-in to side-effects (monadic do-blocks). In OCaml, you have to opt-in to lazy evaluation.
- cannabis_sam 8y agoIn (GHC) haskell you also have opt-in strictness (per module or per variable)
- the_duke 8y agoDo blocks don't allow side effects, they are just syntactic sugar that makes composing monadic operations easier. Haskell does have escape hatches with unsafePerformIO and IORef/STRef
- TuringTest 8y agoYeah, but monadic composing is what make I/O and other side effects stylistic and understandable.
- arunix 8y agoWhy does laziness hinder reasoning and predictability?
- atombender 8y agoLaziness disassociates the execution of a function from its call site. A classic novice mistake is to accidentally read a file after closing it -- something that can happen even if the "read" function is called before the "close" function in the code. It happens because the read function isn't actually called until its return value is used, and that can very well happen after the close call. So you have an implicit dependency graph of evaluation that doesn't exactly match the chain of function call. You may find that some slow code gets deferred until in the middle of some time-sensitive code, for example. You could also have something blow up that's completely unrelated to the calling code, again because of the laziness. The opposite of lazy is strict, and Haskell does let you "opt in" to strictness when you require it.
- n42 8y agoCheck out Purely Functional Data Structures by Chris Okasaki: https://www.cs.cmu.edu/~rwh/theses/okasaki.pdf https://www.cs.cmu.edu/~rwh/theses/okasaki.pdf