Melzzzzz <> writes:
> On Thu, 14 Mar 2013 16:23:30 +0100, Alain Ketterlin wrote:
[...]
>> Pattern matching is the same as dynamic dispatch.
[...]
> Heh, I never thought about it in that way
> What you actually say is this ?
>
> data Shape = Circle Double | Rectangle Double Double | Square Double
> deriving (Show)
>
> perimeter (Circle r) = 2*r*pi
> perimeter (Rectangle x y) = 2*(x+y)
> perimeter (Square x) = 4*x
>
> shapes = [Circle 2.4, Rectangle 3.1 4.4, Square 2.1]
>
> main = do
> putStrLn $ show $ map perimeter shapes
> putStrLn $ show shapes
>
> Actually , yes it looks like perimeter is virtual function
Yes, I think the difference is really a matter of focus: OO focuses on
data (i.e., classes), whereas FP focuses on functions.
OO makes it easy to add a new type of data (a new subclass), but makes
it harder to add a new virtual function (you'll have to modify all
classes).
FP makes it easy to add a new function, but makes it harder to add a new
type of data (you'll have to modify all functions).
-- Alain.