Yes, there's a nice way of updating record fields. In GHCi you can do --
> data Foo = Foo { a :: Int, b :: Int, c :: String } -- define a Foo
> let foo = Foo { a = 1, b = 2, c = "Hello" } -- create a Foo
> let updateFoo x = x { c = "Goodbye" } -- function to update Foos
> updateFoo foo -- update the Foo
Foo {a = 1, b = 2, c = "Goodbye" }
This is a good job for lenses:
data Foo = Foo { a :: Int, b :: Int , c :: String }
test = Foo 1 2 "Hello"
Then:
setL c "Goodbye" test
would update field 'c' of 'test' to your string.
You don’t need to define auxiliary functions or employ lenses. Standard Haskell has already what you need. Let’s take the example by Don Stewart:
data Foo = Foo { a :: Int, b :: Int , c :: String }
test = Foo 1 2 "Hello"
Then you can just say test { c = "Goodbye" }
to get an updated record.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With