Advertisement
Guest User

dynvar

a guest
May 1st, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 3.23 KB | None | 0 0
  1. ;;; dynvar.lisp                                                                
  2. ;;; S. Edward Dolan                                                            
  3. ;;; Wednesday, May  1 2019                                                      
  4. ;;;                                                                            
  5. ;;; If you call FN1 the output will be:                                        
  6. ;;;                                                                            
  7. ;;; global *foo* value: 42                                                      
  8. ;;;    fn1 *foo* value: 99                                                      
  9. ;;;    fn2 *foo* value: 99                                                      
  10. ;;;    fn3 *foo* value: 99                                                      
  11. ;;;    fn1 *foo* value: 0.0333                                                  
  12. ;;; global *foo* value: 42                                                      
  13.                                                                                
  14. ;;; The thing to note here is the program isn't explicitly saving and          
  15. ;;; restoring the values. Lisp handles it behind the scenes.                    
  16.                                                                                
  17. (defvar *foo* 42)                       ;globally bind *foo* to 42              
  18.                                                                                
  19. (defun fn1 ()                                                                  
  20.   (format t " global *foo* value: ~S~%" *foo*)                                  
  21.   (let ((*foo* 99))                     ;dynamically bind *foo* to 99          
  22.     (format t "    fn1 *foo* value: ~S~%" *foo*)                                
  23.     (fn2)                               ;call fn2                              
  24.     (format t "    fn1 *foo* value: ~S~%" *foo*))                              
  25.   ;; The global binding is reinstated when the LET form completes              
  26.   (format t " global *foo* value: ~S~%" *foo*))                                
  27.                                                                                
  28. (defun fn2 ()                                                                  
  29.   ;; Here, *foo* retains the value bound in fn1                                
  30.   (format t "    fn2 *foo* value: ~S~%" *foo*)                                  
  31.   (fn3)                                 ;call fn3                              
  32.   ;; You can reset the var at-will and only the current binding will be        
  33.   ;; affected. Now fn1 will see this value.                                    
  34.   (setf *foo* 33.3e-3))                                                        
  35.                                                                                
  36. (defun fn3 ()                                                                  
  37.   ;; Again, *foo* retains the value bound in fn1                                
  38.   (format t "    fn3 *foo* value: ~S~%" *foo*)                                  
  39.   ;; You could rebind it again here in a LET form and any function called      
  40.   ;; within the body of the LET will see that value.                            
  41.   )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement