Jhoy
Jhoy

Reputation: 117

Cannot understand haskell arrays

I cannot understand haskell arrays.

For example I want to create and store an array in variable bsd but what goes in .... if I want an array of size eg 10 and of type Bool.

bsd :: Array Int Bool --is this correct?
bsd = .... --what comes here?

Please help me understand...

and what if I want to change a value in bsd at e.g. index 5 what is the syntax

and how can i access a index in bsd ??

please help

Upvotes: 1

Views: 1189

Answers (1)

clay
clay

Reputation: 20400

Using https://hackage.haskell.org/package/array-0.5.2.0/docs/Data-Array-IArray.html

This constructs an array of bools from a list. There are tons of other options and functions to use as well

import Data.Array.IArray
let bsd = listArray (0, 3) [False, True, True, False] :: Array Int Bool
elems bsd -- [False,True,True,False]
bsd -- array (0,3) [(0,False),(1,True),(2,True),(3,False)]
bsd ! 0 -- Get element at index 0, which is False
-- Create new array with element 0 changed to True.
let bsd2 = bsd // [(0, True)]
bsd2 -- array (0,3) [(0,True),(1,True),(2,True),(3,False)]

Upvotes: 2

Related Questions