Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add KMP example

  • Loading branch information...
commit ce4d5d0014672da4dfe848e2e1ccc1ae9ccc1223 1 parent 353c9a3
@batterseapower authored
Showing with 32 additions and 0 deletions.
  1. +2 −0  Core/Parser.hs
  2. +29 −0 examples/toys/KMP.core
  3. +1 −0  examples/toys/KMP.hs
View
2  Core/Parser.hs
@@ -85,6 +85,8 @@ buildWrappers ps
dataConArity "Node" = 3 -- ZipTreeMaps
dataConArity "Wheel1" = 2 -- Wheel-Sieve1
dataConArity "Wheel2" = 3 -- Wheel-Sieve2
+ dataConArity "A" = 0 -- KMP
+ dataConArity "B" = 0 -- KMP
dataConArity s = panic "dataConArity" (text s)
newtype ParseM a = ParseM { unParseM :: ParseState -> (ParseState, a) }
View
29 examples/toys/KMP.core
@@ -0,0 +1,29 @@
+alphabetEq x y = case x of
+ A -> case y of A -> True
+ _ -> False
+ B -> case y of B -> True
+ _ -> False
+
+
+match p s = loop p s p s
+
+loop pp ss op os = case pp of
+ [] -> True
+ (p:pp) -> case ss of [] -> False
+ (s:ss) -> if alphabetEq p s then loop pp ss op os else next op os
+
+next op ss = case ss of
+ [] -> False
+ (_:ss) -> loop op ss op ss
+
+root u = match [A, A, B] u
+
+tests = [
+ (root [], False),
+ (root [A], False),
+ (root [A, A], False),
+ (root [A, A, B], True),
+ (root [A, A, A, B], True),
+ (root [A, A, B, A], True),
+ (root [A, B, A], False)
+ ]
View
1  examples/toys/KMP.hs
@@ -0,0 +1 @@
+data Alphabet = A | B
Please sign in to comment.
Something went wrong with that request. Please try again.