Code Snippet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
| #!/usr/bin/env stack
{- stack
--resolver lts-12.0
--install-ghc
script
--ghc-options -Werror
--ghc-options -Wall
--
-}
--------------------------------------------------------------------------------
module Main (main) where
--------------------------------------------------------------------------------
primes :: Integer -> [ Integer ]
primes u =
aux $ 2 : [ 3, 5 .. u ]
where
aux [ ] = [ ]
aux (x:xs) = x : (aux $ filter (\y -> 0 /= y `mod` x) xs)
pprint :: Integer -> Integer -> [Integer] -> [Char]
pprint _ _ [ ] = []
pprint s i (x:xs) =
(if (i `mod` s == 0) then ' ' else y) : pprint s (i + 1) ys
where
(y,ys) =
if i == x
then ('■', xs)
else ('□', x:xs)
chunksOf :: Int -> [a] -> [[a]]
chunksOf _ [] = [ ]
chunksOf n xs = y : (chunksOf n ys)
where
(y,ys) = splitAt n xs
--------------------------------------------------------------------------------
main :: IO ()
main =
mapM_ putStrLn $ chunksOf 80 $ pprint 10 1 $ primes 17530
|
Code Output:
References: