User Tools

Site Tools


ch2_1_setf

SETF

SETF assigns values to places, accepts any number of arguments. The odd (first, third etc.) arguments, if they are not symbols, are evaled, and the result should represent place. The even (second, fourth etc.) arguments are evaled and the value is assigned to that place. The last set value is returned.

SETF can be used just like SETQ to assign values to symbols:

>(setf a 4)           ; set value of symbol A, just like SETQ does
4

Places are returned from several accessor functions - in LabLISP they are only the list-related functions: all the CAR, CDR combinations, the FIRST to TENTH, REST, NTH and NTHCDR.

>(setq li '(a b c))   ; prepare list LI for this example
(A B C)
 
>(setf (car li) 'd)   ; access the CAR of the list LI
D
 
>li
(D C B)               ; value in the list was changed

With SETF, it is possible to create cyclic lists:

>(setf (cdddr li) li) ; connecting the end of the list to the start
(D B C .cyc)          ; LabLISP printer will detect it 

It is not recommended to use this dangerous feature. In the present state, LabLISP has just very rudimental protection against cyclic lists. Trying to work with cyclic lists might crash, produce memory leaks, or get stuck in infinite loops on C++ level.

ch2_1_setf.txt · Last modified: 2022/02/23 08:00 by admin

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki