or ‘Why Haskell Users Should Consider….ActionScript !?’
If you’re a Haskell programmer, you’re probably a little worried about the title of this article. “ActionScript?,” you say, puzzled, “Isn’t that that messy scripting language that glues together Flash animations?” Well yes. And no. As it turns out, ActionScript originated as a bit of an ugly duckling, and as, rather aptly, a way to script actions. But now it’s blossomed, it really is a rather lovely little swan, and it’s especially handy if you’re a functional programmer.
Functions as first class values
Actionscript has them. And uses them. Extensively. Which means you’ll feel very at home writing equivalent of the lambda expression \x.E, which is
function(x){ return E; }
Admittedly not quite as brief as in Haskell, but also, debatably, not quite as cryptic. There’s no form of type inference, so you’ll have to add types manually:
function(x:Number):Number{ return x+1; }
Of course, functional application is also there, and
(function(x){ return x; }) (1)
evaluates to 1. The two basic ingredients for some pretty nifty functional programming ownage.
Arrays (Lists)
What are termed Array in ActionScript programming are, to most extents and purposes, equivalent to lists in functional programming. Literals like [] and [0,2,3] are accepted, and map and filter are native higher-order functions with implementations built into the Flash Player.
Language Warming
To get your ActionScript environment to an even nicer plane of purity, here are some extra nice bits that I normally define in a utility class before starting on a project. These are roughly based on the Haskell prelude.
// identity
function id(x:*):* {return x;}
// curry
function curry(f:Function):Function{
return function(x):Function{
return function(y) {
return f(x,y);
}
}
}
// uncurry
function uncurry(f:Function):Function {
return function(x,y){return f(x)(y);}
}
// constant function
function const(x){ return function(y){return x;} };
// procedural style foldl - uncurried
function foldl(f:Function, e:*, xs:Array):*{
for each(var x in xs)
e = f(e,x);
return e;
}
// functional composition - uncurried
function o(f1:Function,f2:Function):Function {
return function(a:*):*{
return f1(f2(a));
}
}
// summation - uncurried
function sum(x:Number,y:Number):Number { return x+y; }
Now you can write code that looks and feels quite a bit like Haskell. For example, you can now sum the array xs simply with:
foldl(sum,0,xs)
Usage in Practice
Of course, ActionScript isn’t really a functional language, being much more heavily based on OOP principles (a mix a bit like Scala). This does cause some pain. For example, notice that I tend to use uncurried definitions quite a lot of the time because there are no short hands for defining higher-order functions. Alas, there’s also no pattern matching and all functions get lumped under the Function type.
However, on the flip slide, you can use it to develop scalable applications with amazing potential. The Flash Player is a pretty much ubiquitous VM, and now that AIR (being slow, I only just noticed that it’s RIA backwards) is out, you can deploy to the desktop if you like too. Everything is JIT compiled, and the VM is opening up and rapidly converging with the HTML/javascript world. XML types are native, and the e4x access syntax is an incredibly nice way to access tree data. The unusual Flash event model also allows for Tcl/Tk-style variable binding, so that building GUIs in Flex/MXML is a breeze.
Some of the other opportunities are amazing. For instance, I’ve recently build The Effect Generator - an online app for creating Flash movies from templates. Part of the code dynamically loads and executes foreign bytecode from external web sources, but Adobe’s great security features allow’s you to keep things under control. Using the heirarchical security model, you can load remote classes and execute methods without the loaded code having access to your own classes. Scope for runtime disaster: huge. Scope for fun: priceless.
But…Scripting?
Nevertheless, something tells me ActionScript hasn’t quite been getting the attention it deserves, and that maybe it’s something related to how some folk react to the the name. Script…..scripting….informal….script kiddies…..kiddies? If you’re thinking like this, be aware that ActionScript is a seriously powerful programming language, and your reaction may well just be a defense mechanism to avoid picking up something new, fresh, and amazing. When was the last time you saw something like a stunning online photo editor written in Haskell?
Tags: dynamic, functional programming, haskell, lambda, lisp, ocaml, prelude, security, VM










You may want to look at haXe if you wish to use Flash in a functional context. It’s a language slightly different from Actionscript and, (in addition to other cool features) includes a full array of functional constructs in its standard library.
[...] The Effect Generator: ActionScript for Functional Programmers [...]