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

Philip Weaver philip.weaver at gmail.com
Wed May 26 17:50:13 EDT 2010


(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/934d768e/attachment.html


More information about the Haskell mailing list