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.  State Monad (Thomas Jakway)
   2.  dropWhile returning last dropped element (martin)
   3. Re:  dropWhile returning last dropped element (Francesco Ariis)
   4. Re:  dropWhile returning last dropped element (Michael Orlitzky)
   5. Re:  State Monad (Sumit Sahrawat, Maths & Computing, IIT (BHU))
   6. Re:  State Monad (Mike Meyer)
   7. Re:  State Monad (Thomas Jakway)


----------------------------------------------------------------------

Message: 1
Date: Fri, 17 Apr 2015 15:25:52 -0400
From: Thomas Jakway <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] State Monad
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"; Format="flowed"

I'm having some trouble using the state monad with a random number 
generator.

I'm trying to follow the LYAH example 
(http://learnyouahaskell.com/for-a-few-monads-more) but apparently the 
state monad API has changed, so I'm using Control.Monad.Trans.State.Lazy 
(state monad from the transformers package).  Side question: is using 
transformers a good idea?  I've heard some people complain about mtl's 
performance, others don't seem to care.  I'm far too new to be able to 
judge anything (does it even make sense to compare transformers vs. 
mtl?) but if one has overtaken the other I'd rather use that.

Here's what I currently have:
import Control.Monad.Trans.State.Lazy
type GeneratorState = State StdGen

genThree :: Int -> GeneratorState Int
genThree listMax = do --highest index in the list
         let listMin = 0 :: Int --lowest index in the list
         generator <- get
         let (generatedMin, state) = randomR (listMin, listMax) generator
         return generatedMin

Although it typechecks it doesn't seem like an improvement over just 
using StdGen by itself.

What I think I should have:
genThree :: Int -> GeneratorState Int
genThree listMax = do --highest index in the list
         let listMin = 0 :: Int --lowest index in the list
         generatedMin <- state randomR (listMin, listMax)
         return generatedMin
*
*I feel like I really botched this--what am I missing?
*
*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150417/9201074c/attachment-0001.html>

------------------------------

Message: 2
Date: Fri, 17 Apr 2015 22:08:08 +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] dropWhile returning last dropped element
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8

Hello all

Just curious: is there a library function like dropWhile, but which returns 
last dropped element along with the
remainder of the list?

If not, how can I write one

- without explicit recursion
- without traversing the list more than once
- without traversing more of the list than necessary


------------------------------

Message: 3
Date: Fri, 17 Apr 2015 22:20:31 +0200
From: Francesco Ariis <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] dropWhile returning last dropped
        element
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Fri, Apr 17, 2015 at 10:08:08PM +0200, martin wrote:
> Hello all
> 
> Just curious: is there a library function like dropWhile,
> but which returns last dropped element along with the remainder of the
> list?

None that I know of, but span from Data.List might be worth a look


------------------------------

Message: 4
Date: Fri, 17 Apr 2015 17:18:37 -0400
From: Michael Orlitzky <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] dropWhile returning last dropped
        element
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8

On 04/17/2015 04:08 PM, martin wrote:
> Hello all
> 
> Just curious: is there a library function like dropWhile, but which returns 
> last dropped element along with the
> remainder of the list?
> 
> If not, how can I write one
> 
> - without explicit recursion
> - without traversing the list more than once
> - without traversing more of the list than necessary

Here's a version that meets your last two criteria, complete with
QuickCheck tests to make sure it works. Why do you want to avoid
recursion? You could rewrite this to use list indices if you really
wanted to, but anything you come up with is going to be essentially
recursive, only less safe

----

module Main
where

import Test.QuickCheck


-- | A version of dropWhile that returns the last element dropped as
--   well as the remainder of the list. This is simply sugar over the
--   'dw' function, so you don't have to pass in a (Nothing,) tuple at
--   first.
--
dropWhile' :: (a -> Bool)    -- ^ The predicate used to drop stuff
           -> [a]            -- ^ The list of stuff
           -> (Maybe a, [a]) -- ^ (The last element dropped, list tail)
dropWhile' p xs = dw p (Nothing, xs)


-- | The \"real\" implementation of the dropWhile'.
--
dw :: (a -> Bool)     -- ^ The predicate used to drop stuff
   -> (Maybe a, [a]) -- ^ (Current last element dropped, current list)
   -> (Maybe a, [a]) -- ^ (New last element dropped, remaining list)

-- This case is pretty easy. There's nothing left in the list, so just
-- return what we've got.
dw _ (lastdropped, []) = (lastdropped, [])


dw predicate (lastdropped, xs@(x:tail_of_xs))
  -- Drop the first element of the list, and "update" the return value
  -- before recursing.
  | predicate x = dw predicate (Just x, tail_of_xs)

  -- We're not dropping any more, just quit.
  | otherwise = (lastdropped, xs)


-- | Make sure the tail of the list we get back is the same as it would
--   be if we used dropWhile.
--
prop_tailcorrect :: [Int] -> Bool
prop_tailcorrect ints =
  actual == expected
  where
    (_, actual) = dropWhile' even ints
    expected = dropWhile even ints


-- | Use a slower algorithm to make sure that we're getting the
--   correct \"last item dropped\".
--
prop_lastcorrect :: [Int] -> Bool
prop_lastcorrect ints =
  actual == expected
  where
    (actual, _) = dropWhile' even ints
    expected = case (reverse $ takeWhile even ints) of
                 [] -> Nothing
                 (x:_) -> Just x



------------------------------

Message: 5
Date: Sat, 18 Apr 2015 04:01:02 +0530
From: "Sumit Sahrawat, Maths & Computing, IIT (BHU)"
        <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] State Monad
Message-ID:
        <cajbew8n4na+bqxxwbf1k3u1xt_an68kckqadtcjm4i576+a...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

A good exercise will be to re-implement State, and then use it. Take a look
here: http://en.wikibooks.org/wiki/Haskell/Understanding_monads/State.
This link will take you to a tutorial that takes you through the
implementation of State, and then uses it for random number generation.

Hope this helps. Enjoy !

On 18 April 2015 at 00:55, Thomas Jakway <[email protected]> wrote:

>  I'm having some trouble using the state monad with a random number
> generator.
>
> I'm trying to follow the LYAH example (
> http://learnyouahaskell.com/for-a-few-monads-more) but apparently the
> state monad API has changed, so I'm using Control.Monad.Trans.State.Lazy
> (state monad from the transformers package).  Side question: is using
> transformers a good idea?  I've heard some people complain about mtl's
> performance, others don't seem to care.  I'm far too new to be able to
> judge anything (does it even make sense to compare transformers vs. mtl?)
> but if one has overtaken the other I'd rather use that.
>
> Here's what I currently have:
> import Control.Monad.Trans.State.Lazy
> type GeneratorState = State StdGen
>
> genThree :: Int -> GeneratorState Int
> genThree listMax = do --highest index in the list
>         let listMin = 0 :: Int --lowest index in the list
>         generator <- get
>         let (generatedMin, state) = randomR (listMin, listMax) generator
>         return generatedMin
>
> Although it typechecks it doesn't seem like an improvement over just using
> StdGen by itself.
>
> What I think I should have:
> genThree :: Int -> GeneratorState Int
> genThree listMax = do --highest index in the list
>         let listMin = 0 :: Int --lowest index in the list
>         generatedMin <- state randomR (listMin, listMax)
>         return generatedMin
>
> I feel like I really botched this--what am I missing?
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>


-- 
Regards

Sumit Sahrawat
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150418/4baa3ea5/attachment-0001.html>

------------------------------

Message: 6
Date: Fri, 17 Apr 2015 17:48:48 -0500
From: Mike Meyer <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] State Monad
Message-ID:
        <CAD=7u2an-h2hszmbda0hdl4wmnn1tpblmuve4ocxz15hksa...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Fri, Apr 17, 2015 at 2:25 PM, Thomas Jakway <[email protected]> wrote:

> genThree listMax = do --highest index in the list
>         let listMin = 0 :: Int --lowest index in the list
>         generatedMin <- state randomR (listMin, listMax)
>         return generatedMin
>

What you're missing is a $:

The only chagne to our genThree functions is making it "state $" instead of
"state".


#!/usr/bin/env runhaskell

import System.Random
import Control.Monad.State

genThree listMax = do --highest index in the list
        let listMin = 0 :: Int --lowest index in the list
        generatedMin <- state $ randomR (listMin, listMax)
        return generatedMin

main = do
  gen <- newStdGen
  print $ evalState (genThree 10) gen
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150417/c47c827c/attachment-0001.html>

------------------------------

Message: 7
Date: Fri, 17 Apr 2015 19:19:36 -0400
From: Thomas Jakway <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] State Monad
Message-ID: <[email protected]>
Content-Type: text/plain; charset="windows-1252"; Format="flowed"

Thank you very much!

On 4/17/15 6:48 PM, Mike Meyer wrote:
>
> On Fri, Apr 17, 2015 at 2:25 PM, Thomas Jakway <[email protected] 
> <mailto:[email protected]>> wrote:
>
>     genThree listMax = do --highest index in the list
>             let listMin = 0 :: Int --lowest index in the list
>             generatedMin <- state randomR (listMin, listMax)
>             return generatedMin
>
>
> What you're missing is a $:
>
> The only chagne to our genThree functions is making it "state $" 
> instead of "state".
>
>
> #!/usr/bin/env runhaskell
>
> import System.Random
> import Control.Monad.State
>
> genThree listMax = do --highest index in the list
>         let listMin = 0 :: Int --lowest index in the list
>         generatedMin <- state $ randomR (listMin, listMax)
>         return generatedMin
>
> main = do
>   gen <- newStdGen
>   print $ evalState (genThree 10) gen
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150417/ff279697/attachment.html>

------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 82, Issue 21
*****************************************

Reply via email to