[ Comparing and Sorting | Reference Manual | Alphabetic Index ]

# term_hash(?Term, +Depth, +Range, -Hash)

Computes a hash value for an arbitrary term
Term
An arbitrary term
Depth
An integer
Range
An integer
Hash
A variable or an integer

## Description

This predicate attempts to computes a hash value for an arbitrary term. The computed hash value lies between 0 and Range-1.

The Depth argument specifies the nesting depth of the term up to which the term's components are taken into account for the computation of the hash value. More deeply nested parts of the term will be ignored. If the term contains uninstantiated parts in the portion up to Depth, no reliable hash value can be computed and the predicate succeeds, leaving Hash uninstantiated. If Depth is set to -1, the whole depth of the term will be used for computing the hash value. If Depth is set to 0, the hash value will be 0. The main functor of a term is taken to be at depth 1, its arguments at depth 2 etc.

### Modes and Determinism

• term_hash(?, +, +, -) is det

### Exceptions

(4) instantiation fault
Depth or Range are not instantiated
(5) type error
Depth or Range are not integers

## Examples

```Success:
[eclipse 1]: term_hash(hello, 1, 100, H).
H = 4
yes.

[eclipse 2]: term_hash(world, 1, 100, H).
H = 84
yes.

[eclipse 15]: term_hash(foo(bar,3,4.5), -1, 100, H).
H = 40
yes.

[eclipse 15]: term_hash(foo(bar,3,4.5), 1, 100, H).
H = 72
yes.

[eclipse 18]: term_hash(foo(X,3,4.5), 1, 100, H).
X = X
H = 72
yes.

[eclipse 19]: term_hash(foo(X,3,4.5), 2, 100, H).
X = X
H = H
yes.
```