## Change the entries in an array in Haskell

Suppose you have

`let a = array ((1,1),(2,2)) [((2,1),3),((1,2),2),((1,1),2),((2,2),3)]`

Now I want that to multiply the 3 in the last tuple with some number. How can I do that?

If you wanted to multiply that by `5`

it would be:

accum (*) a [((2,2),5)] -- ^ ^ ^ ^ -- function to combine values -- array to read -- one of the indices to manipulated -- value to give to f for the associated index

The signature of this function reads

accum :: Ix i => (e -> a -> e) -> Array i e -> [(i, a)] -> Array i e

and its documentation says:

accum f takes an array and an association list and accumulates pairs from the list into the array with the accumulating function f.

So calling `accum f arrayValue listWithPairsOfIndicesAndValues`

will call `f`

for every index given in `listWithPairsOfIndicesAndValues`

providing the old value and the value from `listWithPairsOfIndicesAndValues`

for this index and return a new array with all the positions mentioned in `listWithPairsOfIndicesAndValues`

updated with the values returned by the respective calls to `f`

.

**A Gentle Introduction to Haskell: Arrays,** Finally, the index operation allows a particular element of an array to be the function higher-order, simply replacing sum and (*) by functional parameters: The first interface provided by the new array library, is defined by the typeclass IArray (which stands for "immutable array" and defined in the module Data.Array.IArray) and defines the same operations that were defined for Array in Haskell'98. The big difference is that it is now a typeclass and there are 4 array type constructors, each of which implements this interface: Array, UArray, DiffArray, and DiffUArray.

There's two different ways to your goal:

Using the incremental update:

`a // [((2,2),(3*7))]`

`>>> array ((1,1),(2,2)) [((1,1),2),((1,2),2),((2,1),3),((2,2),21)]`

(Instead of 3*7, you could use your own n and refer to that same location (a!(2,2)))

Using the accumulation:

`accum (*) a [((2,2), 7)]`

`>>> array ((1,1),(2,2)) [((1,1),2),((1,2),2),((2,1),3),((2,2),21)]`

**Data.Array - Hackage,** The type of immutable non-strict (boxed) arrays with indices in i and elements in e . Instances. Instances details. IArray Array e Source #. Although Haskell has an incremental array update operator, the main thrust of the array facility is monolithic. Arrays are not part of the Standard Prelude -- the standard library contains the array operators. Any module using arrays must import the Array module. 11.1 Index types The Ix library defines a type class of array indices:

One could use `a // [(2,2), n * (a ! (2,2))]`

to multiply the element with index `(2,2)`

by `n`

.

(There should also be some lens-y approach with a better syntax)

**Arrays,** Delete elements that meet some condition. Haskell has a function called filter which will do this for you. Beware Haskell, therefore, does not treat arrays as general functions with an application operation, but as abstract data types with a subscript operation. Two main approaches to functional arrays may be discerned: incremental and monolithic definition.

**How to work on lists,** Haskell, therefore, does not treat arrays as general functions with an application For example, the bounds of a 10-element, zero-origin vector with Int indices the function higher-order, simply replacing sum and (*) by functional parameters: Basic non-strict arrays. Note: The Data.Array.IArray module provides a more general interface to immutable arrays: it defines operations with the same names as those defined below, but with more general types, and also defines Array instances of the relevant classes. To use that more general interface, import Data.Array.IArray but not Data.Array.

**A Gentle Introduction to Haskell: Arrays,** r/haskell: The Haskell programming language community. Daily news and info about all things Haskell related: practical stuff, theory, types … 14.1 Immutable non-strict arrays . Haskell provides indexable arrays, which may be thought of as functions whose domains are isomorphic to contiguous subsets of the integers. Functions restricted in this way can be implemented efficiently; in particular, a programmer may reasonably expect rapid access to the components.

**How to replace an element at an index in a list with another element ,** Daily news and info about all things Haskell related: practical stuff, theory, types 1 to the element at the given index, returning a new array in which this change Convert an array of given length into a Haskell list. The implementation is tail-recursive and so uses constant stack space. peekArray0 :: ( Storable a, Eq a) => a -> Ptr a -> IO [a] Source #