In mathematics, higher order functions are functions that map functions to functions. The typical example is the derivative, along with integration. If you've studied electromagnetism you'll have also encountered the divergence and curl operators in maxwell's equations.
In computer science, higher order functions are equivalent to those in mathematics, that is, they are functions that take functions as arguments or return functions as arguments. Depending on the language, higher order functions can be implemented "out of the box" or need some trick to achieve. For example, higher order functions are central to functional programming, whereas in Java you can only achieve the same effects through the creation of intermediate objects that contain the functions. As an excercise, here's the strategy used to achieve higher order function-like behaviour in some popular languages
Functional languages (eg Haskell): functions are first-class objects
Java: passing/returning objects with methods
C/C++: function pointers
php: passing/returning objects with methods, also eval
javascript: functions are first-class objects
The typical example of use of a higher order function is a generic sort function, where a user supplied comparator is used to achieve the desired result.
viernes, 9 de noviembre de 2007
martes, 6 de noviembre de 2007
Quines
Probably the simplest examples of metaprogramming, quines are curious little programs that write themselves. Some people go to great lengths to write the smallest possible quine for a certain language. I say for a certain language because the difficulty in writing a small quine is dominated by the languages facilities for doing so. For example, in the original BASIC, it's almost trivial:
1. LIST
because the language includes a construct that does exactly what we want. Ive taken a few minutes to write a relatively simple quine in php, here it is:
<?php
$string = '<?php
$string = \'@\';
echo substr_replace($string, addslashes($string), strpos($string, chr(64)), 1);';
echo substr_replace($string, addslashes($string), strpos($string, chr(64)), 1);
If you're wondering, 64 is the ascii code for the @ character above. Im sure you can do it in much less space, but the strategy is the same. The program code must contain a string representation of itself, and at some point you have to do some substitution and some quoting to achieve duplication and correct printing of quote literals ('). As i said before, it's as hard as the language makes it, and php makes it easy with built-in functions like addslashes, strpos and substr_replace.
Quines are not very representantive of metaprogramming, because theyre little more than curious toys. However, I will probably write more on metaprogramming and related concepts (self reference, reflectivity) as they are an important topic in artificial intelligence.
Sources: wikipedia, wikiwikiweb
1. LIST
because the language includes a construct that does exactly what we want. Ive taken a few minutes to write a relatively simple quine in php, here it is:
<?php
$string = '<?php
$string = \'@\';
echo substr_replace($string, addslashes($string), strpos($string, chr(64)), 1);';
echo substr_replace($string, addslashes($string), strpos($string, chr(64)), 1);
If you're wondering, 64 is the ascii code for the @ character above. Im sure you can do it in much less space, but the strategy is the same. The program code must contain a string representation of itself, and at some point you have to do some substitution and some quoting to achieve duplication and correct printing of quote literals ('). As i said before, it's as hard as the language makes it, and php makes it easy with built-in functions like addslashes, strpos and substr_replace.
Quines are not very representantive of metaprogramming, because theyre little more than curious toys. However, I will probably write more on metaprogramming and related concepts (self reference, reflectivity) as they are an important topic in artificial intelligence.
Sources: wikipedia, wikiwikiweb
Suscribirse a:
Comentarios (Atom)