更改欄位值時複製記錄

假設你有這種型別:

data Person = Person { name::String, age:: Int } deriving (Show, Eq)

和兩個值:

alex = Person { name = "Alex", age = 21 }
jenny = Person { name = "Jenny", age = 36 }

可以通過從 alex 複製來建立 Person 型別的新值,指定要更改的值:

anotherAlex = alex { age = 31 }

alexanotherAlex 的值現在為:

Person {name = "Alex", age = 21}

Person {name = "Alex", age = 31}