1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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)
|