[Haskell-cafe] Printing the empty list.

Ryan Ingram ryani.spam at gmail.com
Fri Jul 1 12:28:42 CEST 2011


Figuring out how to tell what type ghci is defaulting to was an interesting
exercise.  The sum [] trick seemed cool, so I tried a variant:

Prelude> let f xs = const xs $ show xs
Prelude> f []
[]
Prelude> :t it
it :: [()]

  -- ryan


On Thu, Jun 30, 2011 at 6:44 PM, Ivan Lazar Miljenovic <
ivan.miljenovic at gmail.com> wrote:

> On 1 July 2011 11:35, Brent Yorgey <byorgey at seas.upenn.edu> wrote:
> > On Fri, Jul 01, 2011 at 09:05:05AM +1000, Ivan Lazar Miljenovic wrote:
> >> On 1 July 2011 08:58, Joshua Ball <joshbball at gmail.com> wrote:
> >> > GHCi seems to be clever about some things:
> >> >
> >> > If I try to print the empty list in ghci, I encounter no problems:
> >> >
> >> > Prelude> []
> >> > []
> >> > Prelude> show []
> >> > "[]"
> >> > Prelude> print []
> >> > []
> >> >
> >> > Even though the type of the list is clearly unknown, it must be
> >> > picking SOME type. (why does it print [] instead of "")?
> >>
> >> Type defaulting: if you don't specify a type, then ghci makes it
> >> [Integer].
> >
> > In this case I'm pretty sure it is [()] since there is only a Show
> > constraint.  If there were a Num constraint it would pick
> > Integer.
>
> Yeah, I forgot about ()
>
> --
> Ivan Lazar Miljenovic
> Ivan.Miljenovic at gmail.com
> IvanMiljenovic.wordpress.com
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110701/e58481eb/attachment.htm>


More information about the Haskell-Cafe mailing list