diff options
author | Marvin Borner | 2024-02-21 20:28:55 +0100 |
---|---|---|
committer | Marvin Borner | 2024-02-21 22:25:09 +0100 |
commit | c9b30f99992c98745d52807f5e45a12f6aee2c5f (patch) | |
tree | 68eaf084eaed2ae515e62fb9c55d52836a327b24 /src/Helper.hs | |
parent | 241315c452b1b06e4b9721cf336d9ab150f7234d (diff) |
Additions for Rosetta Code
Diffstat (limited to 'src/Helper.hs')
-rw-r--r-- | src/Helper.hs | 32 |
1 files changed, 26 insertions, 6 deletions
diff --git a/src/Helper.hs b/src/Helper.hs index 4500de6..50d8b95 100644 --- a/src/Helper.hs +++ b/src/Helper.hs @@ -13,6 +13,7 @@ import Data.Array import qualified Data.BitString as Bit import qualified Data.ByteString.Lazy as Byte import qualified Data.ByteString.Lazy.Char8 as C +import Data.Char import Data.List import qualified Data.Map as M import Data.Maybe ( fromMaybe @@ -313,8 +314,10 @@ matchingFunctions e (Environment env) = maybeHumanifyExpression :: Expression -> Maybe String maybeHumanifyExpression e = unaryToDecimal e + <|> binaryToChar e <|> binaryToDecimal e <|> ternaryToDecimal e + <|> humanifyString e <|> humanifyList e <|> humanifyPair e <|> humanifyMeta e @@ -330,7 +333,7 @@ humanifyMeta e = ("`" <>) <$> go e go (Abstraction (Abstraction (Abstraction (Application (Application (Bruijn 1) a) b)))) = go a >>= \l -> go b >>= \r -> pure $ "(" <> l <> " " <> r <> ")" go (Abstraction (Abstraction (Abstraction (Application (Bruijn 2) n)))) = - unaryToDecimal' n + fmap show (unaryToDecimal' n) go _ = Nothing humanifyList :: Expression -> Maybe String @@ -340,6 +343,12 @@ humanifyList e = do m = map conv es pure $ "{" <> intercalate ", " m <> "}" +humanifyString :: Expression -> Maybe String +humanifyString e = do + es <- unlistify e + str <- mapM binaryToChar' es + pure $ show str + humanifyPair :: Expression -> Maybe String humanifyPair e = do es <- unpairify e @@ -384,12 +393,12 @@ decimalToDeBruijn n | n < 0 = decimalToDeBruijn 0 gen n' = Abstraction $ gen (n' - 1) unaryToDecimal :: Expression -> Maybe String -unaryToDecimal e = (<> "u") <$> unaryToDecimal' e +unaryToDecimal e = (<> "u") . show <$> unaryToDecimal' e -unaryToDecimal' :: Expression -> Maybe String +unaryToDecimal' :: Expression -> Maybe Integer unaryToDecimal' e = do res <- resolve e - return $ show (sum res :: Integer) + return (sum res :: Integer) where multiplier (Bruijn 1) = Just 1 multiplier _ = Nothing @@ -403,9 +412,20 @@ unaryToDecimal' e = do resolve _ = Nothing binaryToDecimal :: Expression -> Maybe String -binaryToDecimal e = do +binaryToDecimal e = (<> "b") . show <$> binaryToDecimal' e + +binaryToChar :: Expression -> Maybe String +binaryToChar e = show <$> binaryToChar' e + +binaryToChar' :: Expression -> Maybe Char +binaryToChar' e = do + n <- binaryToDecimal' e + if n < 255 then Just $ chr $ fromIntegral n else Nothing + +binaryToDecimal' :: Expression -> Maybe Integer +binaryToDecimal' e = do res <- resolve e - return $ show (sum $ zipWith (*) res (iterate (* 2) 1) :: Integer) <> "b" + return (sum $ zipWith (*) res (iterate (* 2) 1) :: Integer) where multiplier (Bruijn 0) = Just 0 multiplier (Bruijn 1) = Just 1 |