Created
October 16, 2011 19:09
-
-
Save mcandre/1291283 to your computer and use it in GitHub Desktop.
Using data types for complex function arguments
This file contains hidden or 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
#!/usr/bin/env runhaskell | |
import Control.Monad (replicateM_) | |
data Dog = Dog { | |
name :: String, | |
breed :: String, | |
barks :: Int | |
} | |
defaultDog :: Dog | |
defaultDog = Dog { | |
name = "Dog", | |
breed = "Beagle", | |
barks = 2 | |
} | |
bark :: Dog -> IO () | |
bark dog = replicateM_ (barks dog) $ putStrLn $ (name dog) ++ " (" ++ (breed dog) ++ "): Ruff!" | |
main :: IO () | |
main = do | |
bark $ defaultDog { | |
name = "Snoopy", | |
barks = 2 | |
} | |
bark $ defaultDog { | |
name = "Wishbone", | |
breed = "Terrier", | |
barks = 3 | |
} |
Thanks, Persson. That's pretty handy.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Adding
{-# LANGUAGE RecordWildCards #-}
on top of code (after hash-bang line), you can then write