Skip to content

Instantly share code, notes, and snippets.

@sleexyz
Created August 31, 2016 16:04
Show Gist options
  • Save sleexyz/a57ed2fd8762bb34cf5cd4a0f4082ad6 to your computer and use it in GitHub Desktop.
Save sleexyz/a57ed2fd8762bb34cf5cd4a0f4082ad6 to your computer and use it in GitHub Desktop.
{-# LANGUAGE RecordWildCards #-}
data Foo = Bar { a :: Int, b :: Int, c :: Int, d :: Int }
| Baz { x :: Int, y :: Int, z :: Int, w :: Int }
deriving (Show)
bar = Bar {a=1, b=2, c=3, d=4}
baz = Baz {x=10, y=20, z=30, w=40}
-- With record wildcards
--
sum2 :: Foo -> Int
sum2 Bar{..} = a + b
sum2 Baz{..} = x + y
-- without record wildcards, I need to know the number of arguments:
sum2' :: Foo -> Int
sum2' f@(Bar _ _ _ _) = a f + b f
sum2' f@(Baz _ _ _ _) = x f + y f
-- I can mediate this problem probably with Pattern synonyms...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment