fork download
  1.  
  2. data FSM state input output = FSM -- Input and output alphabet are constricted at compile time by datatype
  3. { start :: state -- Start state
  4. , trans :: state -> input -> state -- Transition function
  5. , accept :: [state] -- Set of accept states
  6. , out :: OutFunc state input output -- Output function, either Moore or Mealy
  7. }
  8.  
  9. data OutFunc state input output
  10. = Moore (state -> output)
  11. | Mealy (state -> input -> output)
  12.  
  13. runFSM :: Eq state => FSM state input output -> [input] -> (state, Bool)
  14. runFSM (FSM start trans accept out) inputs = (finalState, elem finalState accept)
  15. where
  16. -- finalState = foldl' trans start input
  17. finalState = go inputs start
  18.  
  19. -- Output only changes when state changes occur
  20. --go :: [input] -> state -> state
  21. go [] acc = acc
  22. go (x:xs) currentState = go xs (trans currentState x)
  23.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:2:1: error: unknown type name ‘data’
 data FSM state input output = FSM        -- Input and output alphabet are constricted at compile time by datatype
 ^~~~
prog.c:2:10: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘state’
 data FSM state input output = FSM        -- Input and output alphabet are constricted at compile time by datatype
          ^~~~~
prog.c:2:10: error: unknown type name ‘state’
prog.c:9:1: error: unknown type name ‘data’
 data OutFunc state input output
 ^~~~
prog.c:9:14: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘state’
 data OutFunc state input output
              ^~~~~
prog.c:9:14: error: unknown type name ‘state’
prog.c:16:26: warning: missing terminating ' character
     -- finalState = foldl' trans start input
                          ^
prog.c:16:26: error: missing terminating ' character
     -- finalState = foldl' trans start input
                          ^~~~~~~~~~~~~~~~~~~
stdout
Standard output is empty