Rewriting bits of a data structure

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.

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

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"
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@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (3)
-
Adrian May
-
Daniel Trstenjak
-
Kim-Ee Yeoh