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. Randomness, lists, and unfoldr (Alex Rozenshteyn)
2. Re: how to implement accesor function for given data type
(Brent Yorgey)
3. Re: Randomness, lists, and unfoldr (Daniel Fischer)
4. Re: Randomness, lists, and unfoldr (Sriram Durbha)
5. Re: Randomness, lists, and unfoldr (Stephen Tetley)
6. Re: Randomness, lists, and unfoldr (Alex Rozenshteyn)
----------------------------------------------------------------------
Message: 1
Date: Mon, 13 Sep 2010 14:42:49 -0400
From: Alex Rozenshteyn <[email protected]>
Subject: [Haskell-beginners] Randomness, lists, and unfoldr
To: beginners <[email protected]>, [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="utf-8"
I'm trying to build a list where each entry depends on the previous one.
Unfoldr seemed like a good idea at the time.
Unfortunately, my values are monadic values (specifically RVars, from the
random-fu package). Okay, shouldn't be a problem; I just monadic bind
and...
> -- example code
> updateCell :: Bool -> RVar Bool
> updateCell False = return False
> updateCell True = bernoulli (0.9 :: Double)
> sim = sequence $ take 20 $ unfoldr (\x -> Just (x, x >>= updateCell))
(return True)
> runRVar sim DevURandom
[True,True,True,True,True,False,True,False,True,False,False,False,False,False,False,True,True,False,False,False]
That output shouldn't be possible if I'm doing things right... It appears
that each cell has an independent history. I'm stumped.
Advice on threading monad input in general and random-fu in specific would
be appreciated.
--
Alex R
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100913/0bf742f4/attachment-0001.html
------------------------------
Message: 2
Date: Mon, 13 Sep 2010 14:57:32 -0400
From: Brent Yorgey <[email protected]>
Subject: [Haskell-beginners] Re: how to implement accesor function for
given data type
To: Martin Tomko <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Mon, Sep 13, 2010 at 05:33:12PM +0200, Martin Tomko wrote:
> Hi Brent,
> thank you for your help. Indeed, I need to be able to distinguish
> them in terms of single coordinate, tuple or list - that is crucial
> in order to keep the logic.
> I am not sure how your second suggestion would help though, because
> then I would get a function to return the
> CoordinateSet, but then I would need again to somehow extract the
> individual vertices out of it for any mathematical processing. And
> again, I will be faced by the same problem. just one wrapper further,
> am I right?
No, you won't still have the same problem. The problem before was
that you wanted to write a function which could return values of
several different types, which is not possible. (Well, technically,
it IS possible, but not in a way that would be helpful to you.) This
solution allows you to wrap the options up in a single type. Of
course, you will later need to extract vertices out of the Coordinates
type for processing -- but that's just fine, you can write the
processing function by cases on the Coordinates type.
However, you might decide that the intermediate step is not all that
helpful and just write the mathematical processing directly by cases
on Geometry. It depends on what the mathematical processing looks
like, I suppose.
> This is what I end up with:
>
> type Coordinate = (Float, Float)
>
> data Coordinates = Pt Coordinate | Ls (Coordinate, Coordinate)| Pl
> [Coordinate] deriving (Show,Eq) -- this is what you called the
> CoordinateSet
>
> data Geometry = Point FID Coordinates
> | LineSegment FID Coordinates
> | Polyline FID Coordinates
> | Polygon FID Coordinates
> deriving (Show,Eq)
This looks wrong to me, since this would allow (say) a Point to have a
list of coordinates, or a Polygon to have just a single coordinate. I
think you want to stick with what you had before.
>
> Then, indeed, the whole Geometry ADT seems redundant, and could be
It doesn't look redundant to me. For example, isn't there a
difference between a Polyline and a Polygon (even if they have the
same coordinates)?
> Let us assume a practical example: I want to find vertices shared by
> two geometries, using intersect or elem seems to be the way to go,
> depending if either are lists or soem are single points (not sure
> this will work on tuples... I would prefer a single solution for
> all).
> My thinking is that I need a function to get to the Coordinates of a
> geometry somehow, and maybe convert the mto a vector, but that sounds
> like a dirty trick not worthy of Haskell!
Why not:
shared :: Geometry -> Geometry -> [Coordinate]
shared geo1 geo2 = sharedCoords (getCoords geo1) (getCoords geo2)
sharedCoords :: Coordinates -> Coordinates -> [Coordinate]
sharedCoords (Pt p1) (Pt p2) = if (p1 == p2) then [p1] else []
sharedCoords (Pt p1) (Ls (x,y)) = ...
sharedCoords (Pt p1) (Pl cs) = ...
...
and so on. This is what I mean by doing mathematical analysis by
cases on Coordinates.
-Brent
------------------------------
Message: 3
Date: Mon, 13 Sep 2010 21:21:42 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Randomness, lists, and unfoldr
To: [email protected]
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
On Monday 13 September 2010 20:42:49, Alex Rozenshteyn wrote:
> I'm trying to build a list where each entry depends on the previous one.
> Unfoldr seemed like a good idea at the time.
> Unfortunately, my values are monadic values (specifically RVars, from
> the random-fu package). Okay, shouldn't be a problem; I just monadic
> bind and...
>
> > -- example code
> > updateCell :: Bool -> RVar Bool
> > updateCell False = return False
> > updateCell True = bernoulli (0.9 :: Double)
> >
> > sim = sequence $ take 20 $ unfoldr (\x -> Just (x, x >>= updateCell))
>
> (return True)
So you get
sequence [return True
, return True >>= updateCell
, (return True >>= updateCell) >>= updateCell
, ((return True >>= updateCell) >>= updateCell) >>= updateCell
, ... ]
>
> > runRVar sim DevURandom
>
> [True,True,True,True,True,False,True,False,True,False,False,False,False,
>False,False,True,True,False,False,False]
>
> That output shouldn't be possible if I'm doing things right... It
> appears that each cell has an independent history. I'm stumped.
> Advice on threading monad input in general and random-fu in specific
> would be appreciated.
What you want would be something like
iterateM :: (Monad m) => (a -> m a) -> a -> m [a]
iterateM act start = do
next <- act start
rest <- iterateM act next
return (start : rest)
but that would only work if the bind is sufficiently lazy, otherwise you'd
have to iterate a given number of times.
------------------------------
Message: 4
Date: Mon, 13 Sep 2010 12:24:06 -0700
From: Sriram Durbha <[email protected]>
Subject: Re: [Haskell-beginners] Randomness, lists, and unfoldr
To: Alex Rozenshteyn <[email protected]>
Cc: beginners <[email protected]>, [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
related question: is this also an example of para morphism?
here is my current understanding
cata morphism -> fold/reduce [ e.g. list of numbers -> sum of numbers]
ana morphism -> unfold/reproduce depending on input [ e.g. one number ->
list of reapeated instances of same number]
some functions like map can be written both as a cata and as an ana.
hylo morphism -> ana followed by cata [ e.g. recursion trees of function
calls are ana, and the return path to the final evaluation is cata]
para morphism -> kind of ana but depends on input and output generated so
far fib, fact etc?
Im not sure i understood the last one well.
Thanks
Cheers
Ram
On Mon, Sep 13, 2010 at 11:42 AM, Alex Rozenshteyn <[email protected]>wrote:
> I'm trying to build a list where each entry depends on the previous one.
> Unfoldr seemed like a good idea at the time.
> Unfortunately, my values are monadic values (specifically RVars, from the
> random-fu package). Okay, shouldn't be a problem; I just monadic bind
> and...
>
> > -- example code
> > updateCell :: Bool -> RVar Bool
> > updateCell False = return False
> > updateCell True = bernoulli (0.9 :: Double)
>
> > sim = sequence $ take 20 $ unfoldr (\x -> Just (x, x >>= updateCell))
> (return True)
>
> > runRVar sim DevURandom
>
> [True,True,True,True,True,False,True,False,True,False,False,False,False,False,False,True,True,False,False,False]
>
> That output shouldn't be possible if I'm doing things right... It appears
> that each cell has an independent history. I'm stumped.
> Advice on threading monad input in general and random-fu in specific would
> be appreciated.
>
> --
> Alex R
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100913/8522be0f/attachment-0001.html
------------------------------
Message: 5
Date: Mon, 13 Sep 2010 21:10:00 +0100
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Randomness, lists, and unfoldr
Cc: beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
On 13 September 2010 20:24, Sriram Durbha <[email protected]> wrote:
> para morphism -> kind of ana but depends on input and output generated so
> far fib, fact etc?
> Im not sure i understood the last one well.
No - a paramorphism is a generalization of fold rather than unfold.
Basically it is a foldr where you can look further into the input
(foldr only lets you see the current element). It is sometimes
confusing to use the lookahead as foldr is working "backwards" from
the right.
An apomorphism is one generalisation of unfold. Apomorphisms are
unfolds + a flush operation that operates on the final state.
Jeremy Gibbons has a number of papers that are useful here - most
other presentations build *-morphisms with fixed point functors.
See the 'fission' paper for paramorphism and the paper 'Metamorphisms:
Streaming representation-changers' for apomorphism.
http://www.comlab.ox.ac.uk/jeremy.gibbons/publications/
The "Arithmetic Coding" papers are also worth reading for this topic.
------------------------------
Message: 6
Date: Mon, 13 Sep 2010 18:53:07 -0400
From: Alex Rozenshteyn <[email protected]>
Subject: Re: [Haskell-beginners] Randomness, lists, and unfoldr
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="utf-8"
Is there a way to take a given monad's bind and wrap it to make it more
lazy? It doesn't seem like there should be, but I'm being hopeful.
On Mon, Sep 13, 2010 at 3:21 PM, Daniel Fischer <[email protected]>wrote:
> On Monday 13 September 2010 20:42:49, Alex Rozenshteyn wrote:
> > I'm trying to build a list where each entry depends on the previous one.
> > Unfoldr seemed like a good idea at the time.
> > Unfortunately, my values are monadic values (specifically RVars, from
> > the random-fu package). Okay, shouldn't be a problem; I just monadic
> > bind and...
> >
> > > -- example code
> > > updateCell :: Bool -> RVar Bool
> > > updateCell False = return False
> > > updateCell True = bernoulli (0.9 :: Double)
> > >
> > > sim = sequence $ take 20 $ unfoldr (\x -> Just (x, x >>= updateCell))
> >
> > (return True)
>
> So you get
>
> sequence [return True
> , return True >>= updateCell
> , (return True >>= updateCell) >>= updateCell
> , ((return True >>= updateCell) >>= updateCell) >>= updateCell
> , ... ]
>
> >
> > > runRVar sim DevURandom
> >
> > [True,True,True,True,True,False,True,False,True,False,False,False,False,
> >False,False,True,True,False,False,False]
> >
> > That output shouldn't be possible if I'm doing things right... It
> > appears that each cell has an independent history. I'm stumped.
> > Advice on threading monad input in general and random-fu in specific
> > would be appreciated.
>
> What you want would be something like
>
> iterateM :: (Monad m) => (a -> m a) -> a -> m [a]
> iterateM act start = do
> next <- act start
> rest <- iterateM act next
> return (start : rest)
>
> but that would only work if the bind is sufficiently lazy, otherwise you'd
> have to iterate a given number of times.
>
>
--
Alex R
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100913/8b02794e/attachment.html
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 27, Issue 31
*****************************************