From 5e987e6d9261a70597757d49935966ee1a9b464a Mon Sep 17 00:00:00 2001 From: left_adjoint Date: Wed, 27 Mar 2024 17:33:12 -0700 Subject: [PATCH] First steps towards a thing that will generate a randomized zine --- recombiner1.hs | 44 ++++++++++++++++++++++++++++++++++++++++++++ test.txt | 21 +++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 recombiner1.hs create mode 100644 test.txt diff --git a/recombiner1.hs b/recombiner1.hs new file mode 100644 index 0000000..394b26c --- /dev/null +++ b/recombiner1.hs @@ -0,0 +1,44 @@ +import System.IO +import System.Random + +-- we're starting with a file where sections are separated by +-- begin-section +-- end-section + +collectSections :: [String] -> [String] +collectSections strs = collectSections' strs [] + +-- if we don't close an accumulation we just throw it away +-- we also don't check for beginning a new section inside a section +collectSections' [] accum = [] +collectSections' (x:xs) accum | x == "begin-section" = collectSections' xs [] + | x == "end-section" = (unlines $ reverse accum) : collectSections' xs [] + | otherwise = collectSections' xs (x : accum) + +parseFile :: String -> [String] +parseFile = collectSections . lines + +-- this function shouldn't just split the lists at the point +-- but grab the element at that point +-- [0,1,2,4] +-- this function is only defined for 1 +splitAtElem n xs = let (xl,xr) = splitAt n xs in (xl, head xr, tail xr) + +randomizeSections :: (RandomGen g) => g -> [String] -> [String] +randomizeSections g [] = [] +randomizeSections g [x] = [x] +randomizeSections g xs = let (n,g') = uniformR (0,length xs-1) g -- + (l,x,r) = splitAtElem n xs + in x : randomizeSections g' (l ++ r) + + +-- small testing main function, to replace +main :: IO () +main = do + contents <- readFile "test.txt" + g <- initStdGen + let parsed = parseFile contents + putStrLn "Initial" + putStrLn $ unlines $ parsed + putStrLn "Randomized" + putStrLn $ unlines $ randomizeSections g $ parsed diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..141e5cf --- /dev/null +++ b/test.txt @@ -0,0 +1,21 @@ +begin-section +Here's some text that should be an independent unit +end-section + +begin-section +This is some more text. + +We want to make sure it all shows up with proper linebreaks and + +everything +end-section + + +this text should get completely ignored + +begin-section +Hi there +end-section + + +