Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  Understanding Haskell fcn calls (black...@pro-ns.net)
   2. Re:  Understanding Haskell fcn calls (Daniel Fischer)


----------------------------------------------------------------------

Message: 1
Date: Wed, 16 Feb 2011 14:49:51 -0800
From: <black...@pro-ns.net>
Subject: [Haskell-beginners] Understanding Haskell fcn calls
To: <beginners@haskell.org>
Message-ID: <e7939d72386870be570ee6029c6b8...@iphouse.com>
Content-Type: text/plain; charset=UTF-8; format=flowed

When I learn a new language, I like to look at the assembly code for 
some simple fcn calls.  I tried GHC -S on the following program:

    add' x y = x + y

    main = do
       print $ add' 1 2


The result of  ghc -S add.hs  was 301 lines of assembly code. Adding 
an explicit type annotation for add' brought it down to 252 lines, and 
the code was still beyond my meager ability to take in assembly.

Is there a good reference that gives a simple explanation of how 
Haskell function calls work?  This seems crucial to understanding how 
to efficiently use the language, given what I've read about tail call 
optimization.

thanks
Lee Short




------------------------------

Message: 2
Date: Thu, 17 Feb 2011 01:09:57 +0100
From: Daniel Fischer <daniel.is.fisc...@googlemail.com>
Subject: Re: [Haskell-beginners] Understanding Haskell fcn calls
To: beginners@haskell.org
Cc: black...@pro-ns.net
Message-ID: <201102170109.57975.daniel.is.fisc...@googlemail.com>
Content-Type: text/plain;  charset="utf-8"

On Wednesday 16 February 2011 23:49:51, black...@pro-ns.net wrote:
> Is there a good reference that gives a simple explanation of how
> Haskell function calls work?

I don't know one.

> This seems crucial to understanding how
> to efficiently use the language,

No, fortunately it's not necessary. To use the language efficiently, you 
must understand (not necessarily completely, though) laziness, where that's 
a Good Thing? and where bad.

> given what I've read about tail call optimization.

Which is far less important in Haskell than in strict languages.
Due to laziness, a recursive (but not tail-recursive) function can deliver 
partial results before entering a recursive call, which often is better 
than tail-recursion.

Consider

map :: (a -> b) -> [a] -> [b]
map f (x:xs) = f x : map f xs
map _ [] = []

When you call map f on a nonempty list (x:xs), you immediately get the 
result, a cons cell with two children, one is the thunk (how to calculate f 
x), the other is the thunk (how to calculate map f xs).
Thus, when you consume the result sequentially, the computation can run in 
constant space, each list element can be garbage collected as soon as it is 
consumed, before the next element comes alive (of course, ordinarily the 
garbage collector will not run that frequently, so there'll be a handful of 
values lingering after being consumed).

With a tail-recursive function, the entire result has to be in memory at 
once, since it can only be returned after the computation is complete.

Tail-recursion is however good if no partial results are possible, as for 
the sum of a list of Ints (but then you need to make sure that the 
accumulator is sufficiently evaluated or you'll just create a huge thunk 
which eats your stack when it is finally evaluated).

As a rule of thumb, tail-recursion is for strict stuff, not for lazy 
things. There are lots of lazy things in Haskell, so tail-recursion plays a 
comparatively small role.



------------------------------

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 32, Issue 33
*****************************************

Reply via email to