[Haskell-cafe] fromInteger for Lists

Paul Keir pkeir at dcs.gla.ac.uk
Sat May 2 12:33:08 EDT 2009

Thanks Andy, et al. I can stop hacking for now then. I'm using a simple fromList function already which seems like a reasonable, and at least semi-standard solution (http://www.haskell.org/hoogle/?hoogle=fromList)

-----Original Message-----
From: sploink88 at gmail.com on behalf of andy morris
Sent: Sat 02/05/2009 00:13
To: Paul Keir
Cc: haskell-cafe at haskell.org
Subject: Re: [Haskell-cafe] fromInteger for Lists[MESSAGE NOT SCANNED]
2009/5/1 Paul Keir <pkeir at dcs.gla.ac.uk>:
> There's nothing better than making a data type an instance of Num. In
> particular, fromInteger is a joy. But how about lists?
> For example, if I have
> data Foo a = F [a]
> I can create a fromInteger such as
> fromInteger i = F [fromInteger i]
> and then a 19::(Foo Int), could become F [19].
> Is it possible to do something similar for lists? So could
> [1,2,3]::(Foo Int) become something slightly different, say,
> F [1,2,3]
> Paul
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe

If you mean what I think you're referring to, you can't. The only
reason it works for integer literals is that the compiler replaces
occurrences of, say, 19 with (fromInteger 19).

There's no function that's automatically applied to list literals, so
([1,2,3] :: Foo Int) isn't able to do anything useful, unfortunately.
However, there's an extension in GHC, OverloadedStrings, which lets
you use the method fromString of class Data.String.IsString to
overload literals. (That's not what you asked, though, I know. :) )

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090502/d832bd2d/attachment.htm

More information about the Haskell-Cafe mailing list