Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: OO vs functional programming: what's a suitable newsgroup?

Reply
Thread Tools

Re: OO vs functional programming: what's a suitable newsgroup?

 
 
Melzzzzz
Guest
Posts: n/a
 
      03-15-2013
On Fri, 15 Mar 2013 10:26:17 +0000, Melzzzzz wrote:

Bah copy pasting messed my formatting ;(

>
> 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
>

 
Reply With Quote
 
 
 
 
Alain Ketterlin
Guest
Posts: n/a
 
      03-15-2013
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.
 
Reply With Quote
 
 
 
 
88888 Dihedral
Guest
Posts: n/a
 
      03-15-2013
Alain Ketterlin於 2013年3月15日星期五UTC+8下午7時23分25秒 寫道:
> 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).
>


I think you are referring languages focused on the static compilation
approach for optimized objective parts linked statically to be executed
in the run time.


>
>
> 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.


I can't agree with you in this point. The FP part can't do in-line
optimizations for speed in the compilation phase easily.

 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      03-15-2013
Alain Ketterlin <> writes:
>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).


One can add a new function to a base class not by modifying
the base class, but by adding a new derived class which adds
that function. And the same holds for all the subclasses.
(open-closed principle)

>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).


In Haskell, it might suffice to add just a new pattern on the
left side of the definition for the new type for those functions
that are really needed to operate on the new type. (But I don't
know Haskell that well.)


 
Reply With Quote
 
Alain Ketterlin
Guest
Posts: n/a
 
      03-16-2013
(Stefan Ram) writes:

> Alain Ketterlin <> writes:
>>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).

>
> One can »add« a new function to a base class not by modifying
> the base class, but by adding a new derived class which adds
> that function. And the same holds for all the subclasses.
> (open-closed principle)


Yes, but what I meant is the situation where you need to add a new
function for all existing classes, like, e.g., when you have a very nice
hierarchy of shapes and realize that you need to compute the bounding
box of all possible shapes.

>>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).

>
> In Haskell, it might suffice to add just a new pattern on the
> left side of the definition for the new type for those functions
> that are really needed to operate on the new type. (But I don't
> know Haskell that well.)


Yes again. My remark was more about the arrangement of syntactic
structures: OO is centered around classes, FP around functions.

-- Alain.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: OO vs functional programming: what's a suitable newsgroup? Bart van Ingen Schenau C++ 9 03-18-2013 06:28 PM
Re: OO vs functional programming: what's a suitable newsgroup? Melzzzzz C++ 14 03-17-2013 10:02 PM
Re: OO vs functional programming: what's a suitable newsgroup? Tiib C++ 7 03-17-2013 10:51 AM
Re: OO vs functional programming: what's a suitable newsgroup? Nick Keighley C++ 1 03-16-2013 10:29 PM
Re: OO vs functional programming: what's a suitable newsgroup? Stefan Ram C++ 0 03-14-2013 01:49 PM



Advertisments