Created
January 2, 2021 16:10
-
-
Save YellowOnion/9f52bd9ab9a2a0ce6d346a91029e9700 to your computer and use it in GitHub Desktop.
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
module Main (makeCards, main) where | |
import Data.List | |
import System.IO (withFile, hSetEncoding, utf8, hSetFileSize, IOMode(WriteMode), hPutStr) | |
import Text.Printf (printf) | |
-- | Make Cards from a function, and a format string for itself and it's inverse and a range of numbers. | |
-- Examples: | |
-- | |
-- >>> makeCards "%i + %i; %i\n" (+) "%i - %i; %i\n" 1 1 | |
-- "1 + 1; 2\n2 - 1; 1\n2 - 1; 1\n" | |
-- >>> makeCards "%i * %i = %i;" (*) "%i / %i = %i;" 2 3 | |
-- "3 * 3 = 9;2 * 3 = 6;2 * 2 = 4;9 / 3 = 3;6 / 2 = 3;4 / 2 = 2;9 / 3 = 3;6 / 3 = 2;4 / 2 = 2;" | |
makeCards :: String -> (Int -> Int -> Int) -> String -> Int -> Int -> String | |
makeCards opf op iopf start finish = popf ++ piopf | |
where | |
popf = foldr (\[a, b] s -> s ++ printf opf a b (op a b)) "" items | |
piopf = foldr (\[a, b] s -> s ++ printf iopf (op a b) a b) "" items | |
items = nub $ (\a b -> sort [a,b]) <$> [start..finish] <*> [start..finish] | |
writeFile' :: FilePath -> String -> IO () | |
writeFile' file text = withFile file WriteMode $ \f -> do | |
hSetEncoding f utf8 | |
hSetFileSize f 0 | |
hPutStr f text | |
main :: IO () | |
main = do | |
writeFile' "addition_cards.txt" $ makeCards "%i + %i; %i\n" (+) "%i - %i; %i\n" 2 24 | |
writeFile' "multiplication_cards.txt" $ makeCards "%i × %i; %i\n" (*) "%i ÷ %i; %i\n" 2 12 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment