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.  Rewriting bits of a data structure (Adrian May)
   2. Re:  Rewriting bits of a data structure (Kim-Ee Yeoh)
   3. Re:  Rewriting bits of a data structure (Daniel Trstenjak)
   4. Re:  Rewriting bits of a data structure (Kim-Ee Yeoh)
   5. Re:  Rewriting bits of a data structure (Adrian May)


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

Message: 1
Date: Wed, 22 May 2013 00:35:35 +0800
From: Adrian May <[email protected]>
Subject: [Haskell-beginners] Rewriting bits of a data structure
To: "[email protected]" <[email protected]>
Message-ID:
        <CAD-UbzHDvzV_LAGyp+paxf=wj0+-v_fjvydoyg_12ebsdg_...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Hi All,

I have a lot of ugly code like this:

data Thing = Thing Int Int Int Int Int
rewriteFourth :: Int -> Thing -> Thing
rewriteFourth x (Thing a b c _ e) = Thing a b c x e

Is there a better way?

It was thinking about state monads that reminded me of this junk that I'd
already written a while ago. Maybe it can be done by applying five StateT
transformers, but I'm not entirely sure how to write that, or if there's a
simpler fix.

TIA,
Adrian.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130522/d75fb342/attachment-0001.htm>

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

Message: 2
Date: Tue, 21 May 2013 23:41:19 +0700
From: Kim-Ee Yeoh <[email protected]>
Subject: Re: [Haskell-beginners] Rewriting bits of a data structure
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Message-ID:
        <capy+zdto8amgdthre2majujk1abazzjmc3ub8tcoa2mieh3...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

On Tue, May 21, 2013 at 11:35 PM, Adrian May <[email protected]
> wrote:

> Is there a better way?
>

Yup. One word: lenses. If you're interested in the history of the design
space: semantic editor combinators, fclabels, data.accessors.

-- Kim-Ee
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130521/49ea434e/attachment-0001.htm>

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

Message: 3
Date: Tue, 21 May 2013 21:08:47 +0200
From: Daniel Trstenjak <[email protected]>
Subject: Re: [Haskell-beginners] Rewriting bits of a data structure
To: [email protected]
Message-ID: <20130521190847.GA1995@machine>
Content-Type: text/plain; charset=us-ascii


Hi Adrian,

> data Thing = Thing Int Int Int Int Int
> rewriteFourth :: Int -> Thing -> Thing
> rewriteFourth x (Thing a b c _ e) = Thing a b c x e
> 
> Is there a better way?

If you've nested data structures, than lenses are the way to go,
but in your case using record syntax might also help.

data Thing = Thing {
   a :: Int,
   b :: Int,
   c :: Int,
   d :: Int,
   e :: Int
   }

rewriteFourth :: Int -> Thing -> Thing
rewriteFourth x thing = thing {d = x}


Greetings,
Daniel



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

Message: 4
Date: Wed, 22 May 2013 04:43:58 +0700
From: Kim-Ee Yeoh <[email protected]>
Subject: Re: [Haskell-beginners] Rewriting bits of a data structure
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Message-ID:
        <CAPY+ZdSL2KwM873AvkOtWW93zwzsiVZfY=oqd+3fh3yjtmg...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

On Wed, May 22, 2013 at 2:08 AM, Daniel Trstenjak <
[email protected]> wrote:

> If you've nested data structures, than lenses are the way to go,
> but in your case using record syntax might also help.
>

Yes, agreed. Keep to the lightest solutions first.

-- Kim-Ee
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130522/67357eb2/attachment-0001.htm>

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

Message: 5
Date: Wed, 22 May 2013 07:16:46 +0800
From: Adrian May <[email protected]>
Subject: Re: [Haskell-beginners] Rewriting bits of a data structure
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Message-ID:
        <CAD-UbzEyo3KZzj4YiMgpEn5RgZty5VFTTqsWbDTuarT==6w...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Aha! I was using record syntax but I didn't know I could use it like that.
Thanks! Now for that lenses thing.
On 22 May 2013 03:11, "Daniel Trstenjak" <[email protected]> wrote:

>
> Hi Adrian,
>
> > data Thing = Thing Int Int Int Int Int
> > rewriteFourth :: Int -> Thing -> Thing
> > rewriteFourth x (Thing a b c _ e) = Thing a b c x e
> >
> > Is there a better way?
>
> If you've nested data structures, than lenses are the way to go,
> but in your case using record syntax might also help.
>
> data Thing = Thing {
>    a :: Int,
>    b :: Int,
>    c :: Int,
>    d :: Int,
>    e :: Int
>    }
>
> rewriteFourth :: Int -> Thing -> Thing
> rewriteFourth x thing = thing {d = x}
>
>
> Greetings,
> Daniel
>
> _______________________________________________
> 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/20130522/a7fc81cc/attachment-0001.htm>

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

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


End of Beginners Digest, Vol 59, Issue 28
*****************************************

Reply via email to