[Haskell-beginners] State as function?

Karl Voelker karl at karlv.net
Sun Apr 6 17:39:25 UTC 2014


On Sun, Apr 6, 2014, at 09:28 AM, martin wrote:
> The problem with this approach is that I end up with two representations
> of GuitarString. One is the accessor functions
> above, but there is also a datatype GuitarString which I need to express
> things like "put your finger on the nth fret of
> the mth string".

You could try using an array:

data StringIndex = S1 | S2 | S3 | S4 | S5 | S6 deriving (Eq, Ord, Enum,
Bounded, Read, Show)

type Guitar = Array StringIndex StringState

http://hackage.haskell.org/package/array-0.5.0.0/docs/Data-Array.html

The Enum and Bounded constraints on StringIndex let you do fun things
like:

allStrings = [minBound .. maxBound]

-Karl


More information about the Beginners mailing list