Re:Introduction to Functional Programming in OCaml
by arunix on Thursday August 23, 2018 @12:55PM
(#17828763)
Why does laziness hinder reasoning and predictability?
Re:Introduction to Functional Programming in OCaml
by atombender on Thursday August 23, 2018 @06:03PM
(#17831028)
Laziness 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.
Re:Introduction to Functional Programming in OCaml
by n42 on Thursday August 23, 2018 @06:14PM
(#17831093)
Check out Purely Functional Data Structures by Chris Okasaki: https://www.cs.cmu.edu/~rwh/theses/okasaki.pdf
Re:Introduction to Functional Programming in OCaml
by TuringTest on Thursday August 23, 2018 @10:57AM
(#17827762)
In Haskell, you have to opt-in to side-effects (monadic do-blocks).
In OCaml, you have to opt-in to lazy evaluation.
Re:Introduction to Functional Programming in OCaml
by the_duke on Thursday August 23, 2018 @01:43PM
(#17829190)
Do 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
Re:Introduction to Functional Programming in OCaml
by TuringTest on Friday August 24, 2018 @05:20AM
(#17833761)
Yeah, but monadic composing is what make I/O and other side effects stylistic and understandable.
Re:Introduction to Functional Programming in OCaml
by cannabis_sam on Thursday August 23, 2018 @11:54AM
(#17828290)
In (GHC) haskell you also have opt-in strictness (per module or per variable)
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.