List notation

From HaskellWiki
Revision as of 11:55, 8 November 2006 by Lemming (talk | contribs) (there are cool things possible with bare list syntax)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

We are used to the list notation [0,1,2,3]. However it is syntactic sugar for (0:1:2:3:[]). By using the syntactic sugar, we often miss the benefits of the direct notation.

0 :
1 :
2 :
3 :
[]
Thus it is more theoretically sound and easier to edit.
  • You can easily mix elements and lists into a list by appending the corresponding operator in each line:
[1,2,3] ++
4 :
listA ++
5 :
listB ++
[]
  • You can construct a singleton list with a section of the colon operator:
    (:[]) :: a -> [a]
    
    .
  • You can prepend an element to a list:
    (x:) :: [a] -> [a]
    
    . E.g.
    iterate (' ':) []
    
    creates a list of blank strings with increasing size very efficiently.


See also: