[Haskell-cafe] Specify array or list size?

Hamilton Richards ham at cs.utexas.edu
Sat May 7 11:09:13 EDT 2005


At 9:24 AM -0400 2005/5/7, Daniel Carrera wrote:
>Hello,
>
>Right now I'm using type declarations like:
>
>f :: Int -> [Int]
>
>So f returns a list of Ints.
>
>Is there a way to tell Haskell that a list or array must have 
>exactly (say) 256 elements? I'd love to have Haskell make sure that 
>the array I build is the correct size.

Well, for starters, lists and arrays are two entirely different 
topics. I've noticed that Haskell newbies sometimes confuse them 
--possibly the use of [] in list types and enumerations triggers an 
unconscious association with [] used in conventional languages for 
array indexing.

It's easy to define a function that constructs a list of a given 
size, but that size is not part of the list's type. As far as I know, 
the last programming language that included arrays' sizes in their 
types was Standard Pascal, and it turned out to be an unmitigated 
disaster. Because array parameters were typed with their sizes, a 
procedure for searching arrays of size 100 could not be used for 
arrays of any other size. Every useful (non-Standard) dialect of 
Pascal provided a way around that restriction, as did Pascal's 
successor, Modula-2, and as far as I know the mistake has not been 
repeated.

As for Haskell arrays, I've been programming in Haskell since the 
early 1990's, and I've never really needed them. Most Haskell 
programmers find lists much more useful, and you'd probably be better 
off concentrating on lists until you encounter a problem in which 
arrays are really needed. When you do, you'll discover that in 
Haskell as in most other languages, an array's size is not part of 
its type.

It's nice to see you taking up Haskell with such enthusiasm. If you 
agree with many of us that Haskell's one of today's best programming 
programming languages, I hope you'll help spread the word.

With best wishes,

--Ham

-- 
------------------------------------------------------------------
Hamilton Richards, PhD           Department of Computer Sciences
Senior Lecturer                  The University of Texas at Austin
512-471-9525                     1 University Station C0500
Taylor Hall 5.138                Austin, Texas 78712-0233
ham at cs.utexas.edu                hrichrds at swbell.net
http://www.cs.utexas.edu/users/ham/richards
------------------------------------------------------------------


More information about the Haskell-Cafe mailing list