[Haskell-beginners] How to print the name of a variable
Ozgur Akgun
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
clear enough.
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
seamlessly.
Cheers,
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
> though:
>
> 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)
>
> or
>
> type Named a = (String,a)
>
> Best wishes
>
> Stephen
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
--
Ozgur Akgun
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20100320/2d5d9498/attachment-0001.html
More information about the Beginners
mailing list