Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: WWAAA... I hate monads (Colin Paul Adams)
2. Re: Re: computation vs function (Daniel Carrera)
3. Help with types (Daniel Carrera)
4. Re: Help with types (Daniel Fischer)
5. generating the set of all finite-valued functions on a
finite space (Erik Quaeghebeur)
6. Re: Help with types (Daniel Carrera)
7. Re: Help with types (aditya siram)
8. Re: Help with types (Jason Dusek)
9. Re: Help with types (Thomas Davie)
----------------------------------------------------------------------
Message: 1
Date: Thu, 23 Apr 2009 10:25:03 +0100
From: Colin Paul Adams <[email protected]>
Subject: Re: [Haskell-beginners] WWAAA... I hate monads
To: Daniel Carrera <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
>>>>> "Daniel" == Daniel Carrera <[email protected]> writes:
Daniel> Sam Martin wrote:
>>> This is excellent:
>>>
>>> http://ertes.de/articles/monads.html
>>
>> Wow. That really is a great tutorial! Suddenly the world
>> becomes clear...
>>
>> Definitely gets my vote as must read material.
Daniel> +1
Daniel> I was very impressed too. And I am not easy to impress
Daniel> when it comes to documentation. I plan to read it a second
Daniel> time to solidify some of the ideas, but on my first
Daniel> reading my understanding of Monads increased by leaps and
Daniel> bounds.
Daniel> Ertugrul deserves to be commended, and this tutorial
Daniel> should be made more prominent on haskell.org.
I think so.
I've read VERY MANY tutorials on monads, and they were all confusing -
except this one.
--
Colin Adams
Preston Lancashire
------------------------------
Message: 2
Date: Thu, 23 Apr 2009 11:32:41 +0200
From: Daniel Carrera <[email protected]>
Subject: Re: [Haskell-beginners] Re: computation vs function
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Ertugrul Soeylemez wrote:
> What I refer to as a 'computation' in the article is actually just a
> value of type 'Monad m => m a'. I have chosen that term, because you
> can apply it to any monad I've seen. As mentioned in section 5, you can
> think of 'Just 3' as being a computation, which results in 3. But it's
> important that this is not a function, but just an independent value.
Thanks.
I think the article would benefit from making the meaning of computation
clearer. The word computation appears in the tutorial 51 times before
section 5. That means that when I tried to go back for a definition of
computation I couldn't find one.
The first place you use the word computation is in the preamble, but
that's not a good place to define terms. The second time you use the
word computation is in section 2 "Motivation". This might be a good
place to define the term. The Motivation section already defines several
terms (referentially transparent, purely functional, etc), so it seems
like a good place to define computation as well.
In section 2 we can't say "Monad => m a" or "Just 3" because the terms
are not introduced yet. Perhaps you could say something like this:
<idea>
The word 'computation' here is not a technical term. I use it to refer
to something that returns a value without taking in any parameters. For
example (m a) is a computation that takes no parameters but returns a
value of type 'a' whereas (a -> b) is a function that takes a parameter
of type 'a' and returns a value of type 'b'. It is important that a
computation is not a function, but an independent value.
</idea>
I think that adding that as the third paragraph in the Motivation
section would be helpful. In addition, in the Preamble, when you use the
word computation, I would add "(see next section)".
Question: Would it be reasonable to say that a computation is a wrapper?
I'm not sure, because I don't know if a computation always results in
the *same* value (you know, side-effects). I know that "Just 3" always
results in 3, but is that universal?
Cheers,
Daniel.
------------------------------
Message: 3
Date: Thu, 23 Apr 2009 14:57:58 +0200
From: Daniel Carrera <[email protected]>
Subject: [Haskell-beginners] Help with types
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Hi,
I want to make a simple "average" function:
average xs = (sum xs)/(length xs)
When I try that on ghci I get the following error:
No instance for (Fractional Int)
arising from a use of `/' at <interactive>:1:17-36
Possible fix: add an instance declaration for (Fractional Int)
In the expression: (sum xs) / (length xs)
In the definition of `average': average xs = (sum xs) / (length xs)
Ok, so 'sum' is of type (Num a => [a] -> a), length is of type ([a] ->
Int) and (/) is of type (Fractional a => a -> a -> a). Can someone
explain how I can "add an instance declaration" to my function so as to
make all these types compatible?
Thanks,
Daniel.
------------------------------
Message: 4
Date: Thu, 23 Apr 2009 15:35:12 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Help with types
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Am Donnerstag 23 April 2009 14:57:58 schrieb Daniel Carrera:
> Hi,
>
> I want to make a simple "average" function:
>
> average xs = (sum xs)/(length xs)
>
> When I try that on ghci I get the following error:
>
> No instance for (Fractional Int)
> arising from a use of `/' at <interactive>:1:17-36
> Possible fix: add an instance declaration for (Fractional Int)
> In the expression: (sum xs) / (length xs)
> In the definition of `average': average xs = (sum xs) / (length xs)
>
> Ok, so 'sum' is of type (Num a => [a] -> a), length is of type ([a] ->
> Int) and (/) is of type (Fractional a => a -> a -> a). Can someone
> explain how I can "add an instance declaration" to my function so as to
> make all these types compatible?
Don't. (you wouldn't add the instance declaration to the function, anyway, but
to your
module)
Try explicitly converting the length to the appropriate type:
average xs = sum xs / fromIntegral (length xs)
will yield a working (albeit inefficient)
average :: Fractional a => [a] -> a
>
> Thanks,
> Daniel.
------------------------------
Message: 5
Date: Thu, 23 Apr 2009 15:45:27 +0200 (CEST)
From: Erik Quaeghebeur <[email protected]>
Subject: [Haskell-beginners] generating the set of all finite-valued
functions on a finite space
To: [email protected]
Message-ID: <[email protected]>
Content-Type: TEXT/PLAIN; format=flowed; charset=US-ASCII
Hi,
I'd like to lazily generate the set of all {-1,0,1}-valued functions on
{'a','b','c'}? How should I best approach this. I was thinking about
generalizing the power set definition
powerset :: [a] -> [[a]]
powerset = filterM (const [True, False])
but clearly don't know enough about filterM and the like to do it this
way.
Erik
------------------------------
Message: 6
Date: Thu, 23 Apr 2009 15:52:00 +0200
From: Daniel Carrera <[email protected]>
Subject: Re: [Haskell-beginners] Help with types
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Daniel Fischer wrote:
> Try explicitly converting the length to the appropriate type:
>
> average xs = sum xs / fromIntegral (length xs)
Thanks. Could you help me understand what's happening?
1. length returns Int.
2. sum returns Num.
3. (/) wants Fractional.
It looks like (/) is happy with Num but doesn't like Int. This surprises
me. I would have thought that Fractional is a kind of Num and Int is a
kind of Fractional, so a function that expects Fractional would be happy
with an Int but maybe not with a Num. But clearly that's not the way it
works.
'fromIntegral' converts Int to Num. So obviously, Num is good and Int is
bad. But I don't really get why.
> will yield a working (albeit inefficient)
> average :: Fractional a => [a] -> a
Why is it inefficient? How would you make it efficient?
Thanks for the help.
Cheers,
Daniel.
------------------------------
Message: 7
Date: Thu, 23 Apr 2009 08:55:51 -0500
From: aditya siram <[email protected]>
Subject: Re: [Haskell-beginners] Help with types
To: Daniel Carrera <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
There is a chapter in Real World Haskell [1] devoted to this exact question
on this exact piece of code.
hth,
-deech
[1] http://book.realworldhaskell.org/read/profiling-and-optimization.html
On Thu, Apr 23, 2009 at 8:52 AM, Daniel Carrera <
[email protected]> wrote:
> Daniel Fischer wrote:
>
>> Try explicitly converting the length to the appropriate type:
>>
>> average xs = sum xs / fromIntegral (length xs)
>>
>
> Thanks. Could you help me understand what's happening?
>
> 1. length returns Int.
> 2. sum returns Num.
> 3. (/) wants Fractional.
>
> It looks like (/) is happy with Num but doesn't like Int. This surprises
> me. I would have thought that Fractional is a kind of Num and Int is a kind
> of Fractional, so a function that expects Fractional would be happy with an
> Int but maybe not with a Num. But clearly that's not the way it works.
>
> 'fromIntegral' converts Int to Num. So obviously, Num is good and Int is
> bad. But I don't really get why.
>
>
> will yield a working (albeit inefficient)
>> average :: Fractional a => [a] -> a
>>
>
> Why is it inefficient? How would you make it efficient?
>
> Thanks for the help.
>
> Cheers,
>
> Daniel.
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20090423/4ba489ef/attachment-0001.htm
------------------------------
Message: 8
Date: Thu, 23 Apr 2009 07:08:10 -0700
From: Jason Dusek <[email protected]>
Subject: Re: [Haskell-beginners] Help with types
To: Daniel Carrera <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
2009/04/23 Daniel Carrera <[email protected]>:
> It looks like (/) is happy with Num but doesn't like Int. This
> surprises me. I would have thought that Fractional is a kind
> of Num and Int is a kind of Fractional, so a function that
> expects Fractional would be happy with an Int but maybe not
> with a Num. But clearly that's not the way it works.
In GHCi, let's have a look:
Prelude> :info Int
data Int = GHC.Types.I# GHC.Prim.Int# -- Defined in GHC.Types
instance Bounded Int -- Defined in GHC.Enum
instance Enum Int -- Defined in GHC.Enum
instance Eq Int -- Defined in GHC.Base
instance Integral Int -- Defined in GHC.Real
instance Num Int -- Defined in GHC.Num
instance Ord Int -- Defined in GHC.Base
instance Read Int -- Defined in GHC.Read
instance Real Int -- Defined in GHC.Real
instance Show Int -- Defined in GHC.Show
So `Int` is not actually a kind of `Fractional` (it's
`Integral`, in fact).
Prelude> :t (/)
(/) :: forall a. (Fractional a) => a -> a -> a
So `(/)` needs some `t` that is in `Fractional`.
> 'fromIntegral' converts Int to Num. So obviously, Num is good and Int is
> bad. But I don't really get why.
Actually, `fromIntegral` takes any type `t` that is in
`Integral` and makes it into some other type in `Num` as
appropriate. You can use `fromIntegral` to go from `Int` to
`Integer` or `Word32`, as well.
To work with `(/)` we need a type that is in `Num` and in
`Fractional`. The compiler defaults these *class constraints*
to select, say `Double`. Then `fromIntegral` -- which can take
any `Num` to any other `Num` -- does the "cast" from `Int` to
`Double` for us.
--
Jason Dusek
------------------------------
Message: 9
Date: Thu, 23 Apr 2009 16:17:07 +0200
From: Thomas Davie <[email protected]>
Subject: Re: [Haskell-beginners] Help with types
To: Daniel Carrera <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes
On 23 Apr 2009, at 15:52, Daniel Carrera wrote:
> Daniel Fischer wrote:
>> Try explicitly converting the length to the appropriate type:
>> average xs = sum xs / fromIntegral (length xs)
>
> Thanks. Could you help me understand what's happening?
>
> 1. length returns Int.
> 2. sum returns Num.
> 3. (/) wants Fractional.
>
> It looks like (/) is happy with Num but doesn't like Int. This
> surprises me. I would have thought that Fractional is a kind of Num
> and Int is a kind of Fractional
Int isn't a Fractional because it can't represent fractional numbers.
Bob
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 10, Issue 24
*****************************************