
Is some kind of collection of object with different types in Haskell exist? Except the tuples, which have fixed length. I find this * Tuples heterogeneous, lists homogeneous. * Tuples have a fixed length, or at least their length is encoded in their type. That is, two tuples with different lengths will have different types. * Tuples always finite. But I need something which is heterogeneous and non-fixed length. I'm used do Java, and this switch to functional languages is very strange to me. So, to be clear, I need something like LinkedList<Object> in java. Can you please help me or suggest me, what can I use in this case? Valentin

On 11/17/06, Valentin Gjorgjioski
But I need something which is heterogeneous and non-fixed length.
You're looking for HList: http://homepages.cwi.nl/~ralf/HList/ Kurt

this is not exactly what you are looking for, but it contains a link to HList: http://www.haskell.org/haskellwiki/Extensible_record and then there is the follow-up work to HList, an OO library written in Haskell: http://homepages.cwi.nl/~ralf/OOHaskell/ it's quite a challenging read if you just want to go ahead and use hetero-collections, but it should give you a lot of ideas and pointers. i think there are also less type-safe alternatives in at least ghc. is Data.Typeable an interesting thing to look at? hth, matthias On Fri, Nov 17, 2006 at 06:36:30PM +0100, Valentin Gjorgjioski wrote:
To: haskell-cafe@haskell.org From: Valentin Gjorgjioski
Date: Fri, 17 Nov 2006 18:36:30 +0100 Subject: [Haskell-cafe] Collection of objects? Is some kind of collection of object with different types in Haskell exist? Except the tuples, which have fixed length. I find this
* Tuples heterogeneous, lists homogeneous. * Tuples have a fixed length, or at least their length is encoded in their type. That is, two tuples with different lengths will have different types. * Tuples always finite.
But I need something which is heterogeneous and non-fixed length. I'm used do Java, and this switch to functional languages is very strange to me. So, to be clear, I need something like LinkedList<Object> in java.
Can you please help me or suggest me, what can I use in this case?
Valentin _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Institute of Information Systems, Humboldt-Universitaet zu Berlin web: http://www.wiwi.hu-berlin.de/~fis/ e-mail: fis@wiwi.hu-berlin.de tel: +49 30 2093-5742 fax: +49 30 2093-5741 office: Spandauer Strasse 1, R.324, 10178 Berlin, Germany pgp: AD67 CF64 7BB4 3B9A 6F25 0996 4D73 F1FD 8D32 9BAA

On Fri, 17 Nov 2006, Valentin Gjorgjioski wrote:
Is some kind of collection of object with different types in Haskell exist? Except the tuples, which have fixed length. I find this
* Tuples heterogeneous, lists homogeneous. * Tuples have a fixed length, or at least their length is encoded in their type. That is, two tuples with different lengths will have different types. * Tuples always finite.
But I need something which is heterogeneous and non-fixed length. I'm used do Java, and this switch to functional languages is very strange to me. So, to be clear, I need something like LinkedList<Object> in java.
Can you please help me or suggest me, what can I use in this case?
If the number of types to cover is fixed, then I suggest a data type like data T = ConsInt Int | ConsString String | ConsChar Char Since this is a very FAQ I wonder why I don't find the answer in any of the Haskell wikis.

Depending on your needs and your comfort level with fancier types, the
existential approach to ADTs might solve your problem. The following
code is a demonstration you can cut-and-paste-and-run.
This is example akin to upcasting in Java to an interface that lets
you print things. That way you know how to print every object (or do
whatever else it is you need to do) in the list. Beware: there is no
safe downcasting (that's what Typeable would be for); that would
likely be more than you need.
There are other ways to do this with existentials (e.g. bounded
existentials), but this is what came out of my head when I read your
post. Existentials seems to be the "Haskellish" way to reduce a
hetergenous list to a collection of objects with common operations.
HList would be the Haskellish way for more static and flexible
assurances.
{-# OPTIONS -fglasgow-exts #-}
module Test where
data PrintPackage = forall a . PrintPackage a (a -> String)
instance Show PrintPackage where
show (PrintPackage val showMethod) = showMethod val
list = [ PrintPackage 3 show
, PrintPackage "string" show
, PrintPackage 3.4 show
]
main = print list
Hope that helps more than it confuses,
Nick
On 11/17/06, Henning Thielemann
On Fri, 17 Nov 2006, Valentin Gjorgjioski wrote:
Is some kind of collection of object with different types in Haskell exist? Except the tuples, which have fixed length. I find this
* Tuples heterogeneous, lists homogeneous. * Tuples have a fixed length, or at least their length is encoded in their type. That is, two tuples with different lengths will have different types. * Tuples always finite.
But I need something which is heterogeneous and non-fixed length. I'm used do Java, and this switch to functional languages is very strange to me. So, to be clear, I need something like LinkedList<Object> in java.
Can you please help me or suggest me, what can I use in this case?
If the number of types to cover is fixed, then I suggest a data type like
data T = ConsInt Int | ConsString String | ConsChar Char
Since this is a very FAQ I wonder why I don't find the answer in any of the Haskell wikis. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Fri, 17 Nov 2006, Valentin Gjorgjioski wrote: ...
But I need something which is heterogeneous and non-fixed length. I'm used do Java, and this switch to functional languages is very strange to me. So, to be clear, I need something like LinkedList<Object> in java.
I looked in a couple of on-line tutorials, thinking I would quickly find something about this, but -- maybe in my haste I missed something. Objective CAML documentation gets right to it, I wonder if we're assuming prior exposure to strongly typed functional languages? Anyway, you need to define a type that accounts for the types you want to support - data Object = IntObject Int | StringObject String objectString :: Object -> String objectString (IntObject v) = show v objectString (StringObject v) = v main = mapM (putStrLn . objectString) [(IntObject 7), (StringObject "eight")] Donn Cave, donn@drizzle.com

On 17.11.2006 19:08 Donn Cave wrote:
On Fri, 17 Nov 2006, Valentin Gjorgjioski wrote: ...
But I need something which is heterogeneous and non-fixed length. I'm used do Java, and this switch to functional languages is very strange to me. So, to be clear, I need something like LinkedList<Object> in java.
I looked in a couple of on-line tutorials, thinking I would quickly find something about this, but -- maybe in my haste I missed something. Objective CAML documentation gets right to it, I wonder if we're assuming prior exposure to strongly typed functional languages?
Anyway, you need to define a type that accounts for the types you want to support -
data Object = IntObject Int | StringObject String
objectString :: Object -> String objectString (IntObject v) = show v objectString (StringObject v) = v
main = mapM (putStrLn . objectString) [(IntObject 7), (StringObject "eight")]
Donn Cave, donn@drizzle.com
Thanks to all try to help me. I like the solution from Donn and Henning. I obviously not read enough. The solution is also in Yet Another Haskell Tutorial. Ok, not this much explicit, but its there. It says We have seen an example of the data type with one constructor: Pair. It is also possible (and extremely useful) to have multiple constructors(I'm shame that I even didn't know this). It's interesting that it's said EXTREMELY useful. And yes, it is! So, I think that Henning idea is good and this should go on some haskell wiki - s. Once again thanks to all for quick and nice solution.

On Nov 17, 2006, at 12:36 PM, Valentin Gjorgjioski wrote:
Is some kind of collection of object with different types in Haskell exist? Except the tuples, which have fixed length. I find this
* Tuples heterogeneous, lists homogeneous. * Tuples have a fixed length, or at least their length is encoded in their type. That is, two tuples with different lengths will have different types. * Tuples always finite.
But I need something which is heterogeneous and non-fixed length. I'm used do Java, and this switch to functional languages is very strange to me. So, to be clear, I need something like LinkedList<Object> in java.
The thing you're looking for doesn't really exist. (OK, yes, I'm lying a bit. You can use existential types, but you probably don't actually need them, and they can get complicated quickly; they're better left until later). Can you give us more details about what you're trying to do? Readjusting your thinking patterns can be difficult, but the people on this list are usually happy to help.
Can you please help me or suggest me, what can I use in this case?
Valentin
Rob Dockins Speak softly and drive a Sherman tank. Laugh hard; it's a long way to the bank. -- TMBG

i have cut-and-pasted the last few postings into the wiki: http://haskell.org/haskellwiki/Heterogenous_collections it'is very ad hoc, but i hope it helps those who bump into it a little. and it's easier to edit and improve than a mailing list thread. (-: matthias

On Fri, 17 Nov 2006, Matthias Fischmann wrote:
i have cut-and-pasted the last few postings into the wiki:
http://haskell.org/haskellwiki/Heterogenous_collections
it'is very ad hoc, but i hope it helps those who bump into it a little. and it's easier to edit and improve than a mailing list thread. (-:
Thanks a lot for setting it up! Maybe there should be a FAQ category? This page would certainly belong to it.

On Mon, Nov 20, 2006 at 02:13:30PM +0100, Henning Thielemann wrote:
To: Matthias Fischmann
Cc: haskell-cafe@haskell.org From: Henning Thielemann Date: Mon, 20 Nov 2006 14:13:30 +0100 (CET) Subject: Re: [Haskell-cafe] Collection of objects? On Fri, 17 Nov 2006, Matthias Fischmann wrote:
i have cut-and-pasted the last few postings into the wiki:
http://haskell.org/haskellwiki/Heterogenous_collections
it'is very ad hoc, but i hope it helps those who bump into it a little. and it's easier to edit and improve than a mailing list thread. (-:
Thanks a lot for setting it up!
Maybe there should be a FAQ category? This page would certainly belong to it.
Sure, makes sense to me. (Not that it has to. ;) I just haven't done it because I don't know very much about wiki categories. I am also currently a little distracted. But hey, it's wiki! By all means do stuff to it. cheers, matthias

On Fri, 17 Nov 2006 18:36:30 +0100, you wrote:
But I need something which is heterogeneous and non-fixed length. I'm used do Java, and this switch to functional languages is very strange to me. So, to be clear, I need something like LinkedList<Object> in java.
In an attempt to leverage your existing Java knowledge... In Java, while the "things" in your list are heterogeneous at some level, they are homogeneous at another: They are all instances of Object (or a subclass thereof). You can't, for example, put an int in your list. And in fact, that's a primary reason for the existence of the Integer class--it makes your int look like an Object, so that you can treat it like one. (And similarly for Character, Float, Double, etc.) So you do the analogous thing in Haskell: First, you ask yourself, "What kinds of things do I want to put in my list?" Once you have the answer to that, you ask, "Are these things homogeneous at some level?" If they are, then you make your list a list of those homogenous things (actually, the compiler generally does it for you). If they aren't, then you wrap them up in a homogeneous wrapper in exactly the same way as you wrap your int in an Integer, your double in a Double, etc., in Java. Some of the other replies have shown you how to declare this; it's trivially simple: data MyHomogeneousWrapper = Integer Int | Character Char | Float Float deriving Show Finally, constructing new instances of this homogenous wrapper in Haskesll is just like it is in Java: myList = [(Integer 42), (Character 'a'), (Float 3.14159)] vs. myList = new LinkedList(); myList.add(new Integer(42)); myList.add(new Character('a')); myList.add(new Float(3.14159)); Steve Schafer Fenestra Technologies Corp. http://www.fenestra.com/

On Fri, 17 Nov 2006 20:04:22 +0100, Steve Schafer
myList = [(Integer 42), (Character 'a'), (Float 3.14159)]
You can leave out the parentheses, making even simpler: myList = [Integer 42, Character 'a', Float 3.14159] -- Met vriendelijke groet, Henk-Jan van Tuyl -- http://Van.Tuyl.eu/ -- Using Opera's revolutionary e-mail client: https://secure.bmtmicro.com/opera/buy-opera.html?AID=789433
participants (9)
-
Donn Cave
-
Henk-Jan van Tuyl
-
Henning Thielemann
-
Kurt Hutchinson
-
Matthias Fischmann
-
Nicolas Frisby
-
Robert Dockins
-
Steve Schafer
-
Valentin Gjorgjioski