Arrays in Haskell

Our first program will demonstrate how to work with arrays in Haskell. Here’s the full source code and explanation:

import Data.Array

main :: IO ()
main = do
    -- Create an array with 5 elements, all initialized to 0
    let a = array (0, 4) [(i, 0) | i <- [0..4]] :: Array Int Int
    putStrLn $ "emp: " ++ show a

    -- Set a value at an index
    let a' = a // [(4, 100)]
    putStrLn $ "set: " ++ show a'
    
    -- Get a value from an index
    putStrLn $ "get: " ++ show (a' ! 4)

    -- Get the length of the array
    putStrLn $ "len: " ++ show (rangeSize $ bounds a')

    -- Create and initialize an array in one line
    let b = array (0, 4) [(i, i+1) | i <- [0..4]] :: Array Int Int
    putStrLn $ "dcl: " ++ show b

    -- Create an array with specified elements
    let c = listArray (0, 4) [1, 2, 3, 4, 5] :: Array Int Int
    putStrLn $ "dcl: " ++ show c

    -- Create an array with some elements specified and others defaulting to 0
    let d = array (0, 4) [(0, 100), (3, 400), (4, 500)] :: Array Int Int
    putStrLn $ "idx: " ++ show d

    -- Create a 2D array
    let twoD = array ((0,0), (1,2)) [((i,j), i+j) | i <- [0..1], j <- [0..2]] :: Array (Int, Int) Int
    putStrLn $ "2d: " ++ show twoD

    -- Create and initialize a 2D array at once
    let twoD' = listArray ((0,0), (1,2)) [1,2,3,1,2,3] :: Array (Int, Int) Int
    putStrLn $ "2d: " ++ show twoD'

In Haskell, arrays are not as commonly used as lists, but they are available through the Data.Array module. Here’s a breakdown of the code:

  1. We import the Data.Array module to work with arrays.

  2. We create an array a with 5 elements, all initialized to 0. In Haskell, arrays are created with a specified range and a list of index-value pairs.

  3. We can set a value at an index using the // operator, which creates a new array with the updated value.

  4. To get a value from an array, we use the ! operator.

  5. The length of an array can be obtained by using rangeSize on the bounds of the array.

  6. We can create and initialize an array in one line using list comprehension.

  7. The listArray function allows us to create an array from a list of values.

  8. We can create an array with some elements specified and others defaulting to 0 by only providing the non-zero elements.

  9. Multidimensional arrays are created by using tuples as indices.

  10. We can also create and initialize multidimensional arrays at once using listArray.

When you run this program, you’ll see output similar to this:

emp: array (0,4) [(0,0),(1,0),(2,0),(3,0),(4,0)]
set: array (0,4) [(0,0),(1,0),(2,0),(3,0),(4,100)]
get: 100
len: 5
dcl: array (0,4) [(0,1),(1,2),(2,3),(3,4),(4,5)]
dcl: array (0,4) [(0,1),(1,2),(2,3),(3,4),(4,5)]
idx: array (0,4) [(0,100),(1,0),(2,0),(3,400),(4,500)]
2d: array ((0,0),(1,2)) [((0,0),0),((0,1),1),((0,2),2),((1,0),1),((1,1),2),((1,2),3)]
2d: array ((0,0),(1,2)) [((0,0),1),((0,1),2),((0,2),3),((1,0),1),((1,1),2),((1,2),3)]

Note that Haskell arrays are immutable, so operations that seem to modify an array actually create a new array. This is different from many other languages where arrays are mutable.