[Haskell-beginners] question about comprehension or array creation

Tim Perry perry2of5 at yahoo.com
Sun Jan 16 06:52:04 CET 2011


The docs say:

 [(i, e)] a list of associations of the form (index, value). Typically, this 
list will be expressed as a comprehension. An association '(i, x)' defines the 
value of the array at index i to be x. 


I think the important part is that the array is used as a lookup for the values 
to associate with the array. The lookup returns a ?random? one of the three 
values in the list of tuples that have 1 as the first index. In this case it 
happens to be 3. And so on.

This may help:
  Prelude Array> let e = [1,2,3]
Prelude Array> array (1,3) [(i,v) | i<-[1..3], v<-e]
array (1,3) [(1,3),(2,3),(3,3)]
Prelude Array> array (1,9) [(i,v) | i<-[1..3], v<-e]
array (1,9) [(1,3),(2,3),(3,3),(4,*** Exception: (Array.!): undefined array 
element
Prelude Array> 

It couldn't look up a value for 4 so it failed...

Does that help?

--Tim



----- Original Message ----
From: cchang <djvsrose at gmail.com>
To: beginners at haskell.org
Sent: Sat, January 15, 2011 6:19:05 AM
Subject: [Haskell-beginners] question about comprehension or array creation

Hi,

I tried to create an array like the following.

"array (1,3) [(1,1), (2,2), (3,3)]"

through code in .hs
e = [1,2,3]
array (1,3) [(i,v) | i<-[1..3], v<-e]

but I got
"array (1,3) [(1,3), (2,3), (3,3)]"

why v is always 3 in this case? Can anyone shed some light on this?

Thanks,



_______________________________________________
Beginners mailing list
Beginners at haskell.org
http://www.haskell.org/mailman/listinfo/beginners




More information about the Beginners mailing list