DekGenius.com
[ Team LiB ] Previous Section Next Section

5.5 LISP-likeness

A number of features of AppleScript seem to suggest that someone on the original AppleScript team was fond of LISP (or some LISP dialect such as Scheme). Since I, too, am fond of Scheme, I rather like these features.

For example, AppleScript has lists, which are ordered collections of any values whatever. It provides certain primitive operations for dealing these lists, such as taking the first element, taking everything but the first element, and joining two lists into one (like Scheme's car, cdr, and cons). And AppleScript permits recursion (a subroutine calling itself).

Thus, it is possible to write AppleScript code that bears an extraordinary resemblance to Scheme code. To give an example, here's a little Scheme program that defines a routine for removing the nth element from a list, and then tests it:

(define remvix
        (lambda (ix ls)
                (cond
                        ((null? ls) 
                                '(  ))
                        ((= ix 1) 
                                (cdr ls))
                        (else
                                (cons (car ls) (remvix (- ix 1) (cdr ls)))))))
(remvix 2 '(mannie moe jack))

And here's the same thing done in just the same style in AppleScript:

on remvix(ix, ls)
        if ls is {} then
                return {}
        else if ix is 1 then
                return rest of ls
        else
                return {item 1 of ls} & remvix(ix - 1, rest of ls)
        end if
end remvix
remvix(2, {"Mannie", "Moe", "Jack"})

Even if you don't know any Scheme or any AppleScript, the structural and stylistic similarity between these approaches is hard to miss; they are in fact move for move identical, the only differences between them being matters of syntactic detail. To be sure, I've stacked the deck by deliberately writing the AppleScript routine in a Scheme-like style; but the point is that AppleScript is capable of that style, and invites it.

AppleScript also can generate closures (subroutines that remember their global environment). And there is a sense in which all the components of a script—variables, handlers, and script objects—possess the same first-class status; for example, any of them can be passed as a parameter to a subroutine. All of this has a markedly LISP-like flavor.

    [ Team LiB ] Previous Section Next Section