Created
February 2, 2011 00:21
-
-
Save wavebeem/807009 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
module Test where | |
data Range | |
= FromTo Integer Integer | |
deriving (Eq, Ord) | |
instance Show Range where | |
show (FromTo x y) = show x ++ " --> " ++ show y | |
x --> y | |
| x <= y = FromTo x y | |
| otherwise = error "Invalid range, begin > end" | |
instance Num Range where | |
(+) = rBinOp (+) | |
(*) = rBinOp (*) | |
x - y = x + negate y | |
negate x = rMap negate $ rangeFlip x | |
abs x = rMap abs x | |
signum x = rMap signum x | |
fromInteger x = x --> x | |
rMap f (FromTo beg end) | |
= FromTo (f beg) (f end) | |
pairFlip (beg, end) | |
= (end, beg) | |
rangeFlip (FromTo beg end) | |
= FromTo end beg | |
rBinOp | |
binOp | |
(FromTo beg1 end1) | |
(FromTo beg2 end2) | |
= beg --> end | |
where | |
beg = beg1 `binOp` beg2 | |
end = end1 `binOp` end2 | |
x = 1 --> 10 | |
y = 10 --> 20 | |
k = 4 | |
--- ghci output --- | |
*Test> x * (k --> k) | |
4 --> 40 | |
*Test> x * k | |
<interactive>:1:4: | |
Couldn't match expected type `Range' | |
against inferred type `Integer' | |
In the second argument of `(*)', namely `k' | |
In the expression: x * k | |
In the definition of `it': it = x * k | |
*Test> x * fromInteger k | |
4 --> 40 | |
*Test> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment