Skip to content

Instantly share code, notes, and snippets.

@deech
Last active January 30, 2021 00:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deech/fd3ad2f15708b64e6511f82a619734a2 to your computer and use it in GitHub Desktop.
Save deech/fd3ad2f15708b64e6511f82a619734a2 to your computer and use it in GitHub Desktop.
Record Update with RecordWildCards
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE NamedFieldPuns#-}
data C = C { c::[Int] } deriving Show
data B = B { b::Bool, c::C } deriving Show
data A = A { a::String, b::B } deriving Show
mkA = A "hello world" (B True (C [1,2,3]))
updateC A{b=B{c=C{..},..},..} =
let c = [2,3,4] -- not confused by the 'c' in 'B'!
b = False
in A{b=B{c=C{c,..},..},..}
main = print (updateC mkA)
-- A {a = "hello world", b = B {b = False, c = C {c = [2,3,4]}}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment