Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/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:  How to nest arbitrary things (Magnus Therning)
   2. Re:  How to nest arbitrary things (Imants Cekusins)
   3. Re:  How to nest arbitrary things (Frerich Raabe)
   4. Re:  How to nest arbitrary things (Kim-Ee Yeoh)
   5. Re:  How to nest arbitrary things (Imants Cekusins)
   6. Re:  How to nest arbitrary things (David McBride)
   7. Re:  How to nest arbitrary things (Frerich Raabe)
   8. Re:  How to nest arbitrary things (martin)


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

Message: 1
Date: Mon, 21 Dec 2015 14:33:59 +0100
From: Magnus Therning <[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 nest arbitrary things
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"


martin writes:

> Hello all,
>
> I was trying to model things which can contain other things. This is
> easy as long as containers and the contained items all can be
> described in the same fashion. However when I want to model - say -
>
>       trucks
>       containing boxes
>               containing parcels
>                       containing cans
>
> and trucks, boxes, parcels and cans are not of the same type, then
> this nested thing will become a type in its own right and it will be
> of a different type than trucks containing cans (which are not wrappen
> in parcels ...)
>
> As long as I can squeeze trucks, boxes ... into one type, possibly by
> using a "container_type" attribute, there is no problem. Is this the
> only way to do this? Is there an idiom?

Well, you can always make Truck a bit generic:

    data Truck a = Truck [a]

Then you have have a truck of boxes (`Truck Box`) or a truck of cans
(`Truck Can`).

But maybe that's not really your question?

/M

--
Magnus Therning              OpenPGP: 0x927912051716CE39
email: [email protected]   jabber: [email protected]
twitter: magthe               http://therning.org/magnus

Any sufficiently advanced technology is indistinguishable from a rigged
demo.
     -- Andy Finkel
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 800 bytes
Desc: not available
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20151221/0b7cc29b/attachment-0001.sig>

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

Message: 2
Date: Mon, 21 Dec 2015 14:51:08 +0100
From: Imants Cekusins <[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 nest arbitrary things
Message-ID:
        <CAP1qinZYgYWV9J01hieceWGNkYptYPfdfmFDg0=t9ndwblr...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

>  trucks
        containing boxes
                containing parcels
                        containing cans


.. or try this type:

data Item a = Truck a [Item] | Box a [Item] | Parcel a [Item] | Can a [Item]

"a" here is a property you may use to identify each Item e.g. String


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

Message: 3
Date: Mon, 21 Dec 2015 14:54:32 +0100
From: Frerich Raabe <[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 nest arbitrary things
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII; format=flowed

On 2015-12-21 14:51, Imants Cekusins wrote:
>>  trucks
>         containing boxes
>                 containing parcels
>                         containing cans
> 
> 
> .. or try this type:
> 
> data Item a = Truck a [Item] | Box a [Item] | Parcel a [Item] | Can a [Item]

I guess you'd need that type if you want to be able to express

   
http://i.telegraph.co.uk/multimedia/archive/01845/truck-on-truck-on-_1845173i.jpg

-- 
Frerich Raabe - [email protected]
www.froglogic.com - Multi-Platform GUI Testing


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

Message: 4
Date: Mon, 21 Dec 2015 20:58:38 +0700
From: Kim-Ee Yeoh <[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 nest arbitrary things
Message-ID:
        <capy+zdsqe8e4e9hdm0srjnjf5ucp1sjwce0cpymeaard8x2...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Mon, Dec 21, 2015 at 8:51 PM, Imants Cekusins <[email protected]> wrote:

data Item a = Truck a [Item] | Box a [Item] | Parcel a [Item] | Can a [Item]


A cardinal rule of FP data modelling is to avoid the hazards of junk,
a.k.a. make the meaningless unspeakable.

Here an Item could be a Can that contains a Parcel that contains a Box that
in turn has a Truck inside.

What will Item-processing functions do with this Item?

Garbage In Garbage Out.

-- Kim-Ee
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20151221/6e098f31/attachment-0001.html>

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

Message: 5
Date: Mon, 21 Dec 2015 14:59:55 +0100
From: Imants Cekusins <[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 nest arbitrary things
Message-ID:
        <CAP1qinYu+u4gmHHpit4C1uFDzM7EU3Uo2KKMP=+g7olpqd5...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

>  
> http://i.telegraph.co.uk/multimedia/archive/01845/truck-on-truck-on-_1845173i.jpg

:D

> Garbage In Garbage Out.


well Martin would like to
>  model things which can contain other things.


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

Message: 6
Date: Mon, 21 Dec 2015 09:07:57 -0500
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 nest arbitrary things
Message-ID:
        <CAN+Tr41=kzVegzS6-KYGVLERWfrg+2QCf7ik7rsS6+=hra0...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Personally I'd try to use the type system, if possible.

data Box a = Box [a]
data Parcel a = Parcel [a]
data Can = Can


data Truck a = Truck {
  tname :: String,
  truckContents :: [a]
}

oneboxoftwocanstruck :: Truck (Box Can)
oneboxoftwocanstruck = Truck "Truck of Boxes of Cans" [Box [Can,Can]]

onecantruck :: Truck Can
onecantruck = Truck "Truck of Cans" [Can]

This gets some of the type safety without bogging you down too much.  You
can still end up with parcels with trucks in them, but it's not too bad.
At least cans are just cans, and functions can be written for trucks that
only work on trucks, for example.

On Mon, Dec 21, 2015 at 4:40 AM, martin <[email protected]> wrote:

> Hello all,
>
> I was trying to model things which can contain other things. This is easy
> as long as containers and the contained items
> all can be described in the same fashion. However when I want to model -
> say -
>
>         trucks
>         containing boxes
>                 containing parcels
>                         containing cans
>
> and trucks, boxes, parcels and cans are not of the same type, then this
> nested thing will become a type in its own right
> and it will be of a different type than trucks containing cans (which are
> not wrappen in parcels ...)
>
> As long as I can squeeze trucks, boxes ... into one type, possibly by
> using a "container_type" attribute, there is no
> problem. Is this the only way to do this? Is there an idiom?
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20151221/35c6d307/attachment-0001.html>

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

Message: 7
Date: Mon, 21 Dec 2015 15:21:11 +0100
From: Frerich Raabe <[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 nest arbitrary things
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII; format=flowed

On 2015-12-21 10:40, martin wrote:
> I was trying to model things which can contain other things. This is easy as 
> long as containers and the contained items
> all can be described in the same fashion. However when I want to model - say 
> -
> 
>       trucks
>       containing boxes
>               containing parcels
>                       containing cans
> 
> and trucks, boxes, parcels and cans are not of the same type, then this 
> nested thing will become a type in its own right
> and it will be of a different type than trucks containing cans (which are 
> not wrappen in parcels ...)
> 
> As long as I can squeeze trucks, boxes ... into one type, possibly by using 
> a "container_type" attribute, there is no
> problem. Is this the only way to do this? Is there an idiom?

In addition to what the others wrote (I'd personally probably start with the 
'data Truck a = Truck [a]' idea) you could also use the type system to 
express the possible *legal* ways to nest the load of a truck, e.g.:

   -- A Can can't contain anything
   data Can = Can

   -- A Parcel consists of zero or more cans
   data Parcel = Parcel [Can]

   -- A Box can be empty or contain a mixture of cans and parcels
   data BoxContent = BCCan Can | BCParcel Parcel
   data Box = Box [BoxContent]

   -- A Truck can be empty or contain a mixture of cans, parcels and boxes
   data TruckConent = TCCan Can | TCParcel Parcel | TCBox Box
   data Truck = Truck [TruckContent]

This might be too annoying to deal with though, i.e. the gain of type safety 
is not big enough to actually follow this path.

-- 
Frerich Raabe - [email protected]
www.froglogic.com - Multi-Platform GUI Testing


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

Message: 8
Date: Mon, 21 Dec 2015 18:00:06 +0100
From: martin <[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 nest arbitrary things
Message-ID: <[email protected]>
Content-Type: text/plain; charset=windows-1252

Am 12/21/2015 um 03:21 PM schrieb Frerich Raabe:
> On 2015-12-21 10:40, martin wrote:
>> I was trying to model things which can contain other things. This is easy as 
>> long as containers and the contained items
>> all can be described in the same fashion. However when I want to model - say 
>> -
>>
>>     trucks
>>     containing boxes
>>         containing parcels
>>             containing cans
>>
>> and trucks, boxes, parcels and cans are not of the same type, then this 
>> nested thing will become a type in its own right
>> and it will be of a different type than trucks containing cans (which are 
>> not wrappen in parcels ...)
>>
>> As long as I can squeeze trucks, boxes ... into one type, possibly by using 
>> a "container_type" attribute, there is no
>> problem. Is this the only way to do this? Is there an idiom?
> 
> In addition to what the others wrote (I'd personally probably start with the 
> 'data Truck a = Truck [a]' idea) you could
> also use the type system to express the possible *legal* ways to nest the 
> load of a truck, e.g.:
> 
>   -- A Can can't contain anything
>   data Can = Can
> 
>   -- A Parcel consists of zero or more cans
>   data Parcel = Parcel [Can]
> 
>   -- A Box can be empty or contain a mixture of cans and parcels
>   data BoxContent = BCCan Can | BCParcel Parcel
>   data Box = Box [BoxContent]
> 
>   -- A Truck can be empty or contain a mixture of cans, parcels and boxes
>   data TruckConent = TCCan Can | TCParcel Parcel | TCBox Box
>   data Truck = Truck [TruckContent]
> 
> This might be too annoying to deal with though, i.e. the gain of type safety 
> is not big enough to actually follow this
> path.
> 

That's fine. I'm happy not to be able to pack a truck into another truck. Only 
problem is that I don't know how to write
an "unpack" function, which removes one level of nesting. I can only write 
unpackTruck, unpackParcel ...

I suppose the ability to write a generic unpack function implies that there can 
be a generic pack function and then I
could pack a truck into another truck.








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

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


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

End of Beginners Digest, Vol 90, Issue 38
*****************************************

Reply via email to