XLISP setf

Nyquist / XLISP 2.0 - Contents | Tutorials | Examples | Reference

# setf

Type: | - | special form (fsubr)
Source: | - | xlcont.c

## Syntax

(setf [place1 expr1 ... ])
  placeN - a field specifier which may be one of:
    symbol - set value of a symbol
    (car expr) - set car of a cons node
    (cdr expr) - set cdr of a cons node
    (nth n expr) - set nth car of a list
    (aref expr n) - set nth element of an array
    (get symbol property) - set value of a property
    (symbol-value symbol) - set value of a symbol
    (symbol-function symbol) - set functional value of a symbol
    (symbol-plist symbol) - set property list of a symbol
  exprN - an expression, which will be the new value
  returns - the new value

## Description

The 'setf' special form evaluates the field 'placeN' and sets 'exprN' as it's value. This is a generalized tool that allows you to set the value of the various data types of the system. 'setf' returns the value from 'exprN' as it's result. The specific action of 'setf' depends on the 'placeN' field. A more detailed explanation follows below the examples.

## Examples

    :; setf symbols

    (setf a 123)                          ; set a symbol A to value 123

    ;; setf symbol-values

    (setq x 'y)                           ; make symbol X with value Y
    (setf (symbol-value x) 'z)            ; set symbol that X contains (Y) to value Z

    ;; setf list elements

    (setq mylist '(a b c d))              ; MYLIST with value (A B C D)

    (setf (car mylist) 'x)                ; change CAR of MYLIST to X
                                          ;   MYLIST now is (X B C D)

    (setf (cdr mylist) '(y z da-end))     ; change CDR of MYLIST to (Y Z DA-END)
                                          ;   MYLIST now is (X Y Z DA-END)

    (setf (nth 3 mylist) 'here-i-am)      ; change 3rd of MYLIST to HERE-I-AM
                                          ;   MYLIST now is (X Y Z HERE-I-AM)

    ;; setf arrays

    (setq myarray (make-array 5))         ; make MYARRAY
    (aref myarray 2)                      ; get value of element 2 = NIL
    (setf (aref myarray 2) 'new-value)    ; set value of element 2 to value NEW-VALUE
    (print myarray)                       ; prints #(NIL NIL NEW-VALUE NIL NIL)

    ;; setf properties

    (setq person 'joe-bob)                ; make PERSON with value JOE-BOB
    (putprop person 'critic 'profession)  ; set PROFESSION property to value CRITIC
    (setf (get person 'profession)        ; change PROFESSION to value TEXAS-CRITIC
    (setf (get person 'home) 'texas)      ; add property HOME with value TEXAS

    (symbol-plist person)                 ; returns property list:
                                          ;   (HOME TEXAS
                                          ;    PROFESSION TEXAS-CRITIC)

    (setf (symbol-plist person)           ; change the property list
          '(home on-the-range
            profession movie-critic))

    (get person 'profession)              ; now returns MOVIE-CRITIC
    (get person 'home)                    ; now returns ON-THE-RANGE

## Operations

`(setf sym exprN)`
| - | Sets the value of 'symbol' to the value of 'exprN'. This is equivalent to (setq sym exprN)
`(setf (car expr) exprN)`
| - | Sets the first element of the 'expr' list to 'exprN'. 'expr' must be a list. This is equivalent to (rplaca expr exprN) except that 'setf' will return 'exprN' as the value.
`(setf (cdr expr) exprN)`
| - | Sets the tail of the 'expr' list to 'exprN'. 'expr' must be a list. This is equivalent to (rplacd expr exprN) except that 'setf' will return 'exprN' as the value.
`(setf (nth n expr) exprN)`
| - | Sets the nth element of the 'expr' list to 'exprN'. 'expr' must be a list. This allows you to set an arbitrary element of a list to an arbitrary value. Note that the list is numbered from the 0th element (0, 1, 2, 3, ...).
`(setf (aref expr n) exprN)`
| - | Sets the nth element of the 'expr' array to 'exprN'. 'expr' must be an array. This allows you to set an arbitrary element of an array to an arbitrary value. Note that the list is numbered from the 0th element (0, 1, 2, 3, ...). Note also that this is the intended way to set the value of an array element.
`(setf (get sym prop) exprN)`
| - | Sets the 'property' of 'symbol' to the value 'exprN'. If 'symbol' does not have the 'property', one will be created. This is equivalent to (putprop sym exprN prop).
`(setf (symbol-value sym) exprN)`
| - | Sets the symbol's value to contain 'exprN'. 'symbol' is an expression that must evaluate to a valid symbol, it doesn't have to exist before the 'setf' function is applied, it just has to be a valid symbol. This is equivalent to (set symbol exprN).
`(setf (symbol-plist sym) exprN)`
| - | Sets the property list of 'symbol' to 'exprN'. This allows you to change or destroy the entire property list of a 'symbol' at one time.

See the setf special form in the XLISP 2.0 manual.

Back to Top

Nyquist / XLISP 2.0 - Contents | Tutorials | Examples | Reference