Terms in Functional Programming
Well I am trying to rediscover “Functional Programming” these days, playing around with Common Lisp and Clojure. Found lots of terms that I have little or no understanding of. Think others may be like me and just compile them here for others to learn. The examples are mainly taken from Wikipedia and some from talks I’ve heard. Feel free to suggest what else should be added to the list.
Closure
As defined in Wikipedia “a closure is a first-class function with free variables that are bound in the lexical environment. Such a function is said to be “closed over” its free variables”
Let see an example,
var digit_names=function(){
var digits=["zero","one","two","three",...];
return function(n){
return digits[n];
}
}
In this case the digit_name are the free variables, and it returns a new function with one parameter “n”, that will display the text representation of the number. Notice that even when the function extits, it still maintain the digit_name array in the new function, so the new function is closed over the digit_name array.
Another javascript example this time in jQuery.
var add_link=function(id,text){
$("#"+id).click(function(){
alert("Hello World"+text);
});
}
Functions as first class arguments
What the hell is this ? Basically first class objects are things that you can pass them into function/methods and also return them from methods. In Java that means primitives like int,char and objects like Strings, Integer and your own custom objects.
So if a language supports first class functions that means you can pass functions as a argument and also returns functions from functions.
Exampls of such languages are Common Lisp, Scheme, Clojure, Javascript.
(defn is10? [b]
(if (= b 10) true nil))
(filter a [1 2 3 4 5 10])
The above example in clojure defines a is10? that checks if a number is equals to 10. We then pass the is10? to a filter function that will remove all numbers except where the number is 10. The filter function coincidentally is called a HIgher level function.
Metaprogramming
Sounds quite abstract, programming is programming what the hell is the “meta” word doing there. Metaprogramming simply said are programs that write or manipulate other programs as data.
An example in Bash from Wikipedia.
#!/bin/bash
# metaprogram
echo '#!/bin/bash' >program
for ((I=1; I<=992; I++)) do
echo "echo $I" >>program
done
chmod +x program
This one is a program that writes a program that prints 1 to 992.
Compliers are another examples of metaprogramming, translating programs into bits and bytes that can be executed. C++ templates are another example.
ROR ActiveRecord patterns, Hibernate frameworks are examples of frameworks that use metaprogramming to generate code base on your database structure/definations.
Homoiconicity
Basically it means that the program you are writing is actually some primitive data structure in the language itself. Sounds weird right.
Languages that have this property include LISP,Clojure, Prolog, XSLT, Xquery (not exhaustive).
For example a primitive structure in LISP is the list. You can represent code in list data structure and it can be evaluated. The advantage is that it makes meta programming easier, that means it makes it easy to write programs that manipulate other programs as their data.
Below examples taken from Wikipedia.
(setf expression (list '* (list 'sin 1.1) (list 'cos 2.03)) ) -> (* (SIN 1.1) (COS 2.03)) ; Lisp returns and prints the result (third expression) ; the third element of the expression -> (COS 2.03)
You can see that the sin 1.1 * cos 2.03 code is represented is a list structure in LISP and LISP is able to process that “data” and evaluates the result of that data.
Immutable
It just means that the data cannot be changed after it has been created, the opposite is mutable. In an object orientated world most of the data is mutable that means we allow data or properties in the objects to change state. That’s why we need locking in order to access the data and make sure no one can change the data while we are changing it.
If all your data is immutable, there wouldn’t need to be locks since all data is read only. Erlang,Haskell,OCaml has only single assignment for its variables, most other languages have some keyword or syntax to denote the variable as read only, example Java uses the final keyword.
Data immutability is said to reduce the number of side effects and also makes programs easy to debug.
If you like this post and would like to support my site feel free to
buy me a beer !
June 4, 2010 at 4:54 pm
Great post. Perhaps you could mention “Memoization” as well.
January 3, 2011 at 9:38 am
[...] The busiest day of the year was June 4th with 297 views. The most popular post that day was Terms in Functional Programming. [...]