[Haskell-beginners] How to nest arbitrary things

Magnus Therning magnus at therning.org
Mon Dec 21 13:33:59 UTC 2015


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: magnus at therning.org   jabber: magnus at therning.org
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.sig>


More information about the Beginners mailing list