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
Notice that it is polymorphically typed: this is, of course, essential. It works in the obvious way:
Prelude> ["Zeroth", "First", "Second", "Third"] !! 2
"Second"
Prelude> "Strings are just lists of Chars, much like C" !! 12
'j'
Prelude> "They don't have trailing nulls, unlike C" !! 39
'C'
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> ["List", "of", "lists"] !! 1
"of"
Indexing into an list is a fairly expensive operation (O(n), in fact). If you really must index into large sets of data, some kind of tree would probably be more appropriate. (Then again, if you're worrying about efficiency, you almost certainly know more about trees than I do.)
In many shells, !! repeats the previous command. Ta, N-Wing!
In VIM, [count]!!filter filters count lines through the external program filter.