Why doesn't Haskell provide folds for one-dimensional arrays?

Data.Arraydoes not provide folds for type Array.

In the real world of Haskell (chapter 12), the reason is that it Arraycan be folded differently depending on the needs of the programmer:

First of all, there are several types of folds that make sense. We may still want to discard individual elements, but now we have the ability to stack along rows or columns. In addition, to collapse elements in time, there are no longer only two sequences for traversal.

Is this not true for Lists? It is very common to represent, for example, a multidimensional matrix List, but Listfolds still exist for one-dimensional s.

What subtlety am I missing? Is multidimensional Arrayenough different from a Arrayof Arrays?

Edit: Hm, even multidimensional arrays have certain folds as instances Data.Foldable. [0] So, how does this compare with the quote from Real World Haskell?

[0] http://hackage.haskell.org/packages/archive/base/4.6.0.0/doc/html/Data-Foldable.html

+5
source share
2 answers

Since you mention the difference between “multidimensional” Arrayand Arrayof Arrays, this will illustrate this point well, along with matching with lists.

( Foldable) - , , - ; , foldr. , foldl, .

Array , ​​ . , , , Ix. , , . , Ix , , - .

Array s? , . Ix , , , "" Array, ! . , Ix , ? - , ?

, , , Array , Ix, , - , .

Array of Array s, , , : Array , Array .


, , Array s, , Ix, ? , Array .

, , , Foldable.

+8

, foldr. :

(:) :: a -> [a] -> [a]
[]  :: [a]

[a] b, :

f :: a -> b -> b
z :: b

, , foldr :

foldr :: (a -> b -> b) -> b -> [a] -> b

, / , foldr . " , (:) [], ".

Array ; (Ix i => [(i,a)]), : , .

+4
source

All Articles