Skip to content

Instantly share code, notes, and snippets.

@pneu
Created May 21, 2014 03:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pneu/49cac12278bceceab382 to your computer and use it in GitHub Desktop.
Save pneu/49cac12278bceceab382 to your computer and use it in GitHub Desktop.
レコード構文で代入っぽく。
data Struct = Struct
{ str :: String
, num :: Int
}
deriving Show
assignNum :: Struct -> Struct
assignNum s = let x = str s
in x `seq` s {num = 123}
foo :: Struct
foo = Struct {str = "hello"}
main :: IO ()
main = do
print $ str foo -- 元の値: そのままprintするとnumフィールドが未定義なのでコケる
print $ assignNum foo -- 代入後の値: fooの値にnumフィールドを入れたもの。
@pneu
Copy link
Author

pneu commented May 24, 2014

assignNum s = s {num = 123}

で事足ります。
HLint-1.8.61のsrc/Apply.hsにあるclassifyを読んでた時に発見したものを残しつつ書いたので、seqのくだりが残っています。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment