Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://mail.haskell.org/cgi-bin/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. runState (liftM (+100) pop) [1, 2, 3, 4] (Example from LYH)
(Olumide)
2. Re: runState (liftM (+100) pop) [1, 2, 3, 4] (Example from
LYH) (Francesco Ariis)
3. Manipulate list, one element at a time (martin)
4. Re: Manipulate list, one element at a time (Frerich Raabe)
----------------------------------------------------------------------
Message: 1
Date: Tue, 24 Oct 2017 10:17:46 +0100
From: Olumide <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] runState (liftM (+100) pop) [1, 2, 3, 4]
(Example from LYH)
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8; format=flowed
Dear List,
Apologies for the awkward question title -- its the best I can do ATM.
I'm still working my way through chapter 13 of LYH (title for a few
monads more) and I came across the following two beasts
ghci> runState (liftM (+100) pop) [1,2,3,4]
(101,[2,3,4])
ghci> runState (fmap (+100) pop) [1,2,3,4]
(101,[2,3,4])
See
http://learnyouahaskell.com/for-a-few-monads-more#useful-monadic-functions
Even though I'm still struggling to wrap my mind around monads, I sort
of understand what's going on here. The problem is that I can't explain
why the function (+100) is applied to _only_ the value 1 in (1,[2,3,4]).
Regards,
- Olumide
------------------------------
Message: 2
Date: Tue, 24 Oct 2017 11:32:53 +0200
From: Francesco Ariis <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] runState (liftM (+100) pop) [1, 2, 3,
4] (Example from LYH)
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Tue, Oct 24, 2017 at 10:17:46AM +0100, Olumide wrote:
> ghci> runState (fmap (+100) pop) [1,2,3,4]
> (101,[2,3,4])
>
> Even though I'm still struggling to wrap my mind around monads, I sort of
> understand what's going on here. The problem is that I can't explain why the
> function (+100) is applied to _only_ the value 1 in (1,[2,3,4]).
Hello Olumide,
if we look at the instance of `fmap` for State we'll find (more or
less):
fmap :: (a -> b) -> State s a -> State s b
So fmap modifies the *result*, not the *state* itself.
If we also recall that `State s a` is nothing but `\s -> (a, s)` then
it is easy to see that only the first element of the tuple (the
so called result, `a`) will be modified.
Does this clear your doubts?
------------------------------
Message: 3
Date: Tue, 24 Oct 2017 12:18:10 +0200
From: martin <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] Manipulate list, one element at a time
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8
Hello all,
How can I do something to each element of a list, but only modify one element
at at time, so the result is a list of
lists, where each sublist is the original list with one element altered.
Something like
type Each a = (a->a) -> [a] -> [[a]]
I came up with:
each :: Each a
each f [] = []
each f (x:xs) = (f x : xs) : (map (x:) $ each f xs)
λ> each (*10) [1..3]
[[10,2,3],[1,20,3],[1,2,30]]
but I wonder if there is a more standard way of doing this
------------------------------
Message: 4
Date: Tue, 24 Oct 2017 12:59:57 +0200
From: Frerich Raabe <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Manipulate list, one element at a
time
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed
On 2017-10-24 12:18, martin wrote:
> How can I do something to each element of a list, but only modify one
> element at at time, so the result is a list of
> lists, where each sublist is the original list with one element altered.
>
> Something like
[..]
> λ> each (*10) [1..3]
> [[10,2,3],[1,20,3],[1,2,30]]
You could make use of the 'inits' and 'tails' functions from Data.List:
λ: inits [1..3]
[[],[1],[1,2],[1,2,3]]
λ: tails [1..3]
[[1,2,3],[2,3],[3],[]]
Using this, you could build a little list comprehension which takes every
head and every tail and produces a list in which the two are concatenated,
except that the first element of the tail has some function applied to it:
λ: let each f xs = [i ++ f t:ts | (i, t:ts) <- zip (inits xs) (tails xs)]
λ: :t each
each :: (a -> a) -> [a] -> [[a]]
λ: each (*10) [1..3]
[[10,2,3],[1,20,3],[1,2,30]]
--
Frerich Raabe - [email protected]
www.froglogic.com - Multi-Platform GUI Testing
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 112, Issue 21
******************************************