user4385532
user4385532

Reputation:

How to modify just one element of a tuple without rewriting it completely?

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?

Upvotes: 2

Views: 245

Answers (1)

moonGoose
moonGoose

Reputation: 1510

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)

Upvotes: 11

Related Questions