home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / examples / lexyacc / cl / sieve.cl < prev    next >
Encoding:
Text File  |  1997-08-18  |  726 b   |  25 lines  |  [TEXT/R*ch]

  1. (* Stolen and edited from Klaus Elmquist Nielsen, Copyright (c) 1993 *)
  2.  
  3. letrec
  4.    sieve = \xs.case xs of
  5.                <1>      -> pack {1} ;
  6.                <2> x xs -> pack {2, x, sieve (filter (\n.(n % x) ~= 0) xs)}
  7.                end ;
  8.  
  9.    take = \n.\xs.case xs of
  10.                  <1>      -> pack {1} ;
  11.                  <2> x xr -> if n=0 then pack {1}
  12.                              else pack {2, x, take (n-1) xr}
  13.                  end;
  14.  
  15.    filter = \p.\xs.case xs of
  16.                    <1>      -> pack {1} ;
  17.                    <2> x xs -> if p x then pack {2, x, filter p xs}
  18.                                else filter p xs
  19.                    end ;
  20.  
  21.    from = \n.pack {2, n, from (n+1)}
  22.  
  23. in take 300 (sieve (from 2))
  24.  
  25.