I would like to create a system that has to keep track of a changing state in order to discover an outcome. The format of the input data is a list of strings that I break down into chars.
For example ['ynynyy','ynynyn',...]
In an imperative style the logic would be as follows:
yCount=0,xCount=0,yTotal=0,nTotal=0
for x in input:
for y in x:
if y is 'y':
yCount++
else:
nCount++
if yCount is 10:
yTotal++
if nCount is 10:
nTotal++
if yCount is 10 or nCount is 10:
yCount=0
nCount=0
The use of mutable variables makes it difficult for me to translate the pattern to Haskell.
I am currently working to try and create a solution using nested functions:
main = do
parseInputData input
parseInputData :: [String] -> IO () --would like to write solution to file
parseInputData x = do
let result = map getSingleValue (lines input)
getSingleValue :: [Char] -> String --would like to return string representation of outcome
getSingleValue (x:xs)
| x == 'y' .. --update state and continue
| x == 'n' .. --update state and continue
| otherwise .. --return final state
I am very rusty and out of practice with Haskell syntax and also functional programming in general. I am aware of the State monad but unsure how to correctly implement it.
Any ideas shared that would get me going in the right direction or help me adjust my train of thought which is a bit stuck in imperative would allow me to produce a solution!
Any help is much appreciated!