!!

(idea) by ariels Wed Mar 21 2001 at 9:16:54

An idiom in C (and C++, but less so) for translating a zero/nonzero value to 0/1.

int bit_10_set = !! (x & 0x400);
It's a nice exercise to work out why it works.

(thing) by resiak Sun Nov 14 2004 at 12:18:48

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

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.)

Elsewhere

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

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

Y'know, if you log in, you can write something here, or contact authors directly on the site. Create a New User if you don't already have an account.