Last active
November 24, 2024 08:42
-
-
Save tttardigrado/4f6a2d5a4cdf1ca4287d8a8195d16d23 to your computer and use it in GitHub Desktop.
P'' implementation in under 30 lines of Haskell
This file contains hidden or 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
data P = R | L | Seq P P | Loop P | |
data Tape = Tape [Int] Int [Int] | |
empty :: Tape | |
empty = Tape (repeat 0) 0 (repeat 0) | |
left :: Tape -> Tape | |
left (Tape (l:ls) v rs) = Tape ls l (v:rs) | |
right :: Tape -> Tape | |
right (Tape ls v (r:rs)) = Tape (v:ls) r rs | |
peek :: Tape -> Int | |
peek (Tape _ v _) = v | |
edit :: (Int -> Int) -> Tape -> Tape | |
edit f (Tape ls v rs) = Tape ls (f v) rs | |
run :: Int -> P -> Tape -> Tape | |
run n R t = right t | |
run n L t = left $ edit ((`mod`n) . (+1)) t | |
run n (Seq p q) t = run n q $ run n p t | |
run n (Loop p) t = if peek t == 0 | |
then t | |
else run n (Seq p $ Loop p) t | |
eval :: P -> Int | |
eval p = peek $ run 256 p empty |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment