Cadence SKILL - Functions

Functions

The SKILL language supports both dynamic (sometimes referred to as special) and lexical variables. However, a single function is limited to interpret its variables homogeneously. There are several ways of syntactically indicating whether a function be interpreted lexically or dynamically. One of which are to use (inSkill ..) and (inScheme ...) forms indicating dynamic and lexical scoping respectively.

(inSkill (lambda (A B) (A B)))

In the inSkill example A and B are dynamic variables in the parameter list of the lambda (the lambda list), and (A B) within the body of the function is interpreted as a call to the globally defined function A passing the value of the local dynamic variable B.

(inScheme (lambda (A B) (A B)))

In the inScheme example A and B are lexical variables in both the lambda list, and also (A B) within the body of the function which is interpreted as a call to the function A which has been passed and a parameter the value of the local lexical variable B.

inSkill and inScheme forms may be mixed within a single function with some degree of flexibility.

(inScheme (lambda (A B) (A (inSkill B) B)))

In this example a lexical 2-ary function is created which calls A, a function passed as its first argument, passing two parameters: the value of the dynamic variable B and also the value of the local lexical variable B.


SKILL supports several kinds of functions. In addition to the functions and special forms built into the language, users can create functions in their own applications of several varieties.

Anonymous functions including lexical closures.

(lambda (A) (lambda (B) (plus A B)))

Lambda functions which evaluate their arguments using normal left-to-right evaluation rules.

(defun (A B) (list A A B B))

Nlambda functions which do not evaluate their arguments, but pass their operands unevaluated at run time.

(nprocedure (A) (when (listp A) (eval A)))

Macros which are evaluated by expanding at load/compile time. The sexpressions a macro returns (evaluates to) become input for the compiler, and is thus evaluated at run time.

(defmacro nif (num if_plus if_zero if_minus) (let ((var (gensym))) `(let ((,var ,num)) (cond ((plusp ,var) ,if_plus) ((zerop ,var) ,if_zero) (t ,if_minus)))))

SKILL supports CLOS-like generic functions which are allowed to have an optional default implementation.

(defgeneric generic_add (a b c))

SKILL supports CLOS-like methods which specialize on all their required arguments. (Older versions of SKILL only support specialization of the first argument.)

(defmethod generic_add ((a fixnum) (b number) (c list)) (apply plus a b c))

Local functions of two sorts are supported with flet and labels. If local functions are defined with flet such as inner1 and inner2 below, neither one can see the other's definition.

(defun outer (flet ((inner1 (printf "hello")) (inner2 (printf "world"))) (inner1) (printf " ") (inner2) (newline)))

If local functions are defined with labels such as inner1 and inner2 below, all of them see the other's definition. Local function are only supported in lexical mode.

(defun outer (labels ((inner1 (printf "hello")) (inner2 (inner1) (printf " world\n"))) (inner2)))

Read more about this topic:  Cadence SKILL

Famous quotes containing the word functions:

    Those things which now most engage the attention of men, as politics and the daily routine, are, it is true, vital functions of human society, but should be unconsciously performed, like the corresponding functions of the physical body.
    Henry David Thoreau (1817–1862)

    In today’s world parents find themselves at the mercy of a society which imposes pressures and priorities that allow neither time nor place for meaningful activities and relations between children and adults, which downgrade the role of parents and the functions of parenthood, and which prevent the parent from doing things he wants to do as a guide, friend, and companion to his children.
    Urie Bronfenbrenner (b. 1917)

    Mark the babe
    Not long accustomed to this breathing world;
    One that hath barely learned to shape a smile,
    Though yet irrational of soul, to grasp
    With tiny finger—to let fall a tear;
    And, as the heavy cloud of sleep dissolves,
    To stretch his limbs, bemocking, as might seem,
    The outward functions of intelligent man.
    William Wordsworth (1770–1850)