This post is not about polyglotism for today's market. If you enjoy programming, yet missing the fun part of it, this post is to tickle your programming instincts, and reminding you once again about the loads of fun that you can get out of hacking over a seemingly weird programming language. You may be doing Java / C# in your day job, feel guilty about not yet joining the bandwagon of functional programming, and try to dabble with Haskell, Erlang, Scala or F# over the weekend and during the evenings. Or you may already have joined the bandwagon privately, forked off a branch of your favorite project repository, basking in the sunshine of Lambda The Ultimate, and eagerly awaiting the day when your manager will approach you with a Haskell or Lisp assignment in hand.
If you thought you are having enough fun with lambdas, look out for more fun with functional programming without lambdas. Lambda calculus is about functions and applications of functions on arguments. I have been just been introduced to a functional programming language named Joy, which is NOT based on application of functions to arguments.
Holy crap ? No parameters .. what do functions do ?
They execute instructions on values that they find on the stack. And this post is about enjoying the beauty and savoring the fun that learning Joy has brought forth to me. And remember, you can have that too .. remove your day job hat for some time (maybe over a few weekends) and get immersed in the beauty of recursion and theory of computability.
Let us take an example ..
Suppose you want to write a function that computes the square of a number .. here is a Joy version for that ..
>> (dup)licate the element on top of the stack and apply multiplication operator (*) on the 2 top elements of the stack.
You can have a name for the function if you want to reuse it many times ..
square == dup *
and invoke it as
or without names
5 dup *
What about cubes ?
dup dup * *
What does the following do ?
[dup dup * *] map
Ok .. possibly I have jumped too far ..
In Joy, programs evolve through 2 operations - quotation and concatenation.
Quotation can be thought of as a passive data structure and is indicated by surrounding the quoted program with square brackets. In the last example, [dup dup * *] is a quoted program. But it is also a list - in Joy lists are as well indicated within square brackets e.g. [1 2 4 6] is a list. So generically speaking lists are quoted programs in Joy. And a list can contain elements that are not literals and can be made to operate on values. And for that we need to activate the passive quotation.
Concatenation of two programs in Joy is effectively the composition of the functions denoted by the programs. Consider the example program ..
2 3 + dup *
which is effectively, the composition, denoted abstractly by *(dup(+(2, 3)))
In the above example that we left off without solving, the quoted program is activated by the combinator that follows it ..
map. Combinator is another useful device that causes execution of the quoted program on top of the stack. There are a variety of combinators in Joy, the most trivial one being the
i-combinator just dequotes the quoted program, i.e.
[dup dup * *] i
is equivalent to
dup dup * *
What about the
mapcombinator ? Let us try to run the last example where we left off .. with some actual data ..
[4 6 8 9] [dup dup * *] map
Here is what happens ..
- Joy first pushes the list of integers on the stack
- Then the quoted program is pushed .. remember quoted programs are passive - hence nothing gets executed
mapcombinator then removes the list of integers and the quoted program and constructs a new list by applying the program to each element of the list. The usual stuff that
mapis so well-known to do ..
The result is a list that contains the cubes of the original elements.
I have only scratched the surface in order to tickle you towards a functional language that is not based on lambda calculus. I am sure you will also find Joy equally interesting, and loads of fun learning.
Did you notice that we can have any element within a list ? Sounds familiar ? Yes, you got it right ! Program as data, as Joy would have it. A quoted program is data unless operated upon by a combinator. And, as this landmark paper suggests ..
"Since programs are data, it is possible to define a Y-combinator for recursion and several variants. It follows that there are self-reproducing and self-describing programs in Joy. Practical programs can be written without recursive definitions by using several general purpose recursion combinators which are more intuitive and more efficient than the classical ones."
Go read this paper .. it is an absolute joy (pun intended) if you want to learn more about recursion, combinators and computability. This paper was my starting inspiration in dabbling with Joy. You can also have it .. enJoy!