aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCamil Staps2015-09-03 23:00:48 +0200
committerCamil Staps2015-09-03 23:00:48 +0200
commit72f3db5e79d69ecb4acf9064c863f0914b8441b3 (patch)
tree6f779b82f93179cbe44f68665f557e0d753f0c64
parentAdditions: 9.3,9.4,9.5,9.6; CleanTuringMachines; fixes (diff)
Clean exercises
-rw-r--r--exercises.icl119
1 files changed, 119 insertions, 0 deletions
diff --git a/exercises.icl b/exercises.icl
new file mode 100644
index 0000000..f0f583c
--- /dev/null
+++ b/exercises.icl
@@ -0,0 +1,119 @@
+module exercises
+
+import StdEnv
+import TuringMachines
+
+Start :: *World -> *World
+Start w
+# (io,w) = stdio w
+# m = initTuringMachine ex_9_4 tape_9_4
+# io = fwrites (toString m +++ "\n") io
+# io = loop m io
+# (ok,w) = fclose io w
+| not ok = abort "Couldn't close stdio\n"
+| otherwise = w
+where
+ loop m f
+ # m = step m
+ # f = fwrites (toString m +++ "\n") f
+ | m.running == Running = loop m f
+ | otherwise = f
+
+tape_9_3_b = [Just c \\ c <- fromString "abbaba"]
+ex_9_3_b :: TuringMachine Char
+ex_9_3_b = { alphabet = ['a', 'b', 'X', 'Y'],
+ inputs = ['a', 'b'],
+ transition = f }
+where
+ f :: Int (Maybe Char) -> TuringMachineMove Char
+ f 0 Nothing = Step 1 Nothing Right
+
+ f 1 (Just 'a') = Step 1 (Just 'a') Right
+ f 1 (Just 'b') = Step 1 (Just 'b') Right
+ f 1 c = Step 2 c Left
+
+ f 2 Nothing = Step 6 Nothing Right
+ f 2 (Just 'a') = Step 3 (Just 'X') Right
+ f 2 (Just 'b') = Step 4 (Just 'Y') Right
+
+ f 3 (Just c) = Step 3 (Just c) Right
+ f 3 Nothing = Step 5 (Just 'a') Left
+
+ f 4 (Just c) = Step 4 (Just c) Right
+ f 4 Nothing = Step 5 (Just 'b') Left
+
+ f 5 (Just c) = Step 5 (Just c) Left
+ f 5 Nothing = Step 1 Nothing Right
+
+ f 6 (Just 'X') = Step 6 (Just 'a') Right
+ f 6 (Just 'Y') = Step 6 (Just 'b') Right
+ f 6 (Just c) = Step 6 (Just c) Left
+
+ f _ _ = Halt
+
+tape_9_3_c = [Just c \\ c <- fromString "abbaa"]
+ex_9_3_c :: TuringMachine Char
+ex_9_3_c = { alphabet = ['a', 'b', 'X'],
+ inputs = ['a', 'b'],
+ transition = f }
+where
+ f :: Int (Maybe Char) -> TuringMachineMove Char
+ f 0 Nothing = Step 1 Nothing Right
+
+ f 1 (Just 'a') = Step 1 (Just 'a') Right
+ f 1 (Just 'b') = Step 1 (Just 'b') Right
+ f 1 (Just 'X') = Step 2 (Just 'X') Left
+ f 1 Nothing = Step 2 (Just 'X') Left
+
+ f 2 (Just 'a') = Step 3 (Just 'X') Right
+ f 2 (Just 'b') = Step 4 (Just 'X') Right
+ f 2 Nothing = Step 5 Nothing Left
+
+ f 3 (Just 'X') = Step 2 (Just 'a') Right
+ f 3 Nothing = Step 2 (Just 'a') Right
+
+ f 4 (Just 'X') = Step 2 (Just 'b') Right
+ f 4 Nothing = Step 2 (Just 'b') Right
+
+ f 5 (Just 'a') = Step 6 (Just 'a') Left
+ f 5 (Just 'b') = Step 6 (Just 'b') Left
+
+ f 6 (Just 'X') = Step 5 (Just 'X') Left
+ f 6 (Just 'a') = Step 2 (Just 'a') Right
+ f 6 (Just 'b') = Step 2 (Just 'b') Right
+ f 6 Nothing = Step 7 Nothing Right
+
+ f 7 (Just c) = Step 7 (Just c) Right
+ f 7 Nothing = Step 8 Nothing Left
+
+ f 8 (Just 'X') = Step 8 Nothing Left
+ f 8 (Just c) = Step 8 (Just c) Left
+
+ f _ _ = Halt
+
+tape_9_4 = [Just c \\ c <- fromString "babaabbaaacabababb"]
+ex_9_4 :: TuringMachine Char
+ex_9_4 = { alphabet = ['a', 'b', 'c'],
+ inputs = ['a', 'b', 'c'],
+ transition = f }
+where
+ f :: Int (Maybe Char) -> TuringMachineMove Char
+ f 0 Nothing = Step 1 Nothing Right
+
+ f 1 (Just 'a') = Step 2 (Just 'a') Right
+ f 1 c = Step 1 c Right
+
+ f 2 (Just 'a') = Step 3 (Just 'a') Right
+ f 2 c = Step 1 c Right
+
+ f 3 (Just 'a') = Step 4 (Just 'a') Right
+ f 3 c = Step 1 c Right
+
+ f 4 (Just 'a') = Step 4 (Just 'a') Right
+ f 4 (Just 'c') = Step 5 (Just 'c') Left
+ f 4 c = Step 1 c Right
+
+ f 5 (Just c) = Step 5 (Just c) Left
+
+ f _ _ = Halt
+