Created
May 31, 2021 09:50
-
-
Save ObjectBoxPC/4a554a15d8d2b13168e25d65b97e657a to your computer and use it in GitHub Desktop.
Programs to transform text strings
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
import StrConv | |
fullWidth c | |
| c >= 0x21 && c <= 0x7E = Just $ c + 0xFEE0 -- ASCII printable | |
| c == 0x20 = Just 0x3000 -- ASCII space -> ideographic space | |
| otherwise = Nothing | |
main = strConv (charify fullWidth) |
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
import StrConv | |
regional c | |
| c >= 0x41 && c <= 0x5A = Just $ c + 0x1F1A5 -- Uppercase | |
| c >= 0x61 && c <= 0x7A = Just $ c + 0x1F185 -- Lowercase | |
| otherwise = Nothing | |
main = strConv (charify regional) |
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 StrConv (strConv, charify) where | |
import System.Environment | |
import System.Exit | |
import Data.Char | |
convertStr convertChar = map $ \c -> case convertChar c of | |
Just converted -> converted | |
_ -> c | |
processArgs args | |
| length args == 0 = putStrLn "Not enough arguments" >> return Nothing | |
| length args == 1 = return (Just $ args !! 0) | |
| otherwise = | |
putStrLn "Warning: Only the first argument will be converted" | |
>> return (Just $ args !! 0) | |
writeArg convertChar argResult = case argResult of | |
Just arg -> writeConverted >> exitSuccess | |
where writeConverted = putStrLn $ convertStr convertChar arg | |
Nothing -> exitFailure | |
strConv :: (Char -> Maybe Char) -> IO () | |
strConv convertChar = getArgs >>= processArgs >>= writeArg convertChar | |
charify :: (Int -> Maybe Int) -> Char -> Maybe Char | |
charify convert = (fmap chr) . convert . ord |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment