Structural Informatics Group (SIG) logo
Home | Projects | Demos | Downloads | Publications | Local Info | About Us | New site
Go to the first, previous, next, last section, table of contents.

setf

setf

type: special form (fsubr)
location: built-in
source file: xlcont.c
Common LISP compatible: yes
supported on: all machines

SYNTAX

(setf [ <place1> <expr1> ... ] )
        <placeN>        -       a field specifier which may be one of:
                                <symbol>                (car <expr> )
                                (cdr <expr> )           (nth <n> <expr> )
                                (aref <expr> <n> )      (get <symb> <property> )
                                (symbol-value <symb> )  (symbol-plist <symb> )
        <exprN>         -       an expression - which will be the new value

DESCRIPTION

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

EXAMPLES

                                        ; SETF SYMBOL
(setf a 123)                            ; set a symbol A to value 123   
                                        ;
                                        ; SETF SYMBOL-VALUE
(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) so that 
                                        ;   MYLIST now is (X Y Z DA-END)
(setf (nth 3 mylist) 'here-i-am)        ; change 3rd of MYLIST to 
                                        ;   HERE-I-AM so that MYLIST
                                        ;   now is (X Y Z HERE-I-AM)
                                        ;
                                        ; SETF AREF
(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)                   ;   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:

<placeN>                SETF action
-------------------------------------------------------------------
<symbol>                Sets the value of  <symbol> to the value
                        of  <exprN>.  This  is  equivalent  to a
                        (SETQ <symbol> <exprN> ).

(car <expr> )           Sets the  first  element  of the  <expr>
                        list to <exprN>.  <expr> must be a list.
                        This is equivalent  to a (RPLACA  <expr>
                        <exprN> ) except  that SETF will  return
                        <exprN>  as the  value.  (cdr  <expr>  )
                        Sets  the  tail of the  <expr>  list  to
                        <exprN>.  <expr>  must  be a list.  This
                        is  equivalent   to  a  (RPLACD   <expr>
                        <exprN> ) except  that SETF will  return
                        <exprN> as the value.

(nth <n> <expr> )       Sets the  <n>th  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, ...).

(aref <expr> <n> )      Sets the  <n>th  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.

(get <sym> <prop> )     Sets the  <prop>  of <sym> to the  value
                        <exprN>.  If <sym>  does  not  have  the
                        <prop>,  one  will be  created.  This is
                        equivalent  to  (PUTPROP  <sym>  <exprN>
                        <prop> ).

(symbol-value <symbol>) 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,
                        it just has to be a valid  symbol.  This
                        is equivalent to (SET  <symbol>  <exprN>
                        ).

(symbol-plist <symbol>) 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.


Go to the first, previous, next, last section, table of contents.