Skip to content

Instantly share code, notes, and snippets.

@tttardigrado
Last active November 24, 2024 08:42

Revisions

  1. tttardigrado revised this gist Nov 23, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pll.hs
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ 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 -> Tape
    peek :: Tape -> Int
    peek (Tape _ v _) = v

    edit :: (Int -> Int) -> Tape -> Tape
  2. tttardigrado created this gist Nov 23, 2024.
    29 changes: 29 additions & 0 deletions pll.hs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    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 -> Tape
    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