The following is supposed to be initializing a 2D array but it doesn't seem to work.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
KC
Reply | Threaded
Open this post in threaded view
|

The following is supposed to be initializing a 2D array but it doesn't seem to work.

KC
newArr :: (Ix i) => i -> i -> e -> Array i e
newArr n m x = listArray (n,m) (repeat x)
 
-----
 
Prelude Data.Array> newArr 0 10 0
array (0,10) [(0,0),(1,0),(2,0),(3,0),(4,0),(5,0),(6,0),(7,0),(8,0),(9,0),(10,0)]
 
Prelude Data.Array> newArr (0,0) (5,5) 0
array ((0,0),(5,5)) [((0,0),0),((0,1),0),((0,2),0),((0,3),0),((0,4),0),((0,5),0),((1,0),0),((1,1),0),((1,2),0),((1,3),0),((1,4),0),((1,5),0),((2,0),0),((2,1),0),((2,2),0),((2,3),0),((2,4),0),((2,5),0),((3,0),0),((3,1),0),((3,2),0),((3,3),0),((3,4),0),((3,5),0),((4,0),0),((4,1),0),((4,2),0),((4,3),0),((4,4),0),((4,5),0),((5,0),0),((5,1),0),((5,2),0),((5,3),0),((5,4),0),((5,5),0)]

All I am getting is this:
array ((1,5),(1,5)) [((1,5),1.0)]

Maybe the behaviour of ghc was changed since the article was written.


--
--
Regards,
KC

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: The following is supposed to be initializing a 2D array but it doesn't seem to work.

Ivan Lazar Miljenovic
On 1 August 2012 07:52, KC <[hidden email]> wrote:

> newArr :: (Ix i) => i -> i -> e -> Array i e
>
> newArr n m x = listArray (n,m) (repeat x)
>
>
> -----
>
> Prelude Data.Array> newArr 0 10 0
>
> array (0,10)
> [(0,0),(1,0),(2,0),(3,0),(4,0),(5,0),(6,0),(7,0),(8,0),(9,0),(10,0)]
>
>
> Prelude Data.Array> newArr (0,0) (5,5) 0
>
> array ((0,0),(5,5))
> [((0,0),0),((0,1),0),((0,2),0),((0,3),0),((0,4),0),((0,5),0),((1,0),0),((1,1),0),((1,2),0),((1,3),0),((1,4),0),((1,5),0),((2,0),0),((2,1),0),((2,2),0),((2,3),0),((2,4),0),((2,5),0),((3,0),0),((3,1),0),((3,2),0),((3,3),0),((3,4),0),((3,5),0),((4,0),0),((4,1),0),((4,2),0),((4,3),0),((4,4),0),((4,5),0),((5,0),0),((5,1),0),((5,2),0),((5,3),0),((5,4),0),((5,5),0)]
>
>
> All I am getting is this:
> array ((1,5),(1,5)) [((1,5),1.0)]
>
> Maybe the behaviour of ghc was changed since the article was written.

Works for me:

$ghci
GHCi, version 7.4.2: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> :m +Data.Array
Prelude Data.Array> let newArr n m x = listArray (n,m) (repeat x)
Prelude Data.Array> newArr (0,0) (5,5) 0
array ((0,0),(5,5))
[((0,0),0),((0,1),0),((0,2),0),((0,3),0),((0,4),0),((0,5),0),((1,0),0),((1,1),0),((1,2),0),((1,3),0),((1,4),0),((1,5),0),((2,0),0),((2,1),0),((2,2),0),((2,3),0),((2,4),0),((2,5),0),((3,0),0),((3,1),0),((3,2),0),((3,3),0),((3,4),0),((3,5),0),((4,0),0),((4,1),0),((4,2),0),((4,3),0),((4,4),0),((4,5),0),((5,0),0),((5,1),0),((5,2),0),((5,3),0),((5,4),0),((5,5),0)]
Prelude Data.Array>


Which article are you referring to?

>
>
> --
> --
> Regards,
> KC
>
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>



--
Ivan Lazar Miljenovic
[hidden email]
http://IvanMiljenovic.wordpress.com

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
KC
Reply | Threaded
Open this post in threaded view
|

Re: The following is supposed to be initializing a 2D array but it doesn't seem to work.

KC
In reply to this post by KC
That was it!

On Tue, Jul 31, 2012 at 3:34 PM, Jan-Willem Maessen <[hidden email]> wrote:


On Tue, Jul 31, 2012 at 5:52 PM, KC <[hidden email]> wrote:
All I am getting is this:
array ((1,5),(1,5)) [((1,5),1.0)]

Maybe the behaviour of ghc was changed since the article was written.

I think you've made a common mistake here.  Array bounds are (lower in all dimensions, upper in all dimensions).  So perhaps you intended to use the bounds ((1,1), (5,5)) in your code?

I make this mistake frequently.

-Jan-Willem Maessen



--
--
Regards,
KC

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe