[Haskell] How to implement A-lists and P-lists in Haskell (cont.)

Tom Jordan viresh53 at gmail.com
Wed May 26 18:31:45 EDT 2010


That could definitely work if all the fields could be specified
beforehand..
There are some situations where I won't know which field (out of many) will
be needed to use, and they will change often,
but I think I can finalize the superset of all-possible fields to be used,
and set up a "master" record for them..  I'm working on music, so the
parameters/fields change often and are used in many different combinations.
I'm working on an Algorithmic Composition environment where the basic
functionality is written in Ruby, and the meta-level composition/performance
instructions are controlled from Haskell..
It seeming to be a great combination !

Thanks so much for the help, I'll be posting questions in the Haskell Cafe
from now on, but will still be monitoring this list.

Cheers,

Tom



On Wed, May 26, 2010 at 5:50 PM, Philip Weaver <philip.weaver at gmail.com>wrote:

> (in gmail, I just use reply all).
>
> Yeah, that's certainly a valid solution, but ugly like you said.
>
> Would a record like this work better for you?
>
> data Foo = { root :: Root, oct :: Oct, mode :: Mode }
>
> It accomplishes the same thing.  Each field can have at most one value, you
> can update any field, and you can leave any fields uninitialized like this:
>
> initFoo = Foo { root = error "root is not initialized",
>                 oct  = error "oct is not initialized",
>                 mode = error "mode is not initialized"
>               }
>
> On Wed, May 26, 2010 at 1:35 PM, Tom Jordan <viresh53 at gmail.com> wrote:
>
>> Sorry that I couldn't respond to the previous thread.. I'm using Gmail and
>> not an email client so I couldn't use the reply button. I'll try to resolve
>> this issue soon !
>>
>> Thanks Phillip for responding so quickly,
>>
>> I tried Data.Map first and ran into the issue you mentioned..
>>
>> I've found a way to get around it, but it's ugly and verbose..
>>
>> Instead, I'm wondering if there is a way to use the constructors for the
>> "Val" types below, in place of the keys such as:
>>
>> data Val = Root Root | Oct Oct | Mode Mode deriving (Show, Eq, Ord)
>>
>> And matching against the constructors, instead of resorting to this:
>>
>>
>> import IO
>> import qualified Data.Map as Map
>>
>> type Root = String
>> type Oct  = Integer
>> type Mode = Integer
>>
>> data Key = Root | Oct | Mode deriving (Show, Eq, Ord)
>> data Val = Root_v Root | Oct_v Oct | Mode_v Mode deriving (Show, Eq, Ord)
>>
>> test = Map.insert Oct (Oct_v 3) . Map.insert Mode (Mode_v 0) . Map.insert
>> Root (Root_v "2") $ Map.empty
>>
>> main = print test
>>
>> --  ---->  fromList [(Root,Root_v "2"),(Oct,Oct_v 3),(Mode,Mode_v 0)]
>>
>>
>> Many Thanks,
>>
>> Tom
>>
>> _______________________________________________
>> Haskell mailing list
>> Haskell at haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell/attachments/20100526/7c10eefa/attachment.html


More information about the Haskell mailing list