[Haskell-beginners] Rewriting bits of a data structure

Adrian May adrian.alexander.may at gmail.com
Wed May 22 01:16:46 CEST 2013


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" <daniel.trstenjak at gmail.com> 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
> Beginners at haskell.org
> 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.htm>


More information about the Beginners mailing list