Skip to content

Instantly share code, notes, and snippets.

@japesinator
Last active August 29, 2015 13:56
Show Gist options
  • Save japesinator/9104335 to your computer and use it in GitHub Desktop.
Save japesinator/9104335 to your computer and use it in GitHub Desktop.
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