Last active
August 29, 2015 13:56
-
-
Save japesinator/9104335 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
import Data.Ratio | |
import Data.Complex | |
import Data.Array | |
data LispNumber = Complex (Complex Double) | |
| Rational (Ratio Integer) | |
| Float Double | |
| Integer Integer | |
lispMath :: (Num a) => (a -> a -> a) -> LispNumber -> LispNumber -> LispNumber | |
-- Complex Numbers | |
lispMath func (Complex a) (Complex b) = Complex $ func a b | |
lispMath func (Complex a) (Rational b) = lispMath func (Complex a) (Complex (fromRational b :+ 0)) | |
lispMath func (Complex a) (Float b) = lispMath func (Complex a) (Complex (b :+ 0)) | |
lispMath func (Complex a) (Integer b) = lispMath func (Complex a) (Complex (fromIntegral b :+ 0)) | |
lispMath func a (Complex b) = lispMath func (Complex b) a | |
-- Floats | |
lispMath func (Float a) (Float b) = Float $ func a b | |
lispMath func (Float a) (Rational b) = lispMath func (Float a) (Float $ fromRational b) | |
lispMath func (Float a) (Integer b) = lispMath func (Float a) (Float $ fromIntegral b) | |
lispMath func a (Float b) = lispMath func (Float b) a | |
-- Ratios | |
lispMath func (Rational a) (Rational b) = Rational $ func a b | |
lispMath func (Rational a) (Integer b) = lispMath func (Rational a) (Rational $ fromIntegral b) | |
lispMath func a (Rational b) = lispMath func (Rational b) a | |
-- Integers | |
lispMath func (Integer a) (Integer b) = Integer $ func a b |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment