HaskellWiki

Haskell | Wiki community | Recent changes
Random page | Special pages

 

Not logged in
Log in | Help

List notation

Categories: Idioms | Syntax

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.
[1,2,3] ++
4 :
listA ++
5 :
listB ++
[]
infixr 5 ?:, ?++
 
(?:) :: (Bool, a) -> [a] -> [a]
(?:) (b, x) = if b then (x:) else id
 
(?++) :: (Bool, [a]) -> [a] -> [a]
(?++) (b, x) = if b then (x++) else id
 
list =
   [2,3] ++
   (x==5, 5) ?:
   (x==7, listA) ?++
   []


See also:

Retrieved from "http://www.haskell.org/haskellwiki/List_notation"

This page has been accessed 1,484 times. This page was last modified 09:25, 19 May 2007. Recent content is available under a simple permissive license.