[Haskell-beginners] State as function?
Daniel Trstenjak
daniel.trstenjak at gmail.com
Sun Apr 6 14:05:14 UTC 2014
Hi Martin,
On Sun, Apr 06, 2014 at 03:20:14PM +0200, martin wrote:
> I started out with assoc lists and hashmaps. However, I found it
> diffiult to ensure that all 6 GuitarString have a something attached
> and found myself messing with Maybes. In the real world, there is no
> way a GuitarString can have "no state at all", while the HashMap
> approach would provide this option.
I would start by designing the data structures that represent your
possible states. That way you can also ensure that there can't be
any non representable states.
I really don't now what makes sense in your case, but something like:
data StringState = Resting
| Oscillating
...
data Guitar = Guitar
{ string1 :: StringState
, string2 :: StringState
...
}
If you have worked out the right data structures, then writing
the surrounding code can become quite naturally.
Greetings,
Daniel
More information about the Beginners
mailing list