The less-familiar parts of Lisp for beginners — ftype

Continuing the discussion of Lisp features not necessarily encountered by a brief introduction to the language, we come to ftype.  This is a declaration, used within a declare, declaim, or proclaim, to provide a hint to the compiler as to what the inputs and outputs are of a particular function.  Because Lisp is not strongly typed, it often has to do run-time type checking on operations, and by declaring the types to the compiler, the programmer allows the compiler to avoid those run-time checks, and so can produce better optimized code.

The CLHS provides these examples:
 

(declare (ftype (function (integer list) t) ith)
         (ftype (function (number) float) sine cosine))

The first line declares that the function ith takes two arguments, the first being an integer, the second being a list.  Its output is declared to be t, which matches any legal Lisp type (i.e. nothing is said about the output type).

The second line declares that the sine and cosine functions each take a numeric argument and return a float.  Note that sine and cosine are not the names of those trigonometric functions in the Lisp standard, these will be user-defined functions of non-complex arguments.  The types of the standard sin and cos functions are a bit more complicated, as they account for single and double precision real or complex return values.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

反垃圾邮件 / Anti-spam question * Time limit is exhausted. Please reload CAPTCHA.