Old/sampou.org/HowTo_Array

HowTo_Array

HowTo:Array


Array

Hierarchical Libraries では Data.Array.

配列の構築

Prelude Array> array (1,9) [(i, i*i) | i <- [1..9]]
array (1,9) [(1,1),(2,4),(3,9),(4,16),(5,25),(6,36),(7,49),(8,64),(9,81)]
Prelude Array> listArray (1,9) [i*i | i <- [1..9]]
array (1,9) [(1,1),(2,4),(3,9),(4,16),(5,25),(6,36),(7,49),(8,64),(9,81)]

以下

Prelude Array> let a =  array (1,9) [(1,1),(2,4),(3,9),(4,16),(5,25),(6,36),(7,49),(8,64),(9,81)]

のもとで

配列の要素

Prelude Array> a ! 5
25
Prelude Array> elems a
[1,4,9,16,25,36,49,64,81]
Prelude Array> assocs a
[(1,1),(2,4),(3,9),(4,16),(5,25),(6,36),(7,49),(8,64),(9,81)]

配列の境界

Prelude Array> bounds a
(1,9)
Prelude Array> indices a
[1,2,3,4,5,6,7,8,9]

配列の更新

Prelude Array> a // [(1,0), (2,0)]
array (1,9) [(1,0),(2,0),(3,9),(4,16),(5,25),(6,36),(7,49),(8,64),(9,81)]

蓄積配列

Prelude Array> accumArray (+) 0 (1,3) [(1,1),(1,1),(1,1),(2,1),(2,1)]
array (1,3) [(1,3),(2,2),(3,0)]

配列の添字の射影

Prelude Array> ixmap (0,8) (flip subtract 9) a
array (0,8) [(0,81),(1,64),(2,49),(3,36),(4,25),(5,16),(6,9),(7,4),(8,1)]
Prelude Array> let b = array ((1,1),(10,10)) [((i, j), i + j * 10) | i <- [1..10], j <- [1..10]]
Prelude Array> ixmap (1,10) ((,) 4) b
array (1,10) [(1,14),(2,24),(3,34),(4,44),(5,54),(6,64),(7,74),(8,84),(9,94),(10,104)]

配列を使った fibonacci

fib = array (0,99) $ (0,1):(1,1):[(i, fib ! (i-1) + fib ! (i - 2)) | i <- [2..99]]

Last modified : 2006/07/03 21:04:33 JST