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 ...

Get AppleScript: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.