How to implement A-lists and P-lists in Haskell (cont.)
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
(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@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@haskell.org http://www.haskell.org/mailman/listinfo/haskell
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@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@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@haskell.org http://www.haskell.org/mailman/listinfo/haskell
Thanks for the info Yitz.. I'm psyched to dive in deep into Haskell, so the Haskell Cafe sounds best, although I'll probably sign up for both.. don't want to miss out on anything !
btw- If you can move this thread.. please move it to the Haskell Cafe.. thanks :)) On Wed, May 26, 2010 at 6:35 PM, Tom Jordan <viresh53@gmail.com> wrote:
Thanks for the info Yitz.. I'm psyched to dive in deep into Haskell, so the Haskell Cafe sounds best, although I'll probably sign up for both.. don't want to miss out on anything !
Tom Jordan wrote:
I tried Data.Map first and ran into the issue you mentioned...
Tom, could we please move this discussion to either the Haskell Cafe or the Haskell Beginners mailing list? (The choice basically depends on whether you mind if the discussion might go off into deeper waters.) This list is only for announcements or other very short threads. Please let us know which list you'd like to move to. Thanks, Yitz
participants (3)
-
Philip Weaver -
Tom Jordan -
Yitzchak Gale