2

How to modify just one field of a record without rewriting it completely?

Here I learned a helpful Haskell syntax to modify an element of a record without rewriting it completely:

oldrecord { somefield = newvalue }

Is something similar possible with tuples?

type ABigTuple = (Int, Int, Double, Int, String)

aBigTuple :: ABigTuple
aBigTuple = (5, 6, 3.2, 10, "asdf") 

anotherBigTuple = -- replace the 3rd elt of the prev tuple with 5.5 i/o 3.2

Is this possible in a manner similar to records, or do I have to rewrite the whole tuple?

  • 2
    The ‘best’ way is to using tuples and use a record instead. This helps with understanding what your type represents, too. – AJF May 13 '19 at 19:03
  • 4
    @AJFarmar I think you accidentally a word. "stop using tuples" perhaps? – moonGoose May 13 '19 at 19:35
  • 1
    @moonGoose I'm rationing my words. They don't grow on trees you know! – AJF May 13 '19 at 21:16

1 Answers1

11

I assume by "rewriting the whole tuple" you mean something like,

(\(a,b,_,d,e) -> (a,b,3.2,d,e))

There are lenses for tuples, the link has plenty of examples.

_3 .~ (3.2 :: Double)
moonGoose
  • 1,510
  • 6
  • 14