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. Problems installing plugins? (Patrick LeBoutillier)
2. Monads (Patrick Lynch)
3. Re: Monads (Patrick LeBoutillier)
4. Re: Monads (Daniel Fischer)
5. Re: Monads (Patrick Lynch)
6. feedback on simplifying my Takusen code (Neil Jensen)
7. Re: Monads (Brent Yorgey)
8. Re: Monads (MAN)
----------------------------------------------------------------------
Message: 1
Date: Wed, 2 Mar 2011 07:50:48 -0500
From: Patrick LeBoutillier <[email protected]>
Subject: [Haskell-beginners] Problems installing plugins?
To: beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi,
I'm trying to install the plugins package on Fedora 14 but I get a
strange error:
[patrickl@fc14x64 /]$ sudo cabal install plugins
Resolving dependencies...
cabal: cannot configure plugins-1.5.1.4. It requires ghc >=6.10
There is no available version of ghc that satisfies >=6.10
[patrickl@fc14x64 /]$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 6.12.1
One thing probably worth mentioning is that my home directory is
shared amongst many servers, some running older Fedora versions (10).
Could something in my ~/.cabal be messed up or incompatible between versions?
Thanks,
Patrick
--
=====================
Patrick LeBoutillier
Rosem?re, Qu?bec, Canada
------------------------------
Message: 2
Date: Wed, 02 Mar 2011 09:03:00 -0500
From: "Patrick Lynch" <[email protected]>
Subject: [Haskell-beginners] Monads
To: <[email protected]>
Message-ID: <F51BEB06B83D428286CA1A20A8AC0923@UserPC>
Content-Type: text/plain; charset="iso-8859-1"
In Learn You a Haskell for Great Good!, the author Miran Lipovaca indicates
that to understand Monads you need to know Functors...
So, I noticed the following:
class Functor f where
fmap::(a->b)->f a->f b
instance Functor [] where
fmap = map
map::(a->b)->[a]->[b]
if [] is substituted for f in the class definition of Functor the following is
obtained
class Functor [] where
fmap::(a->b)->[]a->[]b
my questions are:
1. is this substitution ok?
2. is []a = [a]?
3. is []b = [b]?
if 2. and 3. are valid then the following is obtained:
fmap::(a->b)->[a]->[b]
which is the same as map type and therefore: fmap = map. QED.
Can you please answer questions 2 and 3 above?
Thank you
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110302/6d9df3f8/attachment-0001.htm>
------------------------------
Message: 3
Date: Wed, 2 Mar 2011 09:11:47 -0500
From: Patrick LeBoutillier <[email protected]>
Subject: Re: [Haskell-beginners] Monads
To: Patrick Lynch <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi,
> my questions are:
> ? 1. is this substitution ok?
> ? 2. is []a = [a]?
> ? 3. is []b = [b]?
>
> if 2. and 3. are valid then the following is obtained:
> ? fmap::(a->b)->[a]->[b]
> which is the same as map type and therefore: fmap = map. QED.
>
> Can you please answer questions 2 and 3 above?
Yes, they are equivalent. Actually both forms are valid Haskell:
a :: [Int]
a = [1]
a' :: [] Int
a' = [1]
Patrick
>
> Thank you
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
--
=====================
Patrick LeBoutillier
Rosem?re, Qu?bec, Canada
------------------------------
Message: 4
Date: Wed, 2 Mar 2011 15:22:35 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Monads
To: [email protected]
Message-ID: <[email protected]>
Content-Type: Text/Plain; charset="utf-8"
On Wednesday 02 March 2011 15:03:00, Patrick Lynch wrote:
> In Learn You a Haskell for Great Good!, the author Miran Lipovaca
> indicates that to understand Monads you need to know Functors... So, I
> noticed the following:
>
> class Functor f where
> fmap::(a->b)->f a->f b
>
> instance Functor [] where
> fmap = map
>
> map::(a->b)->[a]->[b]
>
> if [] is substituted for f in the class definition of Functor the
> following is obtained class Functor [] where
> fmap::(a->b)->[]a->[]b
>
> my questions are:
> 1. is this substitution ok?
> 2. is []a = [a]?
> 3. is []b = [b]?
>
> if 2. and 3. are valid then the following is obtained:
> fmap::(a->b)->[a]->[b]
> which is the same as map type and therefore: fmap = map. QED.
>
> Can you please answer questions 2 and 3 above?
>
> Thank you
Yes, that's correct. The list constructor is special ([] isn't valid
Haskell for a type constructor, so it's wired in), it can be used prefix
([] a) or `roundfix' ([a]). Ordinarily, you can use type constructors
prefix or infix (if they take two type arguments), e.g. a -> b = (->) a b;
Either a b = a `Either` b.
------------------------------
Message: 5
Date: Wed, 02 Mar 2011 10:04:38 -0500
From: "Patrick Lynch" <[email protected]>
Subject: Re: [Haskell-beginners] Monads
To: "Daniel Fischer" <[email protected]>,
<[email protected]>
Message-ID: <436A7A22EA164C698E07C4C1D1DF57E5@UserPC>
Content-Type: text/plain; format=flowed; charset=iso-8859-1;
reply-type=original
Daniel,
Thank you...I understand now...
Good day
Ps
I think my problem that I'm facing is that I don't see how the instance
Functor Maybe is derived from the class Functor...
for example,
the author Miran Lipovaca gives the following:
class Functor f where
fmap::(a->b)->f a->f b
if Maybe replaces f:
fmap::(a->b)->Maybe a->Maybe b
I don't see how the instance Functor Maybe is derived [presumably: 'f' is
function (a->b) and not functor 'f' as shown in the class description):
instance Functor Maybe where
fmap f (Just x) = Just (f x)
fmap f Nothing = Nothing
the author's examples [presumably: 'f'' = (++ " HEY GUYS IM INSIDE THE JUST
") and 'x' = " Something serious ." and it associates from the right...]:
ghci > fmap (++ " HEY GUYS IM INSIDE THE JUST ") ( Just " Something
serious .")
Just " Something serious . HEY GUYS IM INSIDE THE JUST "
ghci > fmap (++ " HEY GUYS IM INSIDE THE JUST ") Nothing
Nothing
I can continue on my Monad journey, just using the instance defintion for
Functor Maybe - but it bothers me that I can't see its derivation...
I'd appreciate any advice.
Thank you
----- Original Message -----
From: "Daniel Fischer" <[email protected]>
To: <[email protected]>
Cc: "Patrick Lynch" <[email protected]>
Sent: Wednesday, March 02, 2011 9:22 AM
Subject: Re: [Haskell-beginners] Monads
> On Wednesday 02 March 2011 15:03:00, Patrick Lynch wrote:
>> In Learn You a Haskell for Great Good!, the author Miran Lipovaca
>> indicates that to understand Monads you need to know Functors... So, I
>> noticed the following:
>>
>> class Functor f where
>> fmap::(a->b)->f a->f b
>>
>> instance Functor [] where
>> fmap = map
>>
>> map::(a->b)->[a]->[b]
>>
>> if [] is substituted for f in the class definition of Functor the
>> following is obtained class Functor [] where
>> fmap::(a->b)->[]a->[]b
>>
>> my questions are:
>> 1. is this substitution ok?
>> 2. is []a = [a]?
>> 3. is []b = [b]?
>>
>> if 2. and 3. are valid then the following is obtained:
>> fmap::(a->b)->[a]->[b]
>> which is the same as map type and therefore: fmap = map. QED.
>>
>> Can you please answer questions 2 and 3 above?
>>
>> Thank you
>
> Yes, that's correct. The list constructor is special ([] isn't valid
> Haskell for a type constructor, so it's wired in), it can be used prefix
> ([] a) or `roundfix' ([a]). Ordinarily, you can use type constructors
> prefix or infix (if they take two type arguments), e.g. a -> b = (->) a b;
> Either a b = a `Either` b.
------------------------------
Message: 6
Date: Wed, 2 Mar 2011 07:27:33 -0800
From: Neil Jensen <[email protected]>
Subject: [Haskell-beginners] feedback on simplifying my Takusen code
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Hello, beginner here, looking for some ideas on how to simplify how I use
the Takusen database library to query a database and return types.
In the working code below, I'm able to query the database and
through several functions get a list of simple types. I've tried
playing around with the query2Iteratee and getAccounts' functions to
simplify the code and reduce it to a single function, but keep getting hung
up on compiler errors due to type issues.
Is there a better way to write this, or is it best to keep the
various functions quite separate? (in my real project, I have the
database access in a separate module from the list of custom types and the
code to convert to the Account type).
Thanks in advance,
Neil
import Database.Sqlite.Enumerator
import Database.Enumerator
data Account = Account {account_id :: Int}
deriving (Eq, Show)
query2Iteratee :: (Monad m) => Int -> [Int] -> m (IterResult [Int])
--query2Iteratee :: (Monad m) => Int -> IterAct m [Int]
query2Iteratee a accum = result' (a:accum)
getAccounts' :: IO [Int]
getAccounts' = do
let dbh = connect
"performance_data.sqlite"
withSession dbh (do
r <- doQuery (sql "SELECT account_id FROM
ACCOUNT;") query2Iteratee []
return r
)
getAccounts :: IO [Account]
getAccounts = do
accountsList <- getAccounts'
let r = map conv accountsList
return r
conv :: Int -> Account
conv a = Account {account_id=a}
main = do
accounts <- getAccounts'
putStrLn (show accounts)
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110302/399ca4c4/attachment-0001.htm>
------------------------------
Message: 7
Date: Wed, 2 Mar 2011 10:37:37 -0500
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Monads
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Wed, Mar 02, 2011 at 10:04:38AM -0500, Patrick Lynch wrote:
> Daniel,
> Thank you...I understand now...
> Good day
>
> Ps
> I think my problem that I'm facing is that I don't see how the
> instance Functor Maybe is derived from the class Functor...
>
> for example,
> the author Miran Lipovaca gives the following:
> class Functor f where
> fmap::(a->b)->f a->f b
>
> if Maybe replaces f:
> fmap::(a->b)->Maybe a->Maybe b
Right.
>
> I don't see how the instance Functor Maybe is derived [presumably:
> 'f' is function (a->b) and not functor 'f' as shown in the class
> description):
Yes, here 'f' is a function (a -> b). It is confusing that often the
letter f is used both to represent an arbitrary Functor or to
represent an arbitrary function. But you can tell the difference by
seeing whether the f occurs in a type or in some code.
> instance Functor Maybe where
> fmap f (Just x) = Just (f x)
> fmap f Nothing = Nothing
So you are confused about this code? Can you be more specific what
you are confused about? Try thinking carefully about all the types
involved. What is the type of f? (You already answered this: a -> b.)
What is the type of (Just x)? The type of x? What type is required on
the right hand side of the = ? And so on.
-Brent
------------------------------
Message: 8
Date: Wed, 02 Mar 2011 14:08:28 -0300
From: MAN <[email protected]>
Subject: Re: [Haskell-beginners] Monads
To: Patrick Lynch <[email protected]>
Cc: [email protected]
Message-ID: <1299085708.2255.1.camel@dy-book>
Content-Type: text/plain; charset="UTF-8"
El mi?, 02-03-2011 a las 10:04 -0500, Patrick Lynch escribi?:
> I think my problem that I'm facing is that I don't see how the instance
> Functor Maybe is derived from the class Functor...
> I don't see how the instance Functor Maybe is derived [presumably: 'f' is
> function (a->b) and not functor 'f' as shown in the class description):
> instance Functor Maybe where
> fmap f (Just x) = Just (f x)
> fmap f Nothing = Nothing
Perhaps it'll serve you well to think of the monad as a context for
computation. In the case of Maybe, it allows you to represent failure in
your computations.
Warning: lengthy mail piece ahead!
Suppose you've build a chain of computations, each segregated in its own
function. Some of those functions can fail, some others not. In a
different language you might use a special value to represent failure:
an empty string, an emtpy list, zero, some language-specific contruct
a-la NULL, etc. That approach has its cons, though: you have to check
for that particular value in each link of your chain, and since this
fail-value is tied to the return type, different special values will be
used throughout your chain... which is cumbersome, and confusing.
The Maybe type allows to represent failure through a much cleaner
(smarter) mechanism: You build your chain of computations in the Maybe
type. By using it as a monad, successful links will wrap its return
value in 'Just', and whichever link in your chain failing will return
'Nothing' (regardless of what it's working on, be it strings or numbers
or what not). As a result, your whole chain will short-circuit at that
failing link. The rest of the computations are never run.
As an example, suppose you've built a function 'fn', which type is 'a'
-> 'b', and your chain of functions so far gives you that 'a'... But
some of the functions running before 'fn' may fail (signaling, for
example, invalid input). Instead of choosing a particular value 'a' to
signal that error, you decide to wrap your functions in Maybe, and use
Maybe as a monad. Now 'fn' will recieve a 'Maybe a'... Assuming 'fn'
never fails (a situation quite common in long chains), you don't need to
return a 'Maybe b', so:
fn :: Maybe a -> b
fn Nothing = someValueNeverUsed
fn Just someValue = fn someValue
And rely in previous computations to sanitaze fn's input .But, what if
links running _after_ wrapFN can also fail? They live in the Maybe monad
too. So, it can be advantageous to return the results of 'fn' in Maybe,
even if 'fn' will never fail:
wrapFN :: Maybe a -> Maybe b
wrapFN ma = do
res <- ma
case res of Nothing -> Nothing
Just bs -> Just $ fn bs
You see, we run action 'ma' and act according to its results. If 'ma'
returns Nothing, we'll feed the next link in the chain Nothing,
propagating failure. Notice that, in this case 'fn' is never used, never
runs. This saves resources.
Assuming all other links in the chain follow a similar bahaviour, your
chain now short-circuits on failure. Hurra!
But really, what a bummer, wrappers everywhere? Enter 'fmap' :
wrapFN :: Maybe a -> Maybe b
wrapFN ma = fn `fmap` ma
-- or in point free: wrapFN = fmap fn
Which provides the same functionality. If before you where doing
something like:
maybeActionA >>= maybeActionB >>= wrapperFN >>= maybeActionC
now you do:
maybeActionA >>= maybeActionB >>= fmap fn >>= maybeActionC
Which shows how you've "lifted" the function 'fn' into the Maybe monad,
with little effort thanks to 'fmap'. In order for the above to work, you
need the Functor Maybe instance. Review it now, see if it makes more
sense:
instance Functor Maybe where
fmap f (Just x) = Just (f x) -- wrap successful computation
fmap f Nothing = Nothing -- propagate failure
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 33, Issue 2
****************************************