aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Helper.hs
diff options
context:
space:
mode:
authorMarvin Borner2023-03-03 17:34:37 +0100
committerMarvin Borner2023-03-03 17:34:37 +0100
commit62d54116907836342b25a2a4a4632621f9abb57a (patch)
tree350b8f23afc903071e71aace821fc1a4ad28e969 /src/Helper.hs
parent737989f1492b400d59a22a5235555b51a23181fa (diff)
Added pair humanificator
Diffstat (limited to 'src/Helper.hs')
-rw-r--r--src/Helper.hs20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/Helper.hs b/src/Helper.hs
index 9bc2fb0..9571c00 100644
--- a/src/Helper.hs
+++ b/src/Helper.hs
@@ -215,6 +215,11 @@ unlistify (Abstraction (Application (Application (Bruijn 0) e) es)) =
(:) <$> Just e <*> (unlistify es)
unlistify _ = Nothing
+unpairify :: Expression -> Maybe [Expression]
+unpairify (Abstraction (Application (Application (Bruijn 0) e1) e2)) =
+ Just (e1 : [e2])
+unpairify _ = Nothing
+
decodeByte :: Expression -> Maybe [Bool]
decodeByte (Abstraction (Abstraction (Abstraction es))) = decodeByte es
decodeByte (Application (Bruijn 0) es) = (:) <$> Just False <*> (decodeByte es)
@@ -261,12 +266,14 @@ matchingFunctions e (Environment env) =
env
-- TODO: Show binary as char if in ascii range (=> + humanify strings)
+-- TODO: Show list as pair if not ending with empty
maybeHumanifyExpression :: Expression -> Maybe String
maybeHumanifyExpression e =
unaryToDecimal e
<|> binaryToDecimal e
<|> ternaryToDecimal e
<|> humanifyList e
+ <|> humanifyPair e
humanifyExpression :: Expression -> String
humanifyExpression e = case maybeHumanifyExpression e of
@@ -280,6 +287,13 @@ humanifyList e = do
m = map conv es
pure $ "{" <> intercalate ", " m <> "}"
+humanifyPair :: Expression -> Maybe String
+humanifyPair e = do
+ es <- unpairify e
+ let conv x = maybe (show x) id (maybeHumanifyExpression x)
+ m = map conv es
+ pure $ "<" <> intercalate " : " m <> ">"
+
---
-- Dec to Bal3 in Bruijn encoding: reversed application with 0=>0; 1=>1; T=>2; end=>3
@@ -359,3 +373,9 @@ ternaryToDecimal e = do
resolve (Abstraction (Abstraction (Abstraction (Abstraction n)))) =
resolve' n
resolve _ = Nothing
+
+huh :: (a -> Bool) -> [a] -> ([a], [a])
+huh f s = (left, right)
+ where
+ (left, right') = break f s
+ right = if null right' then [] else tail right'