Basic testing weather a number is hamming is straightforward. It can be something like this:

hamming(X) :- X =< 0, !, fail.
hamming(1).
hamming(X) :-0 =:= (X mod 2), !, Y is X // 2, hamming(Y).
hamming(X) :-0 =:= (X mod 3), !, Y is X // 3, hamming(Y).
hamming(X) :-0 =:= (X mod 5), !, Y is X // 5, hamming(Y).

But if want to make an ordered list of of all hamming numbers using lazy algorithm, we should try harder.

See this nice site that compares functional and logical languages for a solution.

His solution uses **dif** predicate to make the evaluation lazy. There are also other predicates for co-routines in Prolog, like **freeze**.

Prolog’s Â **freeze**/**frozen** are similar to scheme’s **promise**/**force**.

### Like this:

Like Loading...

*Related*

Tags: HammingNumbers, lazy, prolog

This entry was posted on 03/23/2009 at 08:04 and is filed under programming. You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.

## Leave a Reply