Previous Page
Next Page

4.5. LISP-likeness

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

For example, AppleScript has lists, which are ordered collections of any values whatsoever. It provides certain primitive operations for dealing with 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 the routine:

(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 of these approaches is unmistakeable; 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 scriptvariables, handlers, and script objectspossess the same first-class status; for example, any of them can be passed as a parameter to, or returned as a result from, a subroutine. All of this has a markedly LISP -like flavor.


Previous Page
Next Page