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.  How to add a "method" to a record (martin)
   2. Re:  How to add a "method" to a record (David McBride)
   3. Re:  How to add a "method" to a record (Julian Birch)
   4. Re:  How to add a "method" to a record (Christopher Allen)
   5. Re:  How to add a "method" to a record (Corentin Dupont)


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

Message: 1
Date: Wed, 10 Sep 2014 20:06:26 +0200
From: martin <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] How to add a "method" to a record
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-15

Hello all

if I have a record like

        data Foo pl = Foo {
                    label :: String,
                    payload :: pl
                }

how can I create a similar type where I can populate label so it is not a plain 
string, but a function which operates on
payload? Something like

        label (Foo pl) = show pl




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

Message: 2
Date: Wed, 10 Sep 2014 14:16:02 -0400
From: David McBride <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] How to add a "method" to a record
Message-ID:
        <can+tr42sbyvolxrsqzyfrufgjqtrhu6uhlpotcwoh97pvfm...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Is this what you are looking for?

data Foo pl = Foo {
  label :: pl -> String,
  payload :: pl
}

On Wed, Sep 10, 2014 at 2:06 PM, martin <[email protected]> wrote:

> Hello all
>
> if I have a record like
>
>         data Foo pl = Foo {
>                     label :: String,
>                     payload :: pl
>                 }
>
> how can I create a similar type where I can populate label so it is not a
> plain string, but a function which operates on
> payload? Something like
>
>         label (Foo pl) = show pl
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20140910/f29ce44d/attachment-0001.html>

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

Message: 3
Date: Wed, 10 Sep 2014 19:30:16 +0100
From: Julian Birch <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] How to add a "method" to a record
Message-ID:
        <cab0tuzbxqb7blirb2whbsk_fqqzs_ncmroipo2s88jo9b-v...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Bear in mind you can just create functions at the top level that operate on
your data structure.  You only need the function to be a member of the
record if the function itself changes, which is relatively rare.

Say you need a Foo and a Bar, and they both have labels, implemented in
different ways, then you can use a typeclass to achieve your goals.

On Wednesday, September 10, 2014, David McBride <[email protected]> wrote:

> Is this what you are looking for?
>
> data Foo pl = Foo {
>   label :: pl -> String,
>   payload :: pl
> }
>
> On Wed, Sep 10, 2014 at 2:06 PM, martin <[email protected]
> <javascript:_e(%7B%7D,'cvml','[email protected]');>> wrote:
>
>> Hello all
>>
>> if I have a record like
>>
>>         data Foo pl = Foo {
>>                     label :: String,
>>                     payload :: pl
>>                 }
>>
>> how can I create a similar type where I can populate label so it is not a
>> plain string, but a function which operates on
>> payload? Something like
>>
>>         label (Foo pl) = show pl
>>
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> <javascript:_e(%7B%7D,'cvml','[email protected]');>
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>
>

-- 
Sent from an iPhone, please excuse brevity and typos.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20140910/9592b852/attachment-0001.html>

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

Message: 4
Date: Wed, 10 Sep 2014 13:49:56 -0500
From: Christopher Allen <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] How to add a "method" to a record
Message-ID:
        <cadnndorskteodcmmjkxrnjbeh2+u_2_wfsae2ryapb5jikx...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

I'd agree with Julian Birch, except to say that you shouldn't make a
typeclass unless overloading is really needed and you can define laws on
the behavior thereof.

A fairly major (IMO) inconvenience with embedding functions in records that
have non-function data they operate on is that you won't have a Show
instance. Also, you'll be forced to extract the method and then apply it.
In doing so, to make things nicer you'll probably pull out an "extract and
apply" function. At that point, you're better off lifting the varying
implementations into top-level functions and making a wrapper that decides
which runs where based on the *data* in the record.

That is, including enough information in the record to decide which
function applied.

And this is without having broached whether or not you plan to be able to
serialize your data or not.


On Wed, Sep 10, 2014 at 1:30 PM, Julian Birch <[email protected]>
wrote:

> Bear in mind you can just create functions at the top level that operate
> on your data structure.  You only need the function to be a member of the
> record if the function itself changes, which is relatively rare.
>
> Say you need a Foo and a Bar, and they both have labels, implemented in
> different ways, then you can use a typeclass to achieve your goals.
>
>
> On Wednesday, September 10, 2014, David McBride <[email protected]> wrote:
>
>> Is this what you are looking for?
>>
>> data Foo pl = Foo {
>>   label :: pl -> String,
>>   payload :: pl
>> }
>>
>> On Wed, Sep 10, 2014 at 2:06 PM, martin <[email protected]> wrote:
>>
>>> Hello all
>>>
>>> if I have a record like
>>>
>>>         data Foo pl = Foo {
>>>                     label :: String,
>>>                     payload :: pl
>>>                 }
>>>
>>> how can I create a similar type where I can populate label so it is not
>>> a plain string, but a function which operates on
>>> payload? Something like
>>>
>>>         label (Foo pl) = show pl
>>>
>>>
>>> _______________________________________________
>>> Beginners mailing list
>>> [email protected]
>>> http://www.haskell.org/mailman/listinfo/beginners
>>>
>>
>>
>
> --
> Sent from an iPhone, please excuse brevity and typos.
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20140910/82e5daea/attachment-0001.html>

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

Message: 5
Date: Wed, 10 Sep 2014 20:50:18 +0200
From: Corentin Dupont <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] How to add a "method" to a record
Message-ID:
        <CAEyhvmopJGyv1B7TpScgKN9mZVVMXYu42hWDUfyS=vbskdo...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

If the field "label" can be deduced from "payload", I recommend not to
include it in your structure, because that would be redundant.

Here how you could write it:

data Foo pl = Foo { payload :: pl}

labelInt :: Foo Int -> String
labelInt (Foo a) = "Int payload:" ++ (show a)

labelString :: Foo String -> String
labelString (Foo a) = "String payload" ++ a

You are obliged to define two separate label function, because "Foo Int"
and "Foo String" are two completly separate types.
Alternatively, if you want only one label function, use a type sum and
pattern match on it:

data T = TS String | TI Int
data Foo2 = Foo2 { payload2 :: T}

label :: Foo2 -> String
label (Foo2 (TI a)) = "Int payload:" ++ (show a)
label (Foo2 (TS a)) = "String payload:" ++ a



On Wed, Sep 10, 2014 at 8:30 PM, Julian Birch <[email protected]>
wrote:

> Bear in mind you can just create functions at the top level that operate
> on your data structure.  You only need the function to be a member of the
> record if the function itself changes, which is relatively rare.
>
> Say you need a Foo and a Bar, and they both have labels, implemented in
> different ways, then you can use a typeclass to achieve your goals.
>
>
> On Wednesday, September 10, 2014, David McBride <[email protected]> wrote:
>
>> Is this what you are looking for?
>>
>> data Foo pl = Foo {
>>   label :: pl -> String,
>>   payload :: pl
>> }
>>
>> On Wed, Sep 10, 2014 at 2:06 PM, martin <[email protected]> wrote:
>>
>>> Hello all
>>>
>>> if I have a record like
>>>
>>>         data Foo pl = Foo {
>>>                     label :: String,
>>>                     payload :: pl
>>>                 }
>>>
>>> how can I create a similar type where I can populate label so it is not
>>> a plain string, but a function which operates on
>>> payload? Something like
>>>
>>>         label (Foo pl) = show pl
>>>
>>>
>>> _______________________________________________
>>> Beginners mailing list
>>> [email protected]
>>> http://www.haskell.org/mailman/listinfo/beginners
>>>
>>
>>
>
> --
> Sent from an iPhone, please excuse brevity and typos.
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20140910/28fc1a81/attachment.html>

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

Subject: Digest Footer

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


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

End of Beginners Digest, Vol 75, Issue 8
****************************************

Reply via email to