Skip to content

Instantly share code, notes, and snippets.

@mengwong
Last active June 12, 2020 14:42
Show Gist options
  • Save mengwong/695726ba3e891de7a5cd671751d19943 to your computer and use it in GitHub Desktop.
Save mengwong/695726ba3e891de7a5cd671751d19943 to your computer and use it in GitHub Desktop.
tax Credit A and B, Haskell
#!stack
-- stack --resolver lts-15.12 script
data Person = Person { pname :: String, children :: [Person] }
a = Person "Alice" []
b = Person "Bob" []
c = Person "Carol" [a, b]
data TaxCredit = TaxCredit { tcname :: String, rate :: Person -> Int }
taxCreditB = TaxCredit "Tax Credit B" (\person -> 100)
taxCreditA = taxCreditB { tcname = "Tax Credit A"
, rate = \person -> let usualresult = (rate taxCreditB) person
in if length (children person) == 2
then usualresult `div` 2
else usualresult }
main = do
mapM_ putStrLn [ unwords [ pname p ++ ":\t", tcname tc, "=", show ((rate tc) p) ]
| tc <- [taxCreditB, taxCreditA]
, p <- [a, b, c] ]
{-
20200612-22:37:23 mengwong@venice4:~/tmp/python/taxc% stack taxc.hs
Alice: Tax Credit B = 100
Bob: Tax Credit B = 100
Carol: Tax Credit B = 100
Alice: Tax Credit A = 100
Bob: Tax Credit A = 100
Carol: Tax Credit A = 50
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment