I have been interested in learning functional programming language during last months. My current choice is Clojure language because this is a dialect of Lisp based on JVM. So it seems to be good candidate for usage in real projects.
Since functional programming requires thinking style diffent from imperative languages like PHP, Python or Java I decided to try and implement some pure computational task.
“Eight queens” task is perfect for testing functional programming language because solution for this task is recursive. You need to put 8 queens on the chessboard so that no two queens attack each other. This task can be extended to N-sized chessboard, in this case you need to place N queens.
My solution in Clojure is written below:
How to run this program: if you have Clojure and Leiningen installed
then save this code into eight.clj
and run:
It will find all solutions and send them to console, one solution per line (92 solutions for standard chessboard):
Alternatively try to run the code through online execution service: http://www.compileonline.com/execute_clojure_online.php
As result Clojure shows itself as a very expressive language. The same conclusion is confirmed by language expressivness study. And I hope to continue my journey to the functional programming world :-)