diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-03-15 14:34:31 +0100 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-03-15 14:34:31 +0100 |
| commit | cc2593028c5f380e177adbf8905a43d665ac64cf (patch) | |
| tree | 3644e175344e1cc70b41ebb7a44cf1d9f3f2b09b /src/atom.hs | |
| parent | 18c9cfd7c1fb4baf1789f178a8d56ddb8f0f1456 (diff) | |
| download | computorv2-cc2593028c5f380e177adbf8905a43d665ac64cf.tar.gz computorv2-cc2593028c5f380e177adbf8905a43d665ac64cf.tar.bz2 computorv2-cc2593028c5f380e177adbf8905a43d665ac64cf.zip | |
atom module with rational and imaginary types and operations
Diffstat (limited to 'src/atom.hs')
| -rw-r--r-- | src/atom.hs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/atom.hs b/src/atom.hs new file mode 100644 index 0000000..d01b157 --- /dev/null +++ b/src/atom.hs @@ -0,0 +1,45 @@ +module Atom where + + +data Atom + = ARational Float + | AImaginary Float + +infixl 6 +? +(+?) :: Atom -> Atom -> Maybe Atom +(ARational a) +? (ARational b) = Just $ ARational (a + b) +(AImaginary a) +? (AImaginary b) = Just $ AImaginary (a + b) +_ +? _ = Nothing + +infixl 6 -? +(-?) :: Atom -> Atom -> Maybe Atom +(ARational a) -? (ARational b) = Just $ ARational (a - b) +(AImaginary a) -? (AImaginary b) = Just $ AImaginary (a - b) +_ -? _ = Nothing + +infixl 7 *? +(*?) :: Atom -> Atom -> Maybe Atom +(ARational a) *? (ARational b) = Just $ ARational (a * b) +(ARational a) *? (AImaginary b) = Just $ AImaginary (a * b) +(AImaginary a) *? (AImaginary b) = (AImaginary (a * b)) ^? ARational 2 +_ *? _ = Nothing + +infixl 7 /? +(/?) :: Atom -> Atom -> Maybe Atom +_ /? (ARational 0) = Nothing +(ARational a) /? (ARational b) = Just $ ARational (a / b) +_ /? _ = Nothing + +infixr 8 ^? +(^?) :: Atom -> Atom -> Maybe Atom +(ARational a) ^? (ARational b) = Just $ ARational (a ** b) +(AImaginary a) ^? (ARational 0) = Just $ ARational a +(AImaginary a) ^? (ARational 1) = Just $ AImaginary a +(AImaginary a) ^? (ARational 2) = Just $ ARational (-a) +(AImaginary a) ^? (ARational 3) = Just $ AImaginary (-a) +(AImaginary a) ^? (ARational b) = AImaginary a ^? (ARational (b - 4)) +_ ^? _ = Nothing + +instance Show Atom where + show (ARational r) = show r + show (AImaginary i) = show i ++ "i" |
