-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
module Main (main) where | ||
|
||
import ApplyMerge.MergeAll (applyMerge) | ||
|
||
import Data.List.Ordered (minus) | ||
import Data.Text qualified as Text | ||
import Data.Ix (range) | ||
import Data.Vector.Unboxed qualified as Vector | ||
import Data.Array qualified as Array | ||
import Data.Array (Array, (!)) | ||
|
||
import Data.List (intersperse) | ||
import Data.Function ((&)) | ||
import Data.Semigroup (Arg (..)) | ||
import Data.Text (Text) | ||
|
||
primes :: [Int] | ||
primes = 2 : 3 : 5 : ([7..] `minus` composites) | ||
|
||
composites :: [Int] | ||
composites = applyMerge (\i p -> p * (p + i)) [0..] primes | ||
|
||
visualize' | ||
:: forall c. (Num c, Ord c) => (Int -> Int -> c) -> [Int] -> [Int] -> Int -> Double -> String | ||
visualize' f xs ys n r = | ||
let f' :: (Int, Int) -> (Int, Int) -> Arg c (Int, Int) | ||
f' (x, a) (y, b) = Arg (f a b) (x, y) | ||
|
||
firstHalf :: [(Int, Int)] | ||
secondHalf :: [(Int, Int)] | ||
(firstHalf, secondHalf) = | ||
applyMerge f' (zip [1..n] xs) (zip [1..n] ys) | ||
& map (\(Arg _ y) -> y) | ||
& (\xs -> splitAt (scale (length xs) r) xs) | ||
arr :: Array (Int, Int) Char | ||
arr = Array.array ((1, 1), (n, n)) $ | ||
map (, ' ') (range ((1, 1), (n, n))) | ||
++ map (, '.') firstHalf | ||
++ map (, '*') secondHalf | ||
mkRow :: Int -> [Char] | ||
mkRow row = | ||
map (\col -> arr ! (row, col)) [1..n] | ||
& intersperse ' ' | ||
|
||
in unlines (map mkRow [1..n]) | ||
|
||
visualize | ||
:: forall c. (Num c, Ord c) => (Int -> Int -> c) -> Int -> Double -> String | ||
visualize f n r = | ||
let f' :: Int -> Int -> Arg c (Int, Int) | ||
f' a b = Arg (f a b) (a, b) | ||
|
||
firstHalf :: [(Int, Int)] | ||
secondHalf :: [(Int, Int)] | ||
(firstHalf, secondHalf) = | ||
applyMerge f' [1..n] [1..n] | ||
& map (\(Arg _ y) -> y) | ||
& (\xs -> splitAt (scale (length xs) r) xs) | ||
arr :: Array (Int, Int) Char | ||
arr = Array.array ((1, 1), (n, n)) $ | ||
map (, ' ') (range ((1, 1), (n, n))) | ||
++ map (, '.') firstHalf | ||
++ map (, '*') secondHalf | ||
mkRow :: Int -> [Char] | ||
mkRow row = | ||
map (\col -> arr ! (row, col)) [1..n] | ||
& intersperse ' ' | ||
|
||
in unlines (map mkRow [1..n]) | ||
|
||
scale :: Int -> Double -> Int | ||
scale n r = round (fromIntegral n * r) | ||
|
||
takeHalf :: [a] -> [a] | ||
takeHalf xs = take (length xs `quot` 2) xs | ||
|
||
|
||
main :: IO () | ||
main = putStrLn "Hello, World!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters