home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lifeos2.zip / LIFE-1.02 / EXAMPLES / HAMMING.LF < prev    next >
Text File  |  1996-06-04  |  805b  |  31 lines

  1. % Copyright 1992 Digital Equipment Corporation
  2. % All Rights Reserved
  3.  
  4. % Hamming sequence problem in Life.
  5. % Author: Hassan Ait-Kaci.
  6.  
  7. module("hamming") ?
  8. public(hamming) ?
  9.  
  10.  
  11. % Multiple each list element by F, up to a maximum of N.
  12. mult_list(F,N,[H|T]) ->
  13.         cond(R:(F*H) =< N, [R|mult_list(F,N,T)], []).
  14.  
  15. % Merge two lists of ascending integers.
  16. merge(L,[]) -> L.
  17. merge([],L) -> L.
  18. merge(L1:[H1|T1],L2:[H2|T2]) ->
  19.         cond(H1 =:= H2,
  20.              [H1|merge(T1,T2)],
  21.              cond(H1 > H2,
  22.                   [H2|merge(L1,T2)],
  23.                   [H1|merge(T1,L2)])).
  24.  
  25. % Generate the Hamming sequence from 1 to N.
  26. hamming_f(N) ->
  27.         S:[1|merge(mult_list(2,N,S),
  28.                    merge(mult_list(3,N,S),
  29.                          mult_list(5,N,S)))].
  30.  
  31. hamming :- write(hamming_f(1000)), nl.