[Haskell-beginners] Arrays in Haskell
Henry Olders
henry.olders at mcgill.ca
Sat Sep 11 12:28:21 EDT 2010
Hi, Lorenzo!
I am also a Haskell beginner, coming from Python, where I also used numpy for speed.
In my python applications, I used lists and list operations (especially list comprehensions) which I found an elegant way of doing things. A numpy array was simply, for my needs, a list of lists.
More experienced users can correct me if I'm wrong, but there are some fundamental differences between Haskell and Python which might influence your choices of lists vs arrays. Haskell, being primarily a compiled language, should give you a speed advantage over interpreted python when you're doing basically the same operations, so you may not need (at least initially) to look for ways to improve speed. My approach basically in any language was to first get the program to work correctly, and to look for speed optimizations only as a secondary step if the program was too slow, and then only for the processes which were the speed bottlenecks.
The second issue is that in Haskell, lists are mutable, whereas arrays are (mostly) immutable. Generally, operations which need to work on every element of a data structure will go faster (and take less memory) if they do not result in the creation of a copy of the data structure, as they need to do for immutable data.
So my tendency would be to look for ways of doing things in Haskell which make use of lists (including lists of lists), and resort to arrays/matrices only for operations which cannot be decomposed into list operations.
Henry
On 2010-09-11, at 10:09 , Lorenzo Isella wrote:
> Dear All,
> The recent feedback I got on the mailing list led me to think about the
> data structure I need for my computations and array manipulations
> (loosely speaking, let us say that I need indexing and slicing tables of
> data).
> Coming from Python, I am a bit confused: let me say that in my Python
> scripts I almost never use lists, but rather NumPy arrays.
> In that case, it is an easy choice (almost every decent software for
> numerics/visualization etc... in Python relies explicitly or implicitly
> on NumPy). On top of that, NumPy is fast, has a lot of inbuilt functions
> and interfaces nicely with SciPy, matplotlib, Mayavi2 etc...
> It seems to me (please correct me if I am mistaken) that the situation
> in Haskell is a bit more 'open' to choices.
> At least I think so when I look at
>
> http://en.wikibooks.org/wiki/Haskell/Hierarchical_libraries/Arrays
>
> I may want to drop lists at some point for performance reasons but also
> because in my work I really have tables of numbers I find convenient to
> think of as arrays/matrices (again, loosely speaking, I mean matrices as
> arrays + linear algebra like taking the inverse, the determinant and so on).
> Bottom line, I would need a data type that
> (1) is decently fast (OK, there is more than performance to scripting,
> but you see my point)
> (2) allows slicing/indexing (e.g. take 3rd row, second column, flatten
> it out, take every element larger than 34 in a row, find its unique
> elements, sort them etc...) without having to re-invent the wheel
> myself. This is more on the manipulation side than the linear algebra.
> As you can see, I would like to be able to find something similar to the
> very useful functions in Data.List for an array.
> (3) it would be nice if these data type could have either integers of
> real numbers as entries. If the original data is a made up of integer
> numbers, conversion to real number is always suspicious (I have horrible
> memories (at least in other languages) of numbers that were equal as
> integers and no longer when read as real numbers because of one last
> digit changing...which can give you a headache in some cases.
> (4) again it would be nice if I could feed these arrays/vectors to tools
> that take integrals, derivatives, inverse etc...
>
> I am considering for this reason several possibilities
>
> http://www.haskell.org/tutorial/arrays.html
> http://users.skynet.be/jyp/html/base/Data-Array.html
> http://code.haskell.org/hmatrix/hmatrix.pdf
>
> hmatrix is probably what I am looking for (linear algebra, interface to
> gnuplot for plotting, ODE solver etc...) but there is no possibility of
> using arrays of integers, so I am concerned about using it to read and
> compare data files filled with integers where I check if certain entries
> are equal or not. Also I wonder if I can find any extra documentation
> other than the well written tutorial (which explains a lot, but cannot
> do everything in less than 30 pages and I would have plenty of questions
> about array manipulations there).
>
> Any suggestion/clarification is more than welcome.
> Cheers
>
> Lorenzo
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
More information about the Beginners
mailing list