Re: [Haskell-cafe] matching constructors

2004-03-08 Thread Vadim Zaliva
On Mar 5, 2004, at 15:48, Vadim Zaliva wrote:

OK, I figured it out. For sake of other novices like me here is what 
you need
to do to make it work.

0. Need to import Data.Generics
1. Compile with '-fglasgow-exts' flag
2. When deriving from Data you also need to derive from Typeable.
It slightly bothers me that this solution seems to be using 
non-standard GHC extensions.

Vadim

On Mar 5, 2004, at 12:41, Brandon Michael Moore wrote:

At the lower level of remimplementing your functions I can suggest a 
few
things.
Brandon,

Thanks for great suggestions! Following them, here is how I redone the 
code:

...

import Data.Maybe
import Data.Either
import Data.Typeable
...

data Flag = Verbose |
Input String  |
Output String |
Filter String
deriving Show Data
instance Eq Flag where
x == y = toConstr x == toConstr y
findFlag :: Flag - [Flag] - Flag
findFlag f fs = fromMaybe f (find (==f) fs)
The only problem is my GHC does not seems to find 'Data' class and I 
am getting following errors:

Type constructor or class not in scope: `Data'
Variable not in scope: `toConstr'
Variable not in scope: `toConstr'
Also I have style question: What is the best way to define equality 
test in this example:

1. Via instantiating EQ class
2. via standalone function (I can define sameConstr Flag - Flag - 
Bool)
3. inline lambda expression passed to find

I am leaning towards #2 or #3.

Vadim

--
La perfection est atteinte non quand il ne reste rien a ajouter, mais
quand il ne reste rien a enlever.  (Antoine de Saint-Exupery)
___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe
--
La perfection est atteinte non quand il ne reste rien a ajouter, mais
quand il ne reste rien a enlever.  (Antoine de Saint-Exupery)


smime.p7s
Description: S/MIME cryptographic signature
___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] matching constructors

2004-03-08 Thread Sven Panne
Vadim Zaliva wrote:
[...] It slightly bothers me that this solution seems to be using non-standard 
GHC extensions.
Hmmm, using generics seems like massive overkill for option handling. Could you
describe what you are exactly trying to achieve?
Cheers,
   S.
___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] matching constructors

2004-03-08 Thread Vadim Zaliva
On Mar 8, 2004, at 11:17, Sven Panne wrote:

Hmmm, using generics seems like massive overkill for option handling. 
Could you
describe what you are exactly trying to achieve?
I am doing command line options parsing. I've defined Flag type with 
constructor
for each possible option:

data Flag = Verbose |
Input String  |
Output String |
Filter String
deriving (Show, Typeable, Data)
getOpt returns me a list of such objects. Now I need to
look things up there by constructor. For example:

doSomething fltflag
where
(Filter fltflag) = findFlag (Filter none) opts
To achieve this I've defined:

instance Eq Flag where
x == y = toConstr x == toConstr y
findFlag :: Flag - [Flag] - Flag
findFlag f fs = fromMaybe f (find (==f) fs)
Sincerely,
Vadim
--
La perfection est atteinte non quand il ne reste rien a ajouter, mais
quand il ne reste rien a enlever.  (Antoine de Saint-Exupery)


smime.p7s
Description: S/MIME cryptographic signature
___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] matching constructors

2004-03-08 Thread Ben Rudiak-Gould
On Mon, 8 Mar 2004, Vadim Zaliva wrote:

 I am doing command line options parsing. I've defined Flag type with 
 constructor
 for each possible option:
 
 data Flag = Verbose |
  Input String  |
  Output String |
  Filter String
  deriving (Show, Typeable, Data)
 
 getOpt returns me a list of such objects. Now I need to
 look things up there by constructor. For example:
 
   
   doSomething fltflag
   where
  (Filter fltflag) = findFlag (Filter none) opts

Try this instead:

doSomething $ option none [fltflag | Filter fltflag - opts]

...

option :: a - [a] - a
option def []  = def
option def [x] = x
option def _   = error Only one of each option allowed


-- Ben

___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] matching constructors

2004-03-08 Thread Vadim Zaliva
On Mar 8, 2004, at 12:55, Ben Rudiak-Gould wrote:

This would work, but I will have to write [] part for each option.
Generics approach is overkill but looks much neater when used.
But thanks for suggestion anyway, it is always good to learn yet
another way of doing things.
Sincerely,
Vadim
Try this instead:
doSomething $ option none [fltflag | Filter fltflag - opts]
option :: a - [a] - a
option def []  = def
option def [x] = x
option def _   = error Only one of each option allowed
--
La perfection est atteinte non quand il ne reste rien a ajouter, mais
quand il ne reste rien a enlever.  (Antoine de Saint-Exupery)


smime.p7s
Description: S/MIME cryptographic signature
___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe