aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Lib.hs
diff options
context:
space:
mode:
authorMarvin Borner2023-10-07 17:26:12 +0200
committerMarvin Borner2023-10-07 17:26:12 +0200
commit9753086b7938cb3c452efe768df5188f90350e28 (patch)
tree07743a93229e11d883f3cc6787afb0d34d8d6803 /src/Lib.hs
parent0525f043d62dba0824b44236d4090ece64630828 (diff)
Implemented from blog
Diffstat (limited to 'src/Lib.hs')
-rw-r--r--src/Lib.hs39
1 files changed, 18 insertions, 21 deletions
diff --git a/src/Lib.hs b/src/Lib.hs
index 5d58008..698c0ae 100644
--- a/src/Lib.hs
+++ b/src/Lib.hs
@@ -1,13 +1,15 @@
{-# LANGUAGE LambdaCase #-}
module Lib
- ( fromJotter
+ ( fromJottary
+ , s
+ , k
+ , i
) where
+import Control.Monad ( replicateM )
+import Debug.Trace ( trace )
import Term
-import Utils
-
-data State = L | R
s :: Term
s = Abs (Abs (Abs (App (App (Idx 2) (Idx 0)) (App (Idx 1) (Idx 0)))))
@@ -18,22 +20,17 @@ k = Abs (Abs (Idx 1))
i :: Term
i = Abs (Idx 0)
-clean :: String -> String
-clean (x : xs) | x == '0' || x == '1' = x : clean xs
- | otherwise = clean xs
-clean [] = []
+count :: String -> Int
+count (x : xs) | x == '1' = 1 + count xs
+ | otherwise = count xs
+count [] = 0
--- reverse wouldn't be needed if [0F] instead of [F0]
-fromCleanJotter :: String -> Term
-fromCleanJotter t | even $ length t = go L $ reverse t
- | otherwise = go R $ reverse t
+fromDecimalJottary :: Int -> Term
+fromDecimalJottary p = foldr1 App (reverse (gen !! p) ++ [i])
where
- go L ('0' : xs) = App s (go R xs)
- go R ('0' : xs) = App (go L xs) s
- go L ('1' : xs) = App k (go R xs)
- go R ('1' : xs) = App (go L xs) k
- go _ [] = i
- go _ _ = invalid
-
-fromJotter :: String -> Term
-fromJotter = fromCleanJotter . clean
+ lio = Abs (App (App (Idx 0) s) k)
+ rio = Abs (App s (App k (Idx 0)))
+ gen = [0 ..] >>= (`replicateM` [lio, rio])
+
+fromJottary :: String -> Term
+fromJottary t = trace (show $ count t) (fromDecimalJottary $ count t)