Skip to content

Instantly share code, notes, and snippets.

@deech
Last active January 30, 2021 00:35
Show Gist options
  • Save deech/fd3ad2f15708b64e6511f82a619734a2 to your computer and use it in GitHub Desktop.
Save deech/fd3ad2f15708b64e6511f82a619734a2 to your computer and use it in GitHub Desktop.

Revisions

  1. deech revised this gist Jan 29, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion recordupdate.hs
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ 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]
    let c = [2,3,4] -- not confused by the 'c' in 'B'!
    b = False
    in A{b=B{c=C{c,..},..},..}

  2. deech created this gist Jan 29, 2021.
    17 changes: 17 additions & 0 deletions recordupdate.hs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    {-# 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]
    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]}}}