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:
{- The 'map' of roman numerals and their string equivalents -} romanMap :: [(Int,[Char])] romanMap = [ (1000, "M"), (900, "CM"), (500, "D"), (400, "CD"), (100, "C"), (90, "XC"), (50, "L"), (40, "XL"), (10, "X"), (9, "IX"), (5, "V"), (4, "IV"), (1, "I")] {- Get the roman numeral equivalent of a number using the given map -} getRoman :: Int -> [(Int,[Char])] -> [Char] getRoman _ [] = "" getRoman 0 _ = "" getRoman n ((d,r):rs) = (concat $ replicate (div n d) r) ++ (getRoman (mod n d) rs) {- Helper function that calls the core function passing in the roman numeral map -} roman :: Int -> [Char] roman n = getRoman n romanMap
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!










March 17, 2008
I wrote about a different haskell solution to this problem, and then gave some python equivalents.
March 18, 2008
Ooh, I remember playing with Miranda back in Uni a bit and being impressed, but I’ve never touched a functional language since.
March 18, 2008
Brings back memories of using SML at University for me.
I still prefer imperative languages
March 18, 2008
@Bill: Nice one. Thanks for the info mate! Welcome to my blog, I haven’t seen you around here before

@Keef: You should get back into it! It’s quite fascinating, and a lot of fun
@Gav: Why do you prefer imperative?
March 18, 2008
@Bill: That certainly puts my definition to shame, and really highlights how little I know of Haskell’s power
One of the things I need to do better at is understanding and using the libraries. I’m still only using the basics of Prelude and rarely branch out into other areas — mainly because I don’t yet know about them! Thanks for the link. Shame you don’t have comments enabled on your blog, otherwise I’d have commented
Cheers!
March 18, 2008
All the credit for the haskell goes to geezusfreek, I just liked it enough to parse it out. When a haskell solution works, everything just fits together so beautifully.
As for the comments, I’ve had them at various points, but I never consistently clean out the spam and it drives me crazy.
March 18, 2008
Is your blog custom built? There are a stack of technologies that can help keep comment spam at bay. Give me a shout if you want a hand getting something set up. I’ve had nearly 12k comment spam posts on this blog since I created it, but they were all caught automatically.
Thanks again!
March 18, 2008
I just seem to be able to understand imperative languages quicker and easier than functional languages.
Whilst I could do all the exercises at Uni I always found it hard to get my head around what the solution would be (what with all the list manipulation and different implementations of recursive functions with ending cases and the general case).
Just a different way of thinking I suppose. Maybe if I spent more time with it i’d get a better feel for it but I did spend a few months with it at Uni.
March 19, 2008
Aye it is a different way of thinking. Vastly different in fact. As an imperative programmer it’s hard to switch mindsets. If you’ve done a lot of coding in imperative languages before you even start some sort of formal study, you’re already in a different mindset and will find it hard to approach functional.
Most people just “can’t make sense of it” or wonder “why fight with functional when I can see the imperative solution already?”. Fair enough I guess. But until you’ve really got your hands dirty with it and have managed to get your head into the “functional” space, it’s kinda hard to say that you prefer one over the other.
It’s kinda like saying “I’ve always liked Apples, so why switch to Gauva?”
Stick with it, after a while you’ll “get it” and you’ll probably find that you like it
Cheers for responding mate.
March 19, 2008
Yup, definitely custom built, it’s no fun if you didn’t write your own. Actually, I was a member of the pybloxsom team before I got frustrated with CGI and basically rewrote pybloxsom in cherrypy.
I’ve written a couple comment plugins, I know about akismet, hidden fields, captchas, “which animal isn’t a kitten”, etc, and I still don’t feel like there’s a great answer out there. I dislike unthreaded comments, but if you can’t efficiently follow your comments, there’s no sense to threads. I’m considering using Disqus or something similar, because then people can have some sort of centralization.
March 20, 2008
I know the feeling. But when you’ve written a million custom CMS-like web apps, the novelty soon wears off
In case it’s not obvious, I use Wordpress. I’m very happy with it. I’m happy I don’t have to maintain it, and I’m happy that it’s constantly improved. I also like the plugins that are available.
Speaking of which, I use Akismet, Bad Behaviour and Spam Karma 2. Between the three of them, no spam comments ever get through. Most spam comments are marked as spam straight away, and I don’t even see them. Some comments that are dubious get flagged for moderation. Your last comment actually came through the moderation queue and that’s why it didn’t appear until now
Coz I’ve just woken up and there it was!
There are also a stack of comment thread plugins to make your comments threaded.
I refuse to use captchas or any other form of “human verification” system for my comments. I hate them as much as the next guy. Thankfully I don’t have to use them.
If you do decide to try other blogging software, I’d recommend Wordpress. I’m happy with it
If you don’t, I’d be keen to read a thread on your thoughts on Disqus, as I’ve heard mixed reviews (one bad point is that you can’t migrate comments out of it).
Cheers!