[Haskell-cafe] Ocaml for Haskellers tutorial
dagit at codersbase.com
Sat Apr 17 02:22:02 EDT 2010
On Fri, Apr 16, 2010 at 8:35 PM, C K Kashyap <ckkashyap at gmail.com> wrote:
> I am a little surprised by the "shortcomings" of Haskell mentioned in the
> I was under the impression that Haskell was closest to Nirvana on the
> usefulness vs safety graph.
> In the paper "Why FP matters" - Laziness is stated as one of the two key
> features of FP that allows conceptual modularity! If I understand right,
> Laziness is not a first class stuff in OCaml - is that not right?
That's right. You can simulate laziness in OCaml. OCaml compilers cannot
assume code is pure and lazy so many very useful optimizations are not
performed by the OCaml compiler. And some syntactic and stylist things are
not available. See for example Ganesh's comment about combinators earlier
in this thread.
> If I understand correctly - Not allowing side-effects allow for equational
> reasoning - which in turn allows you to "reason" about the program better.
> If I understand right - OCaml allows side effects right?
> Jeff, could you please expand on the tail recursion bit - what do you mean
> when you say, in OCaml, "one has to write tail recursively in OCaml"?
Tail recursive functions do not need to maintain a stack for their recursive
calls and the optimizer may replace them with a loop. In strict functional
languages such as OCaml this means that by writing your recursive functions
to be tail recursive you won't run out of stack space. Many recursive
functions can be transformed to be tail recursive by adding an accumulator
argument and building up the return value there.
In a lazy language if you write something to be tail recursive, then each
time the function recurses it will add to the accumulator. Because it's
tail recursive it typically also won't evaluate this accumulator. This
means that in memory you have to build up the unevaluated representation, or
thunk. This thunk keeps getting bigger and bigger until something demands
the value. This means that if you need to write a recursive function in
haskell which has an accumulator, you often want to make the function strict
in the accumulator. Meaning the strict function will evaluate the
accumulator as it goes.
One place where lazy accumulators is bad are the left folds. There is the
lazy foldl and the version which is strict in the accumulator, foldl'. Try
summing big lists of integers, let's use ghci and limit the heap to 1 meg:
ghci +RTS -M1M
Prelude> foldl (+) 0 [1..10000000]
Current maximum heap size is 6291456 bytes (6 MB);
use `+RTS -M<size>' to increase it.
ghci +RTS -M1M
Prelude> :m + Data.List
Prelude Data.List> foldl' (+) 0 [1..10000000]
Haskell lets us choose the evaluation strategy here. After programming in
both Haskell and Common Lisp for several years each I can tell you that
Haskell's way of dealing with evaluation strategies and letting the user
pick the desired one is easier to manage and deal with. I would expect
OCaml less nice than Haskell here as well.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe