Brandon Allbery | 1 Jul 01:04 2011
Picon

Re: Printing the empty list.

On Thu, Jun 30, 2011 at 18:58, Joshua Ball <joshbball <at> gmail.com> wrote:
> GHCi seems to be clever about some things:

GHCi uses extended defaulting rules unless told otherwise, so in the
absence of anything else it uses () as the type.  You can enable this
in GHC as well, with -XExtendedDefaultRules.  See
http://www.haskell.org/ghc/docs/latest/html/users_guide/interactive-evaluation.html#extended-default-rules

--

-- 
brandon s allbery                                      allbery.b <at> gmail.com
wandering unix systems administrator (available)     (412) 475-9364 vm/sms

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Ivan Lazar Miljenovic | 1 Jul 01:05 2011
Picon

Re: Printing the empty list.

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].

> If I write a program in a file and load it in
>
> main = print []
>
> Then I get the ambiguous type variable error that I would expect. Why
> doesn't ghci generate this error at the prompt?

Because ghc doesn't do type defaulting.

--

-- 
Ivan Lazar Miljenovic
Ivan.Miljenovic <at> gmail.com
IvanMiljenovic.wordpress.com
(Continue reading)

Philipp Schneider | 1 Jul 01:47 2011
Picon
Picon

Re: overloading show function

On 06/30/2011 11:46 PM, Holger Siegel wrote:
> Am 30.06.2011 um 22:57 schrieb Philipp Schneider:
>
>> On 06/30/2011 09:49 PM, Holger Siegel wrote:
>>> (...) But that won't work: After you have evaluated an entry of the environment, you store the resulting
value but you throw away its updated environment. That means, you lose the results of all subcomputations
instead of propagating them to all other copies of the environment. Consider the following expression:
>>>
>>> let x = big_computation in let y = x in y + x
>>>
>>> First, big_computation is bound to the name x, resulting in an environment [("x", big_computation)].
Then a closure consisting of this environment together with the right hand side 'x' is bound to the name y.
Now y+x is evaluated: The closure is entered, and from its environment the content of x - a call to
big_computation - is looked up. Now big_computation is evaluated and the result is bound to x in this
environment. After that, this result is also returned as the value of y. But when returning from the
evaluation of y, the environment with the updated value of x is lost and you have to re-evaluate it in order
to calculate x+y!
>> Hello Holger,
>>
>> Can you give me an example of a lambda term in which this would be an issue?
>> Evaluating the following works just fine in my implementation.
>> interp (App (Lam "x" (Add (Var "x") (Var "x"))) big_computation) []
>> When the first variable "x" is evaluated my interp function returns the
>> value and the updated environment. Then to evaluate the second variable
>> the value is just looked up from this environment.
>> Of course in the following big_computation would be evaluated twice
>> (App (Lam "x" (App (Lam "y" (Add (Var "x") (Var "y"))) big_computation))
>> big_computation)
>> But i simply don't have a concept like let x=y.
>
(Continue reading)

Brent Yorgey | 1 Jul 03:35 2011

Re: Printing the empty list.

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.

  Prelude> sum []
  0
  Prelude> :t it
  it :: Integer

-Brent
(Continue reading)

Ivan Lazar Miljenovic | 1 Jul 03:44 2011
Picon

Re: Printing the empty list.

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
(Continue reading)

Eugene Kirpichov | 1 Jul 07:39 2011
Picon

Patterns for processing large but finite streams

Hi,

I'm rewriting timeplot to avoid holding the whole input in memory, and
naturally a problem arises:

How to represent large but finite streams and functions that process
them, returning other streams or some kinds of aggregate values?

Examples:
* Adjacent differences of a stream of numbers
* Given a stream of numbers with times, split it into buckets by time
of given width and produce a stream of (bucket, 50%,75% and 90%
quantiles in this bucket)
* Sum a stream of numbers

Is this, perhaps, what comonads are for? Or iteratees?

--

-- 
Eugene Kirpichov
Principal Engineer, Mirantis Inc. http://www.mirantis.com/
Editor, http://fprog.ru/
Ruohao Li | 1 Jul 07:55 2011
Picon

How to implement the mean function

Hi guys,


I just started learning some Haskell. I want to implement a mean function to compute the mean of a list. The signature of the function is:
mean :: (Num a, Fractional b) => [a] -> b
But when I implement this simple function, the compiler keep whining at me on type errors. I know this is wrong:
mean xs = sum xs / length xs
But how to get it right? Thanks.
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
aditya siram | 1 Jul 08:00 2011
Picon

Re: How to implement the mean function

What compiler errors are you getting?
-deech

On Fri, Jul 1, 2011 at 12:55 AM, Ruohao Li <liruohao <at> gmail.com> wrote:
> Hi guys,
> I just started learning some Haskell. I want to implement a mean function to
> compute the mean of a list. The signature of the function is:
> mean :: (Num a, Fractional b) => [a] -> b
> But when I implement this simple function, the compiler keep whining at me
> on type errors. I know this is wrong:
> mean xs = sum xs / length xs
> But how to get it right? Thanks.
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe <at> haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
Ruohao Li | 1 Jul 08:07 2011
Picon

Re: How to implement the mean function

For mean xs = sum xs / length xs, I got the following:


test.hs:8:10:
    No instance for (Fractional Int)
      arising from a use of `/' at test.hs:8:10-27
    Possible fix: add an instance declaration for (Fractional Int)
    In the expression: sum xs / length xs
    In the definition of `mean': mean xs = sum xs / length xs

test.hs:8:10:
    Couldn't match expected type `b' against inferred type `Int'
      `b' is a rigid type variable bound by
          the type signature for `mean' at test.hs:7:27
    In the expression: sum xs / length xs
    In the definition of `mean': mean xs = sum xs / length xs

test.hs:8:19:
    Couldn't match expected type `a' against inferred type `Int'
      `a' is a rigid type variable bound by
          the type signature for `mean' at test.hs:7:13
    In the second argument of `(/)', namely `length xs'
    In the expression: sum xs / length xs
    In the definition of `mean': mean xs = sum xs / length xs
On Fri, Jul 1, 2011 at 2:00 PM, aditya siram <aditya.siram <at> gmail.com> wrote:
What compiler errors are you getting?
-deech

On Fri, Jul 1, 2011 at 12:55 AM, Ruohao Li <liruohao <at> gmail.com> wrote:
> Hi guys,
> I just started learning some Haskell. I want to implement a mean function to
> compute the mean of a list. The signature of the function is:
> mean :: (Num a, Fractional b) => [a] -> b
> But when I implement this simple function, the compiler keep whining at me
> on type errors. I know this is wrong:
> mean xs = sum xs / length xs
> But how to get it right? Thanks.
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe <at> haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Nathan Howell | 1 Jul 08:07 2011
Picon

Re: How to implement the mean function

(/) operates on a Fractional instance... but length returns an Int, which is not a Fractional.


You can convert the Int to a Fractional instance:
mean xs = sum xs / fromIntegral (length xs)

or try an integer division:
mean xs = sum xs `div` length xs

-n

On Thu, Jun 30, 2011 at 10:55 PM, Ruohao Li <liruohao <at> gmail.com> wrote:
Hi guys,

I just started learning some Haskell. I want to implement a mean function to compute the mean of a list. The signature of the function is:
mean :: (Num a, Fractional b) => [a] -> b
But when I implement this simple function, the compiler keep whining at me on type errors. I know this is wrong:
mean xs = sum xs / length xs
But how to get it right? Thanks.

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Gmane