where clause

Russel Winder russel at russel.org.uk
Mon Mar 7 00:26:09 PST 2011


On Sun, 2011-03-06 at 20:23 -0500, bearophile wrote:
[ . . . ]

I wonder if you may have misunderstood the reason for the where clause
in functional languages such as Haskell and ML (usually OCaml).  In
these languages the body of a function must be a single value-returning
expression.  This means there has to be a separate clause for all the
declarations of the single-assignment variables -- caveat the use of
monads

> do {
>     auto r = map!(sqr)(items);
> } where {
>     int sqr(int x) pure nothrow {
>         return x * x;
>     }
> }

What's wrong with:

        {
          auto sqr = function int ( int ) { return x * x ; } ;
          auto r = map ! ( sqr ) ( items ) ;
        }

Seems idiomatic and straightforward with less noise?

> In D you are able to simplify a complex function moving some of its
> code into smaller functions that you are allowed to put inside the
> original complex function (they are allowed to be pure and static too,
> if you want safety and more performance), this replaces some of the
> Haskell purposes of "where", but not all of them.

In Python, line 5 of the PEP-20 "The Zen of Python" reads:

        Flat is better than nested.

there are good reasons for this.  Of course lines 8 and 9 are:

        Special cases aren't special enough to break the rules.
        Although practicality beats purity.

so there are times when you do nest functions -- basically to make
closures very analogously to the way you seem to have to in D.

> Haskell programmers don't criticize this where syntax (as they do with
> infix function syntax I've shown before).

Because they have no choice.

PS  I have begun to dislike languages that use semi-colon as a statement
terminator!  (It took me 4 attempts to get my two line example to
compile :-((

-- 
Russel.
=============================================================================
Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder at ekiga.net
41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel at russel.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20110307/6e0d9bc7/attachment.pgp>


More information about the Digitalmars-d mailing list