Code Kata – Compare Haskell and C++
Code Kata – Compare Haskell and C++
After many years of experience in object oriented programming and just talking about functional programming, I decided to get a hands-on experience by inventing the following code kata:
It’s about simple classes for agents communicating via FIFO stacks (let’s call them pipes).
The topology of the agent communication should be defined separate from the agent implementation.
The outputs are the following examples
- [cdm-link file=”5″] for the object oriented approach (C++? GHC compiler)
- [cdm-link file=”4″] for the functional part (Haskell HUGS interpreter)
My experience was
- the functional code is more compact
- it took me much longer to determine the right functional structure (but this might be due to less experience)
- side effects can be handles much easier in C++: in the example it’s necessary to have all agents in one list. The list can be easily build in the C++ constructors, in Haskell one need to add them separately (
agents = [producer, modifier, consumer]
) - C++ is stronger in class polymorphism: defining the parent object (
data Agent
), all child objects (Producer
,Modifier
,Consumer
) need already be known at parent definition - Code hiding/protection is not possible in Haskell
I do welcome your feedback on both examples! Maybe you have some additional ideas of how to facilitate better some language specialties.
Especially, I am interested in some feedback from an experienced Haskell programmer who might suggest a different approach.
Related articles
-
- Haskell’s effect on my C++: exploit the type system (vallettaventures.com)
- Why Haskell? (sean.voisen.org)