Send Beginners mailing list submissions to
        beginners@haskell.org

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
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  comments requested (rickmurphy)
   2.  IO ( stuff ) (Paul Monday)
   3. Re:  IO ( stuff ) (Antoine Latter)
   4. Re:  IO ( stuff ) (Brent Yorgey)
   5. Re:  IO ( stuff ) (David McBride)
   6. Re:  IO ( stuff ) (Brent Yorgey)
   7. Re:  IO ( stuff ) (Isaac Dupree)
   8. Re:  IO ( stuff ) (Henk-Jan van Tuyl)


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

Message: 1
Date: Fri, 09 Dec 2011 14:39:46 -0500
From: rickmurphy <r...@rickmurphy.org>
Subject: [Haskell-beginners] comments requested
To: beginners <beginners@haskell.org>
Message-ID: <1323459586.8735.7.camel@metho-laptop>
Content-Type: text/plain; charset="UTF-8"

Hey All:

A few months ago I got some help here starting up some RDF and JSON
work. Special thanks to David McBride for getting me on the right path.

http://osdir.com/ml/beginners@haskell.org/2011-10/msg00348.html

Since then I've been exercising some skill building. I've moved this
work to github with the hope that folks here can comment and offer
suggestions for improvement. You can find the files here:

https://github.com/rickmurphy/hafr

and a bit of a write up here.

http://phaneron.rickmurphy.org/

I welcome your feedback on refactoring, style, performance tuning, etc.

--
Rick 




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

Message: 2
Date: Fri, 9 Dec 2011 13:00:57 -0700
From: Paul Monday <paul.mon...@parsci.com>
Subject: [Haskell-beginners] IO ( stuff )
To: beginners@haskell.org
Message-ID: <ac5b9e86-b209-4730-8a4e-d6630d4b0...@parsci.com>
Content-Type: text/plain; charset=windows-1252

I've hammered through the bulk of my issues as I try to build a matrix that 
consists of random numbers, but the final hurdle is mixing pure and impure 
functions.  Does "impurity" from something like a random number generator or 
file I/O have to move it's way all the way through my code?

Here is the specific example.  I have a function that makes a row in a matrix, 
it looks like this:
makerow :: Int -> (Float, Float) -> IO (U.Vector Float)

The IO (U.Vector Float) is the result of the random number generation that is 
an impure call, making this an impure result.

I want to compose this into a list
makerows :: Int -> Int -> (Float, Float) -> [U.Vector Float]
makerows 0 _ _ = []
makerows r n range = makerow n range : makerows r' n range
    where r' = r - 1

But, of course, I can't mix the IO (U.Vector Float) with a U.Vector Float

The compilation result in:
    Couldn't match expected type `U.Vector Float'
                with actual type `IO (U.Vector Float)'
    In the return type of a call of `makerow'

So, at some point, I have to lift I believe ? is there a simple lifting 
solution?  It initially seemed that Monads were the solution ? but liftIO 
resulted in the same thing ? just with Monad (U.Vector Float) ? 

There is just some "simple" Haskellism I'm missing here, but after an ginormous 
amount of reading and googling, it is still eluding me :(

Any thoughts (besides the one page I found that helpfully ? basically ? said 
"Go back to Java" ;-)

Paul Monday
Parallel Scientific, LLC.
paul.mon...@parsci.com







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

Message: 3
Date: Fri, 9 Dec 2011 14:11:50 -0600
From: Antoine Latter <aslat...@gmail.com>
Subject: Re: [Haskell-beginners] IO ( stuff )
To: Paul Monday <paul.mon...@parsci.com>
Cc: beginners@haskell.org
Message-ID:
        <cakjsnqghs4a+7cir20jynujcerhp3t-m4bjsjgydp+jxvjz...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

On Fri, Dec 9, 2011 at 2:00 PM, Paul Monday <paul.mon...@parsci.com> wrote:
> I've hammered through the bulk of my issues as I try to build a matrix that 
> consists of random numbers, but the final hurdle is mixing pure and impure 
> functions. ?Does "impurity" from something like a random number generator or 
> file I/O have to move it's way all the way through my code?
>
> Here is the specific example. ?I have a function that makes a row in a 
> matrix, it looks like this:
> makerow :: Int -> (Float, Float) -> IO (U.Vector Float)
>
> The IO (U.Vector Float) is the result of the random number generation that is 
> an impure call, making this an impure result.
>
> I want to compose this into a list
> makerows :: Int -> Int -> (Float, Float) -> [U.Vector Float]
> makerows 0 _ _ = []
> makerows r n range = makerow n range : makerows r' n range
> ? ?where r' = r - 1
>

The lifting operator you're looking for is the 'return' function.

A direct fix for your code given above would be:

makerows 0 _ _ = return []
makerows r n range = do
      x <- makerow n range
      xs <-  makerows r' n range
      return (x:xs)

If you understand why that works, you now understand Haskell IO.
Congratulations!

Even better, though, is:

makerows r n range = replicateM r (makerow n range)

See: 
http://hackage.haskell.org/packages/archive/base/latest/doc/html/Control-Monad.html#v:replicateM

Antoine



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

Message: 4
Date: Fri, 9 Dec 2011 15:27:20 -0500
From: Brent Yorgey <byor...@seas.upenn.edu>
Subject: Re: [Haskell-beginners] IO ( stuff )
To: beginners@haskell.org
Message-ID: <20111209202720.ga24...@seas.upenn.edu>
Content-Type: text/plain; charset=us-ascii

> Does "impurity" from something
> like a random number generator or file I/O have to move it's way all
> the way through my code?

No, only through the parts that actually have to do file I/O or
generate random numbers or whatever.  However, cleanly separating the
IO code from the non-IO/"pure" code takes some experience.  It does
seem to be a common experience of people learning Haskell that IO ends
up "infecting" everything, even stuff that shouldn't have to do any
IO, but with good design this is not necessary.

In your particular case, your matrix generation function does depend
on random number generation so it makes sense that its type must
involve IO. However, if you go on to write other functions which do
deterministic operations on matrices, their types should *not* involve
IO, even if you pass randomly generated matrices to them as
arguments.

-Brent



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

Message: 5
Date: Fri, 9 Dec 2011 17:05:15 -0500
From: David McBride <toa...@gmail.com>
Subject: Re: [Haskell-beginners] IO ( stuff )
To: Brent Yorgey <byor...@seas.upenn.edu>
Cc: beginners@haskell.org
Message-ID:
        <can+tr422q_ugsxdnxbqbwoddejw11-ccvnod04rws-lbdcs...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

I wish I'd known this when I was first beginning, but it is possible
to do randomness outside of IO, surprisingly easily.  I like to use
the monadRandom library, which provides some monads and monad
transformers for this task.  I too became frustrated when I wrote a
roguelike but could not figure out how to inject randomness into it
when I wanted.  A program you would write might be like this:

data Obstacle = Mon (Int, Int) Monster | Door (Int, Int) | Trap (Int,
Int) deriving (Show, Enum)
data Monster = Orc | Wolf | Dragon deriving (Show, Enum)

main = do
  print =<< evalRandIO randomObstacle

randomObstacle :: RandomGen g => Rand g Obstacle
randomObstacle = do
  x <- getRandomR (0,2::Int)
  case x of
    0 -> Mon <$> randomLocation <*> randomMonster
    1 -> Door <$> randomLocation
    2 -> Trap <$> randomLocation

randomLocation :: RandomGen g => Rand g (Int,Int)
randomLocation = do
  x <- getRandomR (0,10)
  y <- getRandomR (0,10)
  return (x,y)

randomMonster :: RandomGen g => Rand g Monster
randomMonster = do
  x <- getRandomR (0,2::Int)
  return $ case x of
    0 -> Orc
    1 -> Dragon
    2 -> Wolf

This way, even though my randomBlah functions do not have IO in them,
nor do they pass around a stdGen around, but they can be combined
willy nilly as needed, and only computed when you want them to.  I
also could have made Random instances for Obstacle and Monster so that
I did not have to do the cases in the code, making things easier to
understand.

On Fri, Dec 9, 2011 at 3:27 PM, Brent Yorgey <byor...@seas.upenn.edu> wrote:
>> Does "impurity" from something
>> like a random number generator or file I/O have to move it's way all
>> the way through my code?
>
> No, only through the parts that actually have to do file I/O or
> generate random numbers or whatever. ?However, cleanly separating the
> IO code from the non-IO/"pure" code takes some experience. ?It does
> seem to be a common experience of people learning Haskell that IO ends
> up "infecting" everything, even stuff that shouldn't have to do any
> IO, but with good design this is not necessary.
>
> In your particular case, your matrix generation function does depend
> on random number generation so it makes sense that its type must
> involve IO. However, if you go on to write other functions which do
> deterministic operations on matrices, their types should *not* involve
> IO, even if you pass randomly generated matrices to them as
> arguments.
>
> -Brent
>
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://www.haskell.org/mailman/listinfo/beginners



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

Message: 6
Date: Fri, 9 Dec 2011 17:27:01 -0500
From: Brent Yorgey <byor...@seas.upenn.edu>
Subject: Re: [Haskell-beginners] IO ( stuff )
To: beginners@haskell.org
Message-ID: <20111209222701.ga4...@seas.upenn.edu>
Content-Type: text/plain; charset=us-ascii

On Fri, Dec 09, 2011 at 05:05:15PM -0500, David McBride wrote:
> 
> randomMonster :: RandomGen g => Rand g Monster
> randomMonster = do
>   x <- getRandomR (0,2::Int)
>   return $ case x of
>     0 -> Orc
>     1 -> Dragon
>     2 -> Wolf

This looks like a bug to me.  Everyone knows orcs are much more likely
than dragons. ;)

-Brent



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

Message: 7
Date: Fri, 09 Dec 2011 21:58:38 -0500
From: Isaac Dupree <m...@isaac.cedarswampstudios.org>
Subject: Re: [Haskell-beginners] IO ( stuff )
To: beginners@haskell.org
Message-ID: <4ee2cade.7040...@isaac.cedarswampstudios.org>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

On 12/09/2011 05:27 PM, Brent Yorgey wrote:
> On Fri, Dec 09, 2011 at 05:05:15PM -0500, David McBride wrote:
>>
>> randomMonster :: RandomGen g =>  Rand g Monster
>> randomMonster = do
>>    x<- getRandomR (0,2::Int)
>>    return $ case x of
>>      0 ->  Orc
>>      1 ->  Dragon
>>      2 ->  Wolf
>
> This looks like a bug to me.  Everyone knows orcs are much more likely
> than dragons. ;)

I once wrote a function something like
randomDistributed :: (Num a, RandomGen g) => [(a, b)] -> g -> (b, g)

so that e.g.
randomDistributed [(10, Orc), (1, Dragon), (3.7, Wolf)]
would get you ten orcs for every dragon on average,
the overall chance of an orc being (10 / (10 + 1 + 3.7)).

~Isaac



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

Message: 8
Date: Sat, 10 Dec 2011 09:09:32 +0100
From: "Henk-Jan van Tuyl" <hjgt...@chello.nl>
Subject: Re: [Haskell-beginners] IO ( stuff )
To: "Brent Yorgey" <byor...@seas.upenn.edu>, "David McBride"
        <toa...@gmail.com>, dmcbr...@neondsl.com
Cc: beginners@haskell.org
Message-ID: <op.v59kx6yxpz0...@zen5.arnhem.chello.nl>
Content-Type: text/plain; charset=iso-8859-15; format=flowed;
        delsp=yes

On Fri, 09 Dec 2011 23:05:15 +0100, David McBride <toa...@gmail.com> wrote:

> data Monster = Orc | Wolf | Dragon deriving (Show, Enum)
>
> randomMonster :: RandomGen g => Rand g Monster
> randomMonster = do
>   x <- getRandomR (0,2::Int)
>   return $ case x of
>     0 -> Orc
>     1 -> Dragon
>     2 -> Wolf

You have already created an Enum instance of Monster, so why not use it:

  randomMonster :: RandomGen g => Rand g Monster
  randomMonster =
    do
      x <- getRandomR (0,2::Int)
      return $ toEnum x

Or:
  randomMonster :: RandomGen g => Rand g Monster
  randomMonster = toEnum <$> getRandomR (0,2::Int)

where <$> is imported from module Data.Functor or Control.Applicative.

Regards,
Henk-Jan van Tuyl


-- 
http://Van.Tuyl.eu/
http://members.chello.nl/hjgtuyl/tourdemonad.html
--



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

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 42, Issue 11
*****************************************

Reply via email to