diff options
Diffstat (limited to 'src/Operators.hs')
-rw-r--r-- | src/Operators.hs | 15 |
1 files changed, 10 insertions, 5 deletions
diff --git a/src/Operators.hs b/src/Operators.hs index 45be7f3..84d2894 100644 --- a/src/Operators.hs +++ b/src/Operators.hs | |||
@@ -46,26 +46,31 @@ instance Fractional LispNumber where | |||
46 | (I a) / (F b) = F $ fromIntegral a / b | 46 | (I a) / (F b) = F $ fromIntegral a / b |
47 | (F a) / (F b) = F $ a / b | 47 | (F a) / (F b) = F $ a / b |
48 | 48 | ||
49 | arithmetic :: (LispNumber -> LispNumber -> LispNumber) -> [Expr] -> LispResult Expr | 49 | type Arithmetic = LispNumber -> LispNumber -> LispNumber |
50 | type Comparator = LispNumber -> LispNumber -> Bool | ||
51 | type UnaryBool = Bool -> Bool | ||
52 | type NaryBool = Bool -> Bool -> Bool | ||
53 | |||
54 | arithmetic :: Arithmetic -> [Expr] -> LispResult Expr | ||
50 | arithmetic op args | 55 | arithmetic op args |
51 | | length args < 2 = throwError $ ArgCount 2 args | 56 | | null args = throwError $ ArgCount 1 args |
52 | | otherwise = do | 57 | | otherwise = do |
53 | as <- mapM unwrapNum args | 58 | as <- mapM unwrapNum args |
54 | return . wrapNum $ foldl1 op as | 59 | return . wrapNum $ foldl1 op as |
55 | 60 | ||
56 | comparator :: (LispNumber -> LispNumber -> Bool) -> [Expr] -> LispResult Expr | 61 | comparator :: Comparator -> [Expr] -> LispResult Expr |
57 | comparator op args | 62 | comparator op args |
58 | | length args < 2 = throwError $ ArgCount 2 args | 63 | | length args < 2 = throwError $ ArgCount 2 args |
59 | | otherwise = do | 64 | | otherwise = do |
60 | as <- mapM unwrapNum args | 65 | as <- mapM unwrapNum args |
61 | return . BoolLiteral . all (== True) $ zipWith op as (tail as) | 66 | return . BoolLiteral . all (== True) $ zipWith op as (tail as) |
62 | 67 | ||
63 | unaryBool :: (Bool -> Bool) -> [Expr] -> LispResult Expr | 68 | unaryBool :: UnaryBool -> [Expr] -> LispResult Expr |
64 | unaryBool op args | 69 | unaryBool op args |
65 | | length args /= 1 = throwError $ ArgCount 1 args | 70 | | length args /= 1 = throwError $ ArgCount 1 args |
66 | | otherwise = BoolLiteral . op <$> unwrapBool (head args) | 71 | | otherwise = BoolLiteral . op <$> unwrapBool (head args) |
67 | 72 | ||
68 | naryBool :: (Bool -> Bool -> Bool) -> [Expr] -> LispResult Expr | 73 | naryBool :: NaryBool -> [Expr] -> LispResult Expr |
69 | naryBool op args | 74 | naryBool op args |
70 | | length args < 2 = throwError $ ArgCount 2 args | 75 | | length args < 2 = throwError $ ArgCount 2 args |
71 | | otherwise = do | 76 | | otherwise = do |