[Haskell-beginners] Rewriting bits of a data structure

Daniel Trstenjak daniel.trstenjak at gmail.com
Tue May 21 21:08:47 CEST 2013


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



More information about the Beginners mailing list