As I mentioned in a previous blog post, I have been recently experimenting with Colour Algorithms in bash.
In that post I discussed, at some length, the hiccups I faced and how Bash was not the wisest of choices.
[…] bash supports only integer operations. In retrospect, this should have been a sign that attempting to create an algorithm heavily dependant on floating point mathematics would be a bad idea in this chosen language!
Whoops! As I later suggested, another language could be used to make a more comprehensive tool.
I touched upon Haskell at University in two excellent modules taught by Graham Hutton, author of the language’s de facto book ‘Programming in Haskell’ and Laurence Day, a good friend of mine as well as Graham’s PhD student. I am afraid I cannot admit to being especially excellent at Haskell (I believe I gained a first class mark in the introduction module but then slipped quite poorly to a Third in the advanced module a year later), but always enjoy a challenge and happen to know the rough syntax and enough of the basics to put a few basic tests together. It was for this reason that spla.sh became spla.hs and I began the rewrite in Haskell. (Spla.hs - Github Repo)
The Haskell Rewrite
For those that are not familiar with Haskell as a language, I shall provide you a brief summary, although I urge you to go check it out for yourself.
Haskell is a functional programming language that differs from more conventionally used languages such as C, Java, etc. It is statically typed and can be used to provide some of the most elegant looking code imaginable through concise functions that rely heavily on mathematical principles such as recursion and curried functions. It encompasses a completely different programming paradigm and way of thinking to what is often seen as ‘the norm’ in programming and is a powerful tool when used correctly.
Rewriting spla.sh in Haskell has been fun. Despite the project not being completed (as of writing), I have once again fallen in love with functional programming. I hope I can use this love to write some interesting functions in the near future. However I cannot romanticise too much over the writing as challenges, as always, presented themselves.
Haskell can provide some excellent functionality concisely which (sometimes) leads to powerful functions at the expense of human-readability. One such example is the
group function within spla.hs that takes a list of objects and groups the list into a ‘list of lists’ with the individual lists being of a set length.
> group 2 [1..10]
The source of this polymorphic function is as follows:
group :: Int -> [a] -> [[a]]
group _  = 
group n l
| n > 0 = (take n l) : (group n (drop n l))
| otherwise = group (abs n) l
Possibilities (and lots of them!)
Haskell like many programming languages has excellent standard and external libraries (known as modules) available that can perform many specific tasks. However, with such extensive libraries and seemingly interchangeable syntax, it was difficult to find the right way to write certain functions. “To lambda or not to lambda, that is the question.”
More conversion issues
As I mentioned previously in my spla.sh post, I was finding issues when converting to a colour space and then immediately taking that result and converting it back. I initially thought this was an error in rounding but have now come across the same issue once more. This is still under investigation.
> hsvColorToRGBColor . rgbColorToHSVColor $ RGBColor(255,19,45)
The project is on going and I don’t want to spoil what I am going to add (Just check the issues page on GH!)
If this is your cup of tea, check out the project on Github and lets paint the world in Haskell.
Thanks for reading.
If you like what I do, have an idea I can help with or find numerous bugs in my code - please visit my github, or tweet me @EdGeorge92. I am always interested in new cool projects and learning where I have gone wrong!