Created
May 21, 2014 03:17
-
-
Save pneu/49cac12278bceceab382 to your computer and use it in GitHub Desktop.
レコード構文で代入っぽく。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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フィールドを入れたもの。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
で事足ります。
HLint-1.8.61のsrc/Apply.hsにあるclassifyを読んでた時に発見したものを残しつつ書いたので、seqのくだりが残っています。