Send Beginners mailing list submissions to
        [email protected]

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
        [email protected]

You can reach the person managing the list at
        [email protected]

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


Today's Topics:

   1. Re:  Here's why functions should return functions (Jay Sulzberger)
   2. Re:  Here's why functions should return functions
      (Carlos J. G. Duarte)
   3. Re:  Here's why functions should return functions
      (Ertugrul S?ylemez)


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

Message: 1
Date: Mon, 30 Jul 2012 15:48:31 -0400 (EDT)
From: Jay Sulzberger <[email protected]>
Subject: Re: [Haskell-beginners] Here's why functions should return
        functions
To: [email protected]
Message-ID: <[email protected]>
Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed



On Mon, 30 Jul 2012, Ertugrul S??ylemez <[email protected]> wrote:

> Jay Sulzberger <[email protected]> wrote:
>
>> There is, in most sub-systems of mathematics, whether like recent type
>> theory or not, a general function, let us call it mcf which in Scheme
>> notation may be defined by executing
>>
>> (define mcf
>>    (lambda (a)
>>      (lambda (x) a)))
>>
>> Now in Haskell I know that one, perhaps the, parallel definition must
>> result in a polymorphic function.
>
> First let's ensure that we are talking about the same function.  I'm
> reading "mcf" as "make constant function".  From what I read the Haskell
> equivalent would be this function:
>
>    const a _ = a
>
> It may make it not fully polymorphic, but if you don't provide a type
> signature, then the following fully polymorphic type will be inferred:
>
>    const :: a -> b -> a

This is good.

>
>
>> What is this definition?
>
> Well, this is the constant function, though with slightly unusual (but
> sensible) semantics for Scheme.  Because Scheme syntax requires explicit
> currying the name "make constant function" would also be sensible.  It
> is because of the lack of side effects that we call the function simply
> 'const' in Haskell.  Otherwise most functions would be prefixed with
> "make".

Ah, yes.  Certainly all functions with more than one input.

>
>
>> What implicit constraints are on a?
>
> None.

I am encouraged by this.

>
>
>> Does "lazy vs eager" come in here?
>
> Yes.  Even though you have written a curried version of 'const' there,
> Scheme is still a strict language, which means that the result of the
> inner lambda will depend on its argument 'x'.  This means:
>
>    -- Haskell:
>    const a ??? = a
>
>    -- in other words:
>    loop = loop  -- an infinite loop
>    const a loop = a
>
>    ; Scheme:
>    ((mcf a) ???) = ???
>
>    (define (loop) (loop))  ; an infinite loop
>    ((mcf a) (loop)) = (loop)

Yes, I see, I think.

I think, if we were doing a finer analysis, we might write

((mcf a) (loop)) ~> [(mcf a) waiting] (loop)

where [ waiting] indicates that when loop finishes executing, and
returns, ah, something, perhaps nothing, the constant function
(mcf a) will accept that returned thing.  (To be clear: above
line is not Scheme nor indeed is it a phrase of any standard
programming system.)

We here avoid mentioning (\omega + 1) ;)

>
> This is the semantic difference.  To relate this to "lazy vs. eager"
> it's important to understand how a nonstrict language like Haskell is
> usually evaluated:  Lazy evaluation will defer the evaluation of the
> inner lambda's argument (which is an unnamed '_' here) until it is
> required.  Since it is never required it is never evaluated and the
> unevaluated thunk is garbage-collected immediately, unless it is used
> elsewhere.
>
> A strict language like Scheme is usually evaluated eagerly.  This means
> that the inner lambda is not entered, until the argument is fully
> evaluated.

Yes.

>
>
>> Are there options to ghc which might modify how Haskell handles the
>> definition?
>
> There are optimization flags, which could change the performance of the
> definition, but a proper Haskell compiler like GHC must ensure that
> semantics are not changed.
>
>
>> Of course, my questions are too many and I hope just for some
>> indications of the first things a beginner should study.
>
> No worries.  I'm happy to help. =)
>
>
> Greets,
> Ertugrul

Thanks, Ertugrul!

oo--JS.



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

Message: 2
Date: Mon, 30 Jul 2012 22:30:48 +0100
From: "Carlos J. G. Duarte" <[email protected]>
Subject: Re: [Haskell-beginners] Here's why functions should return
        functions
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

So, what is the usefulness of that "const" function anyway? (go easy on 
me, I'm just beginning on this)

On 07/30/12 20:48, Jay Sulzberger wrote:
>
>
> On Mon, 30 Jul 2012, Ertugrul S?ylemez <[email protected]> wrote:
>
(...)



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

Message: 3
Date: Tue, 31 Jul 2012 03:56:12 +0200
From: Ertugrul S?ylemez <[email protected]>
Subject: Re: [Haskell-beginners] Here's why functions should return
        functions
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"

"Carlos J. G. Duarte" <[email protected]> wrote:

> So, what is the usefulness of that "const" function anyway? (go easy
> on me, I'm just beginning on this)

There are many functions in Haskell, which you wouldn't define in most
other languages, including:

    const :: a -> b -> a
    flip  :: (a -> b -> c) -> (b -> a -> c)
    id    :: a -> a

They become useful when used as arguments to combinators.  Some
examples:

    maybe 8 (const 16)
    foldl' (flip (-)) 0
    id &&& length

In use:

    maybe 8 (const 16) (Just 3) = 16 = const 16 3
    maybe 8 (const 16) Nothing  = 8

    foldl' (flip (-)) 0 [1,2,3]
        = flip (-) (flip (-) (flip (-) 0 1) 2) 3
        = (-) 3 ((-) 2 ((-) 1 0))
        = 3 - (2 - (1 - 0))

    map (id &&& length) ["abc", "de", "f"]
        = [("abc", 3),
           ("de", 2),
           ("f", 1)]


Greets,
Ertugrul

-- 
Not to be or to be and (not to be or to be and (not to be or to be and
(not to be or to be and ... that is the list monad.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20120731/c4de64e8/attachment-0001.pgp>

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

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


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

Reply via email to