[Haskell-beginners] Creating arrays in Haskell

Karol Samborski edv.karol
Wed Oct 2 10:43:14 UTC 2013


2013/10/2 Michal Kawalec <michal at bazzle.me>

> Excerpts from Karol Samborski's message of 2013-10-02 11:11:15 +0100:
> > I think you should just add type signature to the [1..100], like this:
> > array (1,100) [(i, i*i) | i <- ([1..100]::[Int])]
>
> Thank you, it seems that it worked, but with my limited knowledge of
> Haskell I stumbled onto another problem:
>
> Prelude Data.List Math.FFT Data.Array.CArray Data.Array.Base> array
> (1,100) [(i, i*i) | i <- [1..100]::[Int]]
>
> <interactive>:74:1:
>     No instance for (IArray a0 Int) arising from a use of `array'
>     The type variable `a0' is ambiguous
>     Possible fix: add a type signature that fixes these type variable(s)
>     Note: there are several potential instances:
>       instance Foreign.Storable.Storable e => IArray CArray e
>         -- Defined in `Data.Array.CArray.Base'
>       instance IArray Array e -- Defined in `Data.Array.Base'
>       instance IArray UArray Int -- Defined in `Data.Array.Base'
>     Possible fix: add an instance declaration for (IArray a0 Int)
>     In the expression:
>       array (1, 100) [(i, i * i) | i <- [1 .. 100] :: [Int]]
>     In an equation for `it':
>         it = array (1, 100) [(i, i * i) | i <- [1 .. 100] :: [Int]]
>
>
> --
> Michal
>
>
This is because ghci doesn't know which instance of IArray to use. If you
look at type signature of function 'array' you'll see that it returns some
type 'a i e' where 'a e' is an instance of IArray class. So what you must
do is simply tell it what 'a i e' you want. Try this:

array (1,100) [(i, i*i) | i <- [1..100]] :: CArray Int Int

Best,
Karol
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20131002/757f51f8/attachment.html>



More information about the Beginners mailing list