Skip to content

Instantly share code, notes, and snippets.

@evgenii-malov
Last active September 6, 2022 15:57
Show Gist options
  • Save evgenii-malov/7685937e38c9c73c93a76c31935cfe01 to your computer and use it in GitHub Desktop.
Save evgenii-malov/7685937e38c9c73c93a76c31935cfe01 to your computer and use it in GitHub Desktop.
work with Immutable array in Haskell
-- video https://youtu.be/RPEtIZ44RSc
import Data.Array.IArray
import Data.Char (toUpper)
-- elements that is associated with some kind of numerical index
-- truly constant time (O(1)) access.
-- store their data more efficiently in memory.
-- modification function must create an entirely new array (partially can be fixed with Monad context)
-- math look:
-- functions from indices to values
-- to assure efficient, domains must be isomorphic to finite contiguous subsets of the integers.
main = do
let a = array (0,2) [(0, 'a'),(1, 'b'),(2, 'c')] :: Array Int Char
let c = array (1,1) [(1, 'h')] :: Array Int Char
let b = listArray (0, 2) ['x','y','z'] :: Array Int Char
let e = a ! 1
let bs = bounds a
let es = elems a
let is = indices a
let pas = assocs a
let ua = amap toUpper a
let ub = ixmap (0,1) (+1) b
print a
print b
print e
print bs
print es
print is
print pas
print c
let d = a // (assocs c)
print d
print ua
print ub
--Some links:
--https://www.haskell.org/tutorial/arrays.html
--https://mmhaskell.com/data-structures/array
--https://wiki.haskell.org/Arrays
@evgenii-malov
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment