~cpp
module Slurpys
where
import IO
main = do
putStrLn "slurpys by thom (masterhand at gmail dot com)"
slump string =
if length string == 0
then False
else
if myOr [head string=='D', head string=='E']
then checkSubSlump (tail string)
else False
checkSubSlump string =
if length string == 0
then False
else
if head string == 'F'
then checkSlumpSub (tail string)
else False
checkSlumpSub subString =
-- check empty string
if length subString == 0
then False
else
if head subString == 'F'
then checkSlumpSub (consumeLeadingChars 'F' subString)
-- check there exists 'G' or not.
else if head subString == 'G'
then slumpEndCheck subString
-- if no, check if following string is slump or not.
else slump subString
-- check slump end condtion
slumpEndCheck lastString =
if (length lastString) == 0
then False
else if myAnd [head lastString == 'G', length lastString == 1]
--else if head lastString == 'G'
then True
else slump lastString
-- remove Fs after slump
consumeLeadingChars (filterChar) (toBeFiltered) =
if length toBeFiltered == 0
then []
else
if head toBeFiltered == filterChar
then consumeLeadingChars (filterChar) (tail toBeFiltered)
else toBeFiltered
-- and, or function defined by thom.
myOr booleans = length (filter (\x->x/=False) booleans) > 0
myAnd booleans = length (filter (\x->x/=False) booleans) == length booleans
-- assertion list
slumpTrueAssert = [slump "DFG", slump "EFG", slump "DFFFFFG", slump "DFDFDFDFG", slump "DFEFFFFG"]
slumpFalseAssert = [slump "DFEFF", slump "EFAHG", slump "DEFG", slump "DG", slump "EFFFFDG"]
-- check for slimp
slimp string =
if length string == 0
then False
-- "AH"
else if myAnd [length string/=0, length string==2, head string=='A', tail string=="H"] -- note that type of (tail "string") is string.
then True
-- length ("AB" + slimp "C" > 3)
-- length ("A" + slump "C" > 2 )
-- "AB" + slimp + 'H' or 'A' + slump + 'H'
else if myAnd [length string>2, head string=='A']
then checkSubSlimp string
else False
checkSubSlimp string =
if length string == 0
then False
else if myAnd [length string>1, head string=='A', head (tail string)== 'B']
then checkSlimpInside string
else if myAnd [length string>0, head string=='A']
then checkSlumpInside string
else False
checkSlimpInside string = slimp (tearABC string)
checkSlumpInside string = slump (tearAC string)
-- tear A, B, C from "AB" + slimp + ' C'
tearABC string = reverse (tail (reverse (tail (tail string))))
-- tear A, C from 'A' + slump + ' C'
tearAC string = reverse (tail (reverse (tail string)))
-- assertion list
slimpTrueAssert = [slimp "AH", slimp "ABAHC", slimp "ABABAHCC", slimp "ADFGC", slimp "ADFFFFGC", slimp "ABAEFGCC", slimp "ADFDFGC"]
slimpFalseAssert = [slimp "ABC", slimp "ABAH", slimp "DFGC", slimp "ABABAHC", slimp "SLIMP", slimp "ADGC"]
slurpy string =
--myOr (slurpyTest string)
slurpyTest 2 string
slurpyTest num string =
if num > (length string) -1
then False
else if slurpyLoopTest num string
then True
else slurpyTest (num+1) string
-- check slimp + slump for every sequential combination with given string.
slurpyLoopTest num string =
if myAnd [slimp (take num string), slump (drop num string) ]
then True
else False
slurpyTrueAssert = [slurpy "AHDFG", slurpy "ADFGCDFFFFFG", slurpy "ABAEFGCCDFEFFFFFG"]
slurpyFalseAssert = [slurpy "AHDFGA", slurpy "DFGAH", slurpy "ABABCC"]
-- Is structured programming appropriate for functional programming language like Haskell?