Concepts of Programming Languages

(Sean Pound) #1

700 Chapter 15 Functional Programming Languages


similar in these regards to C++ and C#. Its basis, however, is the original LISP,
so its syntax, primitive functions, and fundamental nature come from that
language.
Following is the factorial function written in Common LISP:

(DEFUN factorial (x)
(IF (<= n 1)
1
(* n factorial (− n 1)))
))

Only the first line of this function differs syntactically from the Scheme version
of the same function.
The list of features of Common LISP is long: a large number of data
types and structures, including records, arrays, complex numbers, and charac-
ter strings; powerful input and output operations; and a form of packages for
modularizing collections of functions and data, and also for providing access
control. Common LISP includes several imperative constructs, as well as some
mutable types.
Recognizing the occasional flexibility provided by dynamic scoping, as well
as the simplicity of static scoping, Common LISP allows both. The default
scoping for variables is static, but by declaring a variable to be “special,” that
variable becomes dynamically scoped.
Macros are often used in Common LISP to extend the language. In fact,
some of the predefined functions are actually macros. For example, DOLIST,
which takes two parameters, a variable and a list, is a macro. For example,
consider the following:

(DOLIST (x '(1 2 3)) (print x))

This produces the following:

1


2


3


NIL


NIL here is the return value of DOLIST.
Macros create their effect in two steps: First, the macro is expanded. Second,
the expanded macro, which is LISP code, is evaluated. Users can define their
own macros with DEFMACRO.
The Common LISP backquote operator (`) is similar to Scheme’s QUOTE,
except some parts of the parameter can be unquoted by preceding them with
commas. For example, consider the following two examples:

`(a (* 3 4) c)
Free download pdf