In Haskell

The list indexing operator in Haskell. It is an infix operator; like others in Haskell, it can be used as a curried function by enclosing it in parentheses. It is defined by the standard Prelude in HUGS as follows:

(!!)               :: [a] -> Int -> a
(x:_)  !! 0         = x
(_:xs) !! n | n > 0 = xs !! (n-1)
(_:_)  !! _         = error "Prelude.!!: negative index"
[]     !! _         = error "Prelude.!!: index too large

It works in the obvious way:

Prelude> ["Zeroth", "First", "Second", "Third"] !! 2
Prelude> "Strings are just lists of Chars, much like C" !! 12
Prelude> "They don't have trailing nulls, unlike C" !! 39
Prelude> "They don't have trailing nulls, unlike C" !! 40
Program error: Prelude.!!: index too large
Prelude> "You can't index backwards, unlike Perl" !! -1
Program error: Prelude.!!: negative index
Prelude> [('a', True), ('b', False)] !! 1
('b', False)

Lists are lists, not arrays, so indexing is O(n). It turns out that indexing a list is not such a common task in Haskell; you tend either to map or fold them instead. If you want O(1) indexing, then you should go wield ! at an Array.


In many shells, !! repeats the previous command. Ta, N-Wing!

In VIM, [count]!!filter filters count lines through the external program filter.