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: Class and Instance Question (James Cook)
2. I don't understand mapM in mapM id (Just 1, Nothing, Just
3) (Kees Bleijenberg)
3. Re: I don't understand mapM in mapM id (Just 1, Nothing,
Just 3) (Magnus Therning)
4. Re: I don't understand mapM in mapM id (Just 1, Nothing,
Just 3) (Daniel Fischer)
5. Re: "pure" versus "impure" code (Paul Sargent)
6. Re: "pure" versus "impure" code (Thomas Davie)
7. Re: question about Yesod (Michael Steele)
----------------------------------------------------------------------
Message: 1
Date: Fri, 20 May 2011 08:29:24 -0400
From: James Cook <[email protected]>
Subject: Re: [Haskell-beginners] Class and Instance Question
To: Adam C.Emerson <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes
On May 19, 2011, at 9:05 PM, Adam C.Emerson wrote:
> Good morning,
>
> As my first "real" toy project (something trivial but with some
> pretense of usefulness at least to me) in Haskell, I had been trying
> to write an XDR encoder/decoder based on top of Data.Binary.
I have an unreleased XDR library[1] you're welcome to use or hack on.
The source is not exactly beginner-friendly, but it's fairly easy to
use (IMO, of course) and has been working very well for my purposes.
It's more or less complete, but very unpolished so it may be more
hassle than its worth for you to try to figure out how to use it. If
you're interested, though, I wouldn't mind putting some time into
polishing and documenting it. Or if you'd prefer to finish your own I
absolutely understand and wouldn't be offended in the least. ;)
> As such,
> I have a typeclass:
>
> class Encodable t where
> -- | Encode a value in the Put monad.
> put :: t -> Put
> -- | Decode a value in the Get monad
> get :: Get t
>
> And I have various instances, all of which seem to work, except for
> two. My thought was to treat a list of Chars as a string and a list
> of "encodable" types as a counted array.
Other people have responded with suggestions about how to achieve
this, so I'll just mention that in my library I chose not to provide a
class instance for strings, because there are so many ambiguities
about how it could be encoded that I didn't feel comfortable making
the choice once and for all.
-- James
[1] https://github.com/mokus0/xdr
------------------------------
Message: 2
Date: Fri, 20 May 2011 14:31:54 +0200
From: "Kees Bleijenberg" <[email protected]>
Subject: [Haskell-beginners] I don't understand mapM in mapM id (Just
1, Nothing, Just 3)
To: <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"
I was playing with winghci and I tried:
mapM id [Just 1, Just 2, Just 3]
result: Just [1,2,3]
I don't understand this answer.
>From <http://members.chello.nl/hjgtuyl/tourdemonad.html#mapM>
http://members.chello.nl/hjgtuyl/tourdemonad.html#mapM
mapM mf xs takes a monadic function mf (having type Monad m => (a -> m b))
and applies it to each element in list xs; the result is a list inside a
monad.
A few things I've found:
mapM :: (a -> m b) -> [a] -> m [b]
So in this case: a = Maybe Int (second arg in mapM id [Just1, Just 2, Just
3] and b = Int and m = Maybe. So id is :: Maybe Int -> Maybe Int
mapM id [Just 1, Nothing, Just 3]
result: Nothing.
My first guess for the result: Just [Just 1, Nothing, Just 3]
when I do: mapM id [1,2,3] I get an error (id has wrong type, which makes
sense)
Can somebody explain what is going on here?
Kees
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110520/c86bd056/attachment.htm>
------------------------------
Message: 3
Date: Fri, 20 May 2011 13:45:20 +0100
From: Magnus Therning <[email protected]>
Subject: Re: [Haskell-beginners] I don't understand mapM in mapM id
(Just 1, Nothing, Just 3)
To: [email protected]
Message-ID: <20110520124520.GA3562@ohann>
Content-Type: text/plain; charset="us-ascii"
On Fri, May 20, 2011 at 02:31:54PM +0200, Kees Bleijenberg wrote:
> I was playing with winghci and I tried:
>
> mapM id [Just 1, Just 2, Just 3]
> result: Just [1,2,3]
>
> I don't understand this answer.
>
> From <http://members.chello.nl/hjgtuyl/tourdemonad.html#mapM>
> http://members.chello.nl/hjgtuyl/tourdemonad.html#mapM
> mapM mf xs takes a monadic function mf (having type Monad m => (a -> m b))
> and applies it to each element in list xs; the result is a list inside a
> monad.
>
> A few things I've found:
> mapM :: (a -> m b) -> [a] -> m [b]
> So in this case: a = Maybe Int (second arg in mapM id [Just1, Just 2, Just
> 3] and b = Int and m = Maybe. So id is :: Maybe Int -> Maybe Int
>
> mapM id [Just 1, Nothing, Just 3]
> result: Nothing.
> My first guess for the result: Just [Just 1, Nothing, Just 3]
>
> when I do: mapM id [1,2,3] I get an error (id has wrong type, which makes
> sense)
>
> Can somebody explain what is going on here?
You have to types right:
a = Maybe Int
m = Maybe
b = Int
'mapM' is implemented like this:
mapM f as = sequence (map f as)
So working through the examples:
mapM id [Just 1, Just 2, Just 3]
= sequence (map id [Just 1, ...])
= sequence [Just 1, ...] = Just [1, ...]
mapM id [Just 1, Nothing, Just 3]
= sequence (map id [Just 1, Nothing, ...])
= sequence [Just1, Nothing, ...]
= Nothing
/M
--
Magnus Therning OpenPGP: 0xAB4DFBA4
email: [email protected] jabber: [email protected]
twitter: magthe http://therning.org/magnus
I invented the term Object-Oriented, and I can tell you I did not have
C++ in mind.
-- Alan Kay
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110520/fd72fd73/attachment-0001.pgp>
------------------------------
Message: 4
Date: Fri, 20 May 2011 14:57:37 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] I don't understand mapM in mapM id
(Just 1, Nothing, Just 3)
To: [email protected]
Message-ID: <[email protected]>
Content-Type: Text/Plain; charset="utf-8"
On Friday 20 May 2011 14:31:54, Kees Bleijenberg wrote:
> I was playing with winghci and I tried:
>
> mapM id [Just 1, Just 2, Just 3]
> result: Just [1,2,3]
>
> I don't understand this answer.
>
> From <http://members.chello.nl/hjgtuyl/tourdemonad.html#mapM>
> http://members.chello.nl/hjgtuyl/tourdemonad.html#mapM
> mapM mf xs takes a monadic function mf (having type Monad m => (a -> m
> b)) and applies it to each element in list xs; the result is a list
> inside a monad.
>
> A few things I've found:
> mapM :: (a -> m b) -> [a] -> m [b]
> So in this case: a = Maybe Int (second arg in mapM id [Just1, Just 2,
> Just 3] and b = Int and m = Maybe. So id is :: Maybe Int -> Maybe Int
In more detail,
[Just 1, Just 2, Just 3] :: [Maybe Int]
That means a = Maybe Int, in mapM id [Just 1, Just 2, Just 3]
id :: t -> t (for all t)
in mapM id list, id must have type (a -> m b) for some Monad m, we have to
unify the type (t -> t) with (a -> m b); that gives
t = a (the argument types must be the same) and
t = m b (the result types must be the same)
Now a = Maybe Int = t = m b, hence m = Maybe, b = Int, thus
mapM id [Just 1, Just 2, Just 3] :: m [b] = Maybe [Int]
>
> mapM id [Just 1, Nothing, Just 3]
> result: Nothing.
> My first guess for the result: Just [Just 1, Nothing, Just 3]
No, that would have type Maybe [Maybe Int], but we've seen that it must
have type Maybe [Int], so it's either Just (list of some Ints) or Nothing.
Now, since mapM is polymorphic (works with all Monads and all parameter
types), there's not much choice for doing something sensible. mapM can only
use the function it gets as first argument and the Monad methods, so
mapM foo [] = return []
mapM foo (x:xs) = ?
well, it could ignore x (not sensible) or it can apply foo to x and then do
something with that and xs; what can it do with xs? Well, mapM foo xs.
So we have to somehow sensible combine
foo x :: m b
mapM foo xs :: m [b]
The most natural is
do y <- foo x
ys <- mapM foo xs
return (y:ys)
or, without do-notation,
foo x >>= \y -> mapM foo xs >>= \ys -> return (y:ys)
or, with a combinator,
liftM2 (:) (foo x) (mapM foo xs)
Now, in the above, mapM id [Just 1, Nothing, Just 3], the Monad instance of
Maybe says Nothing >>= _ = Nothing, so we get
Just 1 >>= \y -> (Nothing >>= \z -> whatever)
~> Just 1 >>= \y -> Nothing
~> (\y -> Nothing) 1
~> Nothing
>
> when I do: mapM id [1,2,3] I get an error (id has wrong type, which
> makes sense)
>
> Can somebody explain what is going on here?
>
> Kees
------------------------------
Message: 5
Date: Fri, 20 May 2011 16:07:24 +0100
From: Paul Sargent <[email protected]>
Subject: Re: [Haskell-beginners] "pure" versus "impure" code
To: "Costello, Roger L." <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On 19 May 2011, at 21:12, Costello, Roger L. wrote:
> My understanding is that:
>
> - Pure code is code that does no I/O
>
> - Impure code is code that does I/O
I don't particularly like that definition. It's true, but the definition of I/O
has to be very board.
The definition I use is that a pure function is one that
- takes input only from it's parameters.
- outputs only via it's return value.
If x is an external variable in memory
Pure : f(b,c) = b + c
Impure: f(b) = b + x; (violates first rule - Uses value in x)
Impure: f(b,c) = 1 ; x = b + c; (violates second rule - Changes value in x)
Pure functions can combine to make larger pure functions. Impure functions
combined with pure functions make larger impure functions.
> Also, it is my understanding that good software design is to isolate/separate
> the impure code from the pure code. Is that correct?
As the result of pure code is dependent only on it's inputs you can make
statements like:
f(1,2) is always is equal to f(1,2) at any time
...without knowing what function f() is. That's quite powerful, and can be
useful in optimisation.
Another way of phrasing it - A pure piece of code causes no side-effects, and
it is not effected by other code's side-effects. This property is very useful
in parallel computations.
Separating pure code out can be useful, but most programs in most languages
don't. That's mainly due to the use of shared state variables between
functions. Object orientation is inherently impure as every object has retained
state, modified and used by the methods. This isn't necessarily a bad thing. At
some point all programs have to be impure otherwise they'll always calculate
the same result.
> Does that principle apply to all programming languages, or just Haskell?
Yes, it applies to all languages.
------------------------------
Message: 6
Date: Fri, 20 May 2011 16:52:06 +0100
From: Thomas Davie <[email protected]>
Subject: Re: [Haskell-beginners] "pure" versus "impure" code
To: Paul Sargent <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=windows-1252
The definition of pure code is very simple. Pure code always returns the same
value given the same arguments. The issue comes in the subtleties of arguments
of the IO monad. Functions that produce IO actions are pure ? they always
produce the same IO action given the same arguments. What's not pure is the
runtime which then interprets the IO action. The result of this is that you
only get the nice guarantees that purity gives you when considering which IO
action is produced, not when considering the effects of the IO action being
executed by the runtime.
Bob
------------------------------
Message: 7
Date: Fri, 20 May 2011 08:54:04 -0700
From: Michael Steele <[email protected]>
Subject: Re: [Haskell-beginners] question about Yesod
To: Michael Litchard <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
There's a list of yesod-powered sites at
http://wiki.yesodweb.com/Powered%20by%20Yesod. Some of them have made
their source code available, so you can look there for other examples.
I recommend reading through the book that Michael Snoyman is writing.
On Thu, May 19, 2011 at 4:09 PM, Michael Litchard <[email protected]> wrote:
> I admire what's been done with Yesod, especially in the example given
> by the Haskellers website. It's time I start making design decisions
> for my own website project, and I thought it would be a great idea to
> just steal from Haskellers liberally. My initial approach was to just
> gut Haskellers until everything I don't need is gone. This isn't
> working out as I had hoped. If it's not a huge pain in the butt, could
> someone put an example on github that does the following: I'm looking
> for the look of Haskellers. The tabs, and navbars is fine. The little
> bar that tracks what part of the site you are on would be bonus.
>
>
> Thanks for any help on this.
>
>
> Michael Litchard
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
--
-- Michael Steele
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 35, Issue 42
*****************************************