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:  Using Traversal as a kind of pointer (Elise Huard)
   2. Re:  Using Traversal as a kind of pointer (Benjamin Edwards)
   3. Re:  Using Traversal as a kind of pointer (Daniel Trstenjak)
   4.  "Learn You a Haskell" question (Frank)
   5. Re:  "Learn You a Haskell" question (Bob Ippolito)
   6. Re:  Data.Stream interleave implementation        question
      (Curt McDowell)
   7. Re:  Data.Stream interleave implementation        question (Julian Birch)


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

Message: 1
Date: Mon, 18 Aug 2014 15:17:11 +0200
From: Elise Huard <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Using Traversal as a kind of pointer
Message-ID:
        <CAHfyCqkc=g890dk+16zp_1dkrfaxitfwnevgug-1ydcuc4j...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

Hi Daniel,

thanks for your answer. That looks like something that might work -
however,  having never worked with Optics, I'm not entirely sure
whether I'm doing it right, getting an error:
*Main Graphics.Rendering.OpenGL Control.Lens> :t (liveforms . filtered
(colliding p))

<interactive>:1:14:
    Couldn't match type _Lifeform_ with _[Lifeform]_
    Expected type: (Lifeform -> f Lifeform)
                   -> [Lifeform] -> f [Lifeform]
      Actual type: Optic' (->) f Lifeform Lifeform
    In the second argument of _(.)_, namely
      _filtered ((flip colliding) p)_
    In the expression: (liveforms . filtered ((flip colliding) p))
*Main Graphics.Rendering.OpenGL Control.Lens>

Also, may I ask what the '&' is in your proposed solution?
Thanks,

Elise

On 18 August 2014 13:30, Daniel Trstenjak <[email protected]> wrote:
>
> Hi Elise,
>
> I'm not quite sure if I completely understood your use case, but if
> you only want to modify the lifeforms that are colliding with the player,
> then you could have something like:
>
>    world & liveforms . filtered (collidingWith player) %~ \liveform -> ...
>
> with
>
>    liveforms :: Lens' World [Lifeform]
>
>    collidingWith :: Player -> Lifeform -> Bool
>
>
> I would consider this as a quite nice solution.
>
>
> Greetings,
> Daniel
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners


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

Message: 2
Date: Mon, 18 Aug 2014 14:31:26 +0100
From: Benjamin Edwards <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Using Traversal as a kind of pointer
Message-ID:
        <CAN6k4ni0RVK7F=3je5lpxanupenebjoz-wysoeqkfpw9wz5...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

And just remember filtered is only a valid traversal if you don't change
the number of targets with the modification function!


On 18 August 2014 14:17, Elise Huard <[email protected]> wrote:

> Hi Daniel,
>
> thanks for your answer. That looks like something that might work -
> however,  having never worked with Optics, I'm not entirely sure
> whether I'm doing it right, getting an error:
> *Main Graphics.Rendering.OpenGL Control.Lens> :t (liveforms . filtered
> (colliding p))
>
> <interactive>:1:14:
>     Couldn't match type _Lifeform_ with _[Lifeform]_
>     Expected type: (Lifeform -> f Lifeform)
>                    -> [Lifeform] -> f [Lifeform]
>       Actual type: Optic' (->) f Lifeform Lifeform
>     In the second argument of _(.)_, namely
>       _filtered ((flip colliding) p)_
>     In the expression: (liveforms . filtered ((flip colliding) p))
> *Main Graphics.Rendering.OpenGL Control.Lens>
>
> Also, may I ask what the '&' is in your proposed solution?
> Thanks,
>
> Elise
>
> On 18 August 2014 13:30, Daniel Trstenjak <[email protected]>
> wrote:
> >
> > Hi Elise,
> >
> > I'm not quite sure if I completely understood your use case, but if
> > you only want to modify the lifeforms that are colliding with the player,
> > then you could have something like:
> >
> >    world & liveforms . filtered (collidingWith player) %~ \liveform ->
> ...
> >
> > with
> >
> >    liveforms :: Lens' World [Lifeform]
> >
> >    collidingWith :: Player -> Lifeform -> Bool
> >
> >
> > I would consider this as a quite nice solution.
> >
> >
> > Greetings,
> > Daniel
> > _______________________________________________
> > Beginners mailing list
> > [email protected]
> > http://www.haskell.org/mailman/listinfo/beginners
> _______________________________________________
> 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/20140818/ae439108/attachment-0001.html>

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

Message: 3
Date: Mon, 18 Aug 2014 16:03:22 +0200
From: Daniel Trstenjak <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Using Traversal as a kind of pointer
Message-ID: <20140818140322.GA28516@machine>
Content-Type: text/plain; charset=us-ascii


Hi Elise,

> thanks for your answer. That looks like something that might work -
> however,  having never worked with Optics, I'm not entirely sure
> whether I'm doing it right, getting an error:

oh sorry, there has to be a 'traversed' before the 'filtered'.

> Also, may I ask what the '&' is in your proposed solution?

The '&' is just applying a lens to a variable.


So here's a working example:

   {-# LANGUAGE Rank2Types #-}
   
   import Control.Lens
   
   type Background = Int
   type Lifeform   = Int
   type Player     = Int
   
   
   data World = World Background [Lifeform] deriving (Show)
   
   
   lifeforms :: Lens' World [Lifeform]
   lifeforms = lens getLifeforms setLifeforms
      where
         getLifeforms (World _ lifeforms)    = lifeforms
         setLifeforms (World bg _) lifeforms = World bg lifeforms
   
   
   colliding :: Player -> Traversal' [Lifeform] Lifeform
   colliding player = traversed . filtered (== player)


If you load this into ghci und can write:

   > World 1 [1, 2] & lifeforms . colliding 1 %~ (+ 10)
   > World 1 [11,2]


Greetings,
Daniel


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

Message: 4
Date: Mon, 18 Aug 2014 14:02:46 -0400
From: Frank <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] "Learn You a Haskell" question
Message-ID:
        <CA+a3wk+KHiM4WD9uzesjcg=3yfc1jqrp3n+2hj6wu4uyj5t...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Hi,
    I'm reading "Learn You a Haskell..." and have a question about the
chapter "Making Our Own Types and Typeclasses". On the 'Functor'/'Either'
example, I feel completely lost. I don't see why the 'Left x' portion of
'Functor (Either a)' is simply 'Left x' and the document is not exactly
clear. Any clarification would be most appreciated.

Sincerely,
Frank D. Martinez


-- 
P.S.: I prefer to be reached on BitMessage at
BM-2D8txNiU7b84d2tgqvJQdgBog6A69oDAx6
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20140818/8fabed6e/attachment-0001.html>

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

Message: 5
Date: Mon, 18 Aug 2014 11:15:32 -0700
From: Bob Ippolito <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] "Learn You a Haskell" question
Message-ID:
        <CACwMPm-2RgbAJt7csms_SApQs3+3=efdbh8s+fxtnezq2eh...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

The definition of a Functor requires that exactly one of the type variables
be free, which is why it's written as `Either a` instead of `Either a b`.
Any fields that are not `b` must be simply passed through as-is by fmap.
There could be a separate functor that would fmap over the Left, but there
isn't (in the base package anyhow).

There's a related Functor for `(,) a` where the Functor fmaps over the snd
of the tuple, and the fst is left as-is.

fmap (+1) ('a', 2) == ('a', 3)
fmap (+1) (Right 2) == Right 3
fmap (+1) (Left 'a') == Left 'a'

Chris Done recently prototyped a fmap explorer that you might find useful:
http://www.reddit.com/r/haskell/comments/2dok9w/functor_explorer/

-bob



On Mon, Aug 18, 2014 at 11:02 AM, Frank <[email protected]> wrote:

> Hi,
>     I'm reading "Learn You a Haskell..." and have a question about the
> chapter "Making Our Own Types and Typeclasses". On the 'Functor'/'Either'
> example, I feel completely lost. I don't see why the 'Left x' portion of
> 'Functor (Either a)' is simply 'Left x' and the document is not exactly
> clear. Any clarification would be most appreciated.
>
> Sincerely,
> Frank D. Martinez
>
>
> --
> P.S.: I prefer to be reached on BitMessage at
> BM-2D8txNiU7b84d2tgqvJQdgBog6A69oDAx6
>
> _______________________________________________
> 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/20140818/d6fbfe75/attachment-0001.html>

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

Message: 6
Date: Mon, 18 Aug 2014 18:22:15 +0000 (UTC)
From: Curt McDowell <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Data.Stream interleave implementation
        question
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

Funny, I was trying the same Homework, implemented interleaveStreams using 
the same algorithm you did (taking one item from each stream per recursive 
call), and got the same result with the ruler function hanging. It was also 
fixed by changing interleaveStreams to take from just one stream per call 
and switch back and forth between streams.

This happened even though I implemented my ruler function itself 
recursively:

ruler' :: Integer -> Stream Integer
ruler' n = interleaveStreams (streamRepeat n) (ruler' (n + 1))

ruler = ruler' 0

Sorry, but I also don't know why. I speculate that if one were to expand it 
out on pencil and paper, they'd find that the interleaved stream somehow 
expands at a different rate than the ruler (half or twice).

-Curt




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

Message: 7
Date: Mon, 18 Aug 2014 21:24:23 +0100
From: Julian Birch <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Data.Stream interleave implementation
        question
Message-ID:
        <CAB0TuzCG6VgPCz-ZekaZKqv_5KHr98F9=qvgomlkxcpji79...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

I had the exact same problem.  It's a consequence of "pattern matching
drives evaluation", and indeed that's the lesson I learned from the
exercise.  If you match them together at the start, you end up with a
recursive call before you've generated the first item in the list.  If you
just restructure it so that the second destructure happens after you've
produced the first value, everything works.

Julian.


On 18 August 2014 19:22, Curt McDowell <[email protected]> wrote:

> Funny, I was trying the same Homework, implemented interleaveStreams using
> the same algorithm you did (taking one item from each stream per recursive
> call), and got the same result with the ruler function hanging. It was also
> fixed by changing interleaveStreams to take from just one stream per call
> and switch back and forth between streams.
>
> This happened even though I implemented my ruler function itself
> recursively:
>
> ruler' :: Integer -> Stream Integer
> ruler' n = interleaveStreams (streamRepeat n) (ruler' (n + 1))
>
> ruler = ruler' 0
>
> Sorry, but I also don't know why. I speculate that if one were to expand it
> out on pencil and paper, they'd find that the interleaved stream somehow
> expands at a different rate than the ruler (half or twice).
>
> -Curt
>
>
> _______________________________________________
> 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/20140818/84f1000b/attachment.html>

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

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


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

End of Beginners Digest, Vol 74, Issue 15
*****************************************

Reply via email to