Local data type definitions

Implementing finite state machines in Haskell, I would like to do this:

zigzag :: Int -> Int -> [Int]
zigzag low upp = fsa Incr low

   where
      data State = Incr | Decr

      fsa :: State -> Int -> [Int]
      fsa state n
         = n : case state of
                  Incr | n < upp   -> fsa Incr (n + 1)
                       | otherwise -> fsa Decr (n - 1)

                  Decr | n > low   -> fsa Decr (n - 1)
                       | otherwise -> fsa Incr (n + 1)

      

But I can not:

error: parse error on input ‘data’

      

Is there a better way than to separate the definition State

from zigzag

?


I would not want to code state identifiers implicitly in terms of booleans or numbers if I can use an explicit enum or datatype. The local function definition is intended to be modular to protect the integrity of descriptive namespaces from messy / messy use. I do not understand why data definitions in this regard should be interpreted differently.

+3


source to share


1 answer


You cannot only set local definitions data

at the top level. However, you can use Bool

and if

rather than the custom data type and case

.

Alternatively, you can put the function and data type in your own module if you don't want to export the type data

.



It has been suggested to enable this feature, but for some reason this is not a priority. My guess is that generally people think of types as high-level protocols between top-level functions, and not usually between where

-level functions.

+2


source







All Articles