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

  1. % Programming using streams in Life
  2. % The algorithm is from Shapiro's survey article on Concurrent Logic
  3. % Programming languages.
  4. % Simple query: A=primes(100)?
  5. % Query showing streams: A=nsift([2,3,4,5|L])?  (This will residuate on L)
  6.  
  7. % Create a stream of integers.
  8. integers(From,To) -> cond(From>To, [], [From|integers(From+1,To)]).
  9.  
  10. % Remove multiples of P.
  11. filter([]) -> [].
  12. filter([X|In],P) -> cond((X mod P =\= 0), [X|filter(In,P)], filter(In,P)).
  13.  
  14. % Filter out multiples of each element.
  15. sift([]) -> [].
  16. sift([P|Ns],Max) -> [P|sift(cond(P=<Max,filter(Ns,P),Ns),Max)].
  17.  
  18. % Create a list of primes.
  19. primes(N) -> sift(integers(2,N),sqrt(N)).
  20.  
  21. % Naive version (from the article):
  22. % No maximum sift value; 7x slower for primes(1000)
  23. nsift([]) -> [].
  24. nsift([P|Ns]) -> [P|nsift(filter(Ns,P))].
  25.  
  26. nprimes(N) -> nsift(integers(2,N)).
  27.  
  28.