aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Helper.hs
diff options
context:
space:
mode:
authorMarvin Borner2024-04-15 17:12:46 +0200
committerMarvin Borner2024-04-15 17:13:13 +0200
commitbfdd0ef6897e42e03cefa4d43c8757ef09d96de2 (patch)
tree253509bc1a1ed34afc4a128952401921197fae7a /src/Helper.hs
parent32f0891053cb9efe7b2987638a96395d2b9f9722 (diff)
Added parsing and printing of complex numbers
Diffstat (limited to 'src/Helper.hs')
-rw-r--r--src/Helper.hs51
1 files changed, 44 insertions, 7 deletions
diff --git a/src/Helper.hs b/src/Helper.hs
index 8ea5600..921604c 100644
--- a/src/Helper.hs
+++ b/src/Helper.hs
@@ -344,7 +344,9 @@ maybeHumanifyExpression e =
<|> binaryToChar e
<|> binaryToString e
<|> ternaryToString e
- <|> rationalToFloat e
+ <|> rationalToString e
+ <|> realToString e
+ <|> complexToString e
<|> humanifyString e
<|> humanifyList e
<|> humanifyPair e
@@ -398,8 +400,9 @@ floatToRational f = Abstraction
floatToReal :: Rational -> Expression
floatToReal = Abstraction . floatToRational
-floatToComplex :: Rational -> Expression
-floatToComplex f = Bruijn 0
+floatToComplex :: Rational -> Rational -> Expression
+floatToComplex r i = Abstraction
+ $ Application (Application (Bruijn 0) (floatToReal r)) (floatToReal i)
-- Dec to Bal3 in Bruijn encoding: reversed application with 0=>0; 1=>1; T=>2; end=>3
-- e.g. 0=0=[[[[3]]]]; 2=1T=[[[[2 (1 3)]]]] -5=T11=[[[[1 (1 (2 3))]]]]
@@ -504,11 +507,10 @@ ternaryToDecimal e = do
resolve' n
resolve _ = Nothing
-rationalToFloat :: Expression -> Maybe String
-rationalToFloat (Abstraction (Application (Application (Bruijn 0) a) b)) = do
+rationalToString :: Expression -> Maybe String
+rationalToString (Abstraction (Application (Application (Bruijn 0) a) b)) = do
n <- ternaryToDecimal a
d <- ternaryToDecimal b
- -- let (h, r) = properFraction (n % (d + 1))
Just
$ show n
<> "/"
@@ -519,4 +521,39 @@ rationalToFloat (Abstraction (Application (Application (Bruijn 0) a) b)) = do
""
)
<> ")"
-rationalToFloat _ = Nothing
+rationalToString _ = Nothing
+
+realToString :: Expression -> Maybe String
+realToString (Abstraction e) = rationalToString e
+realToString _ = Nothing
+
+complexToString :: Expression -> Maybe String
+complexToString (Abstraction (Application (Application (Bruijn 0) (Abstraction (Abstraction (Application (Application (Bruijn 0) lr) rr)))) (Abstraction (Abstraction (Application (Application (Bruijn 0) li) ri)))))
+ = do
+ nlr <- ternaryToDecimal lr
+ drr <- ternaryToDecimal rr
+ nli <- ternaryToDecimal li
+ dri <- ternaryToDecimal ri
+ Just
+ $ show nlr
+ <> "/"
+ <> show (drr + 1)
+ <> " + "
+ <> show nli
+ <> "/"
+ <> show (dri + 1)
+ <> "i"
+ <> " (approx. "
+ <> (showFFloatAlt
+ (Just 8)
+ ((fromIntegral nlr) / (fromIntegral $ drr + 1) :: Double)
+ ""
+ )
+ <> "+"
+ <> (showFFloatAlt
+ (Just 8)
+ ((fromIntegral nli) / (fromIntegral $ dri + 1) :: Double)
+ ""
+ )
+ <> "i)"
+complexToString _ = Nothing