summaryrefslogtreecommitdiff
path: root/Brainfuck/Tape.hs
diff options
context:
space:
mode:
authorJed Barber <jjbarber@y7mail.com>2014-03-26 16:18:57 +1100
committerJed Barber <jjbarber@y7mail.com>2014-03-26 16:18:57 +1100
commit4885bc81e3716841723b585572ff157b4e324198 (patch)
tree7f72999e6d353a1ad88868e5c4239dcaf0ebbceb /Brainfuck/Tape.hs
parentd52939715e33a40b2c59ccfadf417b2752d3229e (diff)
The interpreter looks suspiciously like a turing machine
Diffstat (limited to 'Brainfuck/Tape.hs')
-rw-r--r--Brainfuck/Tape.hs46
1 files changed, 46 insertions, 0 deletions
diff --git a/Brainfuck/Tape.hs b/Brainfuck/Tape.hs
new file mode 100644
index 0000000..8da4352
--- /dev/null
+++ b/Brainfuck/Tape.hs
@@ -0,0 +1,46 @@
+module Brainfuck.Tape (
+ Tape,
+
+ shiftLeft,
+ shiftRight,
+ currentCell,
+ applyToCurrentCell
+ ) where
+
+
+import Control.Monad
+import Data.Maybe
+
+
+
+type Tape a = ([a], Maybe a, [a])
+
+
+
+
+shiftLeft :: Eq a => Tape a -> Tape a
+shiftLeft (x,y,z) =
+ let x' = if (x /= []) then tail x else x
+ y' = if (x /= []) then Just (head x) else Nothing
+ z' = if (isJust y) then (fromJust y):z else z
+ in (x', y', z')
+
+
+
+shiftRight :: Eq a => Tape a -> Tape a
+shiftRight (x,y,z) =
+ let x' = if (isJust y) then (fromJust y):x else x
+ y' = if (z /= []) then Just (head z) else Nothing
+ z' = if (z /= []) then tail z else z
+ in (x', y', z')
+
+
+
+currentCell :: Tape a -> Maybe a
+currentCell (_,c,_) = c
+
+
+
+applyToCurrentCell :: (a -> a) -> Tape a -> Tape a
+applyToCurrentCell f (x,y,z) = (x, (liftM f) y, z)
+