[Haskell-beginners] State as function?

martin martin.drautzburg at web.de
Mon Apr 7 18:43:34 UTC 2014


Am 04/06/2014 07:39 PM, schrieb Karl Voelker:
> 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]

Thanks Karl, this tidies up my code quite a bit.
BTW: I named the StringIndexes E6 | A | D | G | B | E1 which reflects the way a guitar is tuned. Way cool.




More information about the Beginners mailing list