OO vs. FP is just a matter of whether you focus the nouns or the verbs. The counterargument to the OP is that surely a function that manipulates "data" is less powerful and abstract than one that manipulates objects. For example, take an "interface" or abstract data type like Array, consisting of a length() and a get(i) method. (This is really called List in Java and Seq in Scala.) There may even be an associated typ…
In Haskell I guess it would look like this: I'm still new to Haskell hopefully someone can improve it? data M = M1 | M2 | M3 deriving(Show, Eq, Ord) fromM M1 = 1 fromM M2 = 2 fromM M3 = 3 instance Num M where abs = abs a + b = fromInteger (mod ((fromM a) + (fromM b)) 3) a * b = fromInteger (mod ((fromM a) * (fromM b)) 3) a - b = fromInteger (mod (abs $ (fromM a) - (fromM b)) 3) fromInteger 0 = M3 fromInteger 1 = M1 f…
newtype BoundedInt b = BoundedInt Int
class Bound b where
boundRange :: t b -> (Int, Int)
fromBounded :: BoundedInt b -> Int
fromBounded (BoundedInt x) = x
toBounded :: Bound b => Int -> BoundedInt b
toBounded x =
let result = BoundedInt x
(minb, maxb) = boundRange result
in if minb Num (BoundedInt b) where
abs = toBounded . abs . fromBounded
negate = toBounded . negate . fromBounded
signum = toBounded . signum . fromBounded
x + y = toBounded (fromBounded x + fromBounded y)
x - y = toBounded (fromBounded x - fromBounded y)
x * y = toBounded (fromBounded x * fromBounded y)
fromInteger = toBounded . fromInteger
This assumes you want exceptions for overflow. Nowadays, you can put numbers in the type system, obviating the need for a "Bound" class, but I'm not familiar with it yet. The implementation above will also silently overflow given large enough bounds.