{- Make text match the capitalization, punctuation, and spacing of the 1st amendment. Copyright 2016 Ken Takusagawa This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . -} -- originally from simple-cipher/cipher.ll {-# LANGUAGE ScopedTypeVariables, LambdaCase #-} module Main(main) where { import Control.Category((>>>)); import Prelude hiding((>>)); import Data.List; import Data.Char; import System.Environment(getArgs); main :: IO(); main = getArgs >>= \case{ -- expecting a series of numbers 0..25 to be mapped to letters ["digits"] -> getContents >>= (words >>> map (read >>> numtochar) >>> do_fst_amd >>> putStrLn); _ -> getContents >>= (do_fst_amd >>> putStrLn); }; data Template = Punctuation Char | Modify_case (Char -> Char); -- no Show derivable because function one_template :: [Template] -> String -> Maybe (Char,([Template],String)); one_template [] [] = Nothing; one_template (Punctuation p:trest) s = Just (p,(trest,s)); -- final punctuation gets in one_template _ [] = Nothing; one_template (Modify_case f:trest) (s1:rest) = Just (f s1, (trest,rest)); one_template [] _ = error "ran out of template"; -- should have used cycle apply_template :: [Template] -> String -> String; apply_template t s = unfoldr (uncurry one_template) (cycle t,s); to_template :: Char -> Template; to_template c = if isAlpha c then Modify_case (if isUpper c then toUpper else toLower) else Punctuation c; --- Filler to so the text is unchanged when the program is run on --- itself for fun: uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu --- uuuuuuuuuuuuuuuuuuuuuuuuuuuu first_amendment :: String; first_amendment = "Congress shall make no law respecting an establishment of religion, or prohibiting the free exercise thereof; or abridging the freedom of speech, or of the press; or the right of the people peaceably to assemble, and to petition the Government for a redress of grievances. "; -- spaces needed at the end for cycle --- 222 alpha characters do_fst_amd :: String -> String; do_fst_amd = filter isAlpha >>> apply_template (map to_template first_amendment); numtochar :: Integer -> Char; numtochar = fromInteger >>> (+) (fromEnum 'a') >>> toEnum; } --end