[Haskell-beginners] How to print the name of a variable
ozgurakgun at gmail.com
Sat Mar 20 08:08:46 EDT 2010
You're right, if you implement the type class independent of the value
you'll end up with associating names with types.
I use it as an interface to your method actually - sorry I didn't make it
So, something like this:
data NamedInt = NamedInt Int String
instance HasName NamedInt where
name (NamedInt _ n) = n
updateName n (NamedInt v _) = NamedInt v n
Now, if you want, you can have a Num instance for Named Int, redirect
everything to the inner-int, and here you go. You can use NamedInt as a Num
On 20 March 2010 11:03, Stephen Tetley <stephen.tetley at gmail.com> wrote:
> Hi Ozgur
> Doesn't that associate names with types rather than names with values
> instance HasName Int where
> name = "Int"
> > printWithName (1::Int)
> Int: 1
> > printWithName (70::Int)
> Int: 70
> I think the same is achievable with Data.Typeable / Data.Data although
> how to do it is somewhat buried...
> For associating names and values you'd still need to do it 'by hand'
> with something like
> data Named a = Named String a
> deriving (Show)
> type Named a = (String,a)
> Best wishes
> Beginners mailing list
> Beginners at haskell.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Beginners