Tax Brackets
January 26, 2021
We represent the tax brackets as a list of pairs, arranged from high to low:
(define brackets '((100000 0.4) (30000 0.25) (10000 0.1)))
Then the tax calculation is a simple recursion:
(define (tax income brackets) (if (null? brackets) 0 (+ (* (max (- income (caar brackets)) 0) (cadar brackets)) (tax (min income (caar brackets)) (cdr brackets)))))
Then the tax calculation is a simple recursion:
> (tax 123456 brackets) 28882.4
You can run the program at https://ideone.com/T62CTT.
Perl
Forgot to say – the trick here is to only add the additional tax per bracket – so about 10K is 10%, above 30K is then (25-10)% = 15% and about 100K is then (40-25)% = 15% as well…
A solution in Racket:
Solution in use:
Here’s a solution in Python.
Output:
Solution in Java: