Back in the day when I was studying at University I tutored a subject which taught students how to write functional code in Miranda. I loved it! The whole functional programming thing really did it for me. I was fascinated by it. Despite my interest, I never chased it up after finishing at University.
I recently stumbled on a random blog post that went into depth on some functional programming topics, and that resparked my interest.
So after a few years off, I’m now back into toying with functional lanauges. Right now, the one I’m using is Haskell, which is considered a pure functional programming language (by that I mean it has no side effects).
I’m still in the learning phase, and hence am not an expert. But I do love how concise the solutions are to some seriously crazy problems.
I decided to show you a really basic program which solves a really basic problem. Its job is to generate the Roman Numeral equivalent of an integer. It’s far from complete, and doesn’t cover zero or numbers less than zero, but it does show how easy it can be to solve the problem with a very small amount of code. It’s probably far from the best solution, and there may well be a more functional approach than what I’ve listed.
So for your scrutiny (and enjoyment :)) here’s the source to that little proggy:
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
This should compile fine using GHC. If it doesn’t, feel free to abuse me. Invoke the roman function and pass it a positive integer to get the result.
Over the coming weeks I’ll start sharing my solutions to the Project Euler questions in the hope that you guys can help me improve them.
Be warned! Functional programming, once you’ve got a taste for it, can be very addictive. Start at your own risk!