[Haskell-cafe] Collection of objects?

Valentin Gjorgjioski valentin.gjorgjioski at ijs.si
Fri Nov 17 13:55:26 EST 2006

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 at 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.

More information about the Haskell-Cafe mailing list