CLISP special

special含义:创建变量的动态绑定。

let 创建lexical var. 

fun parameter 创建lexical var .如果正好函数参数与动态变量同名,则转化为动态变量。


1、为了将let创建的lexical var转化为使用动态绑定,则需要声明 (declare (special var)) .

2、若已声明为动态绑定变量。那么在接下来的lexical 作用域内想要再使用动态绑定,则需要声明 (locally (declare (special var-list))  ,@deal-code-list)


测试代码如下:

;----------------------------------------------------------------------------------------------------------------------------

;动态变量名与函数参数同名

(defvar *p* nil)
(defun print-p ()
(format t "*p=~a~&" *p*))
(defun test ()
(print-p)
(let ((*p* 111))
(print-p))
(print-p))
(defun test-1 (*p*)
(print-p)
(let ((*p* 111))
;(declare *p* special)
(declare (inline test-1) (special *p*))
(print-p))
(print-p))

结果如下:


;----------------------------------------------------------------------------------------------------------------------------

;动态变量名不在函数参数中

(defun print-test ()

;(declare (special x y)) ;好像没有什么影响
(format t "x=~a y=~a~%" x y)
(shiftf x y x));exchange  x y
(defun test-3 ()
(declare (special x y))
(format t "x=~a y=~a~%" x y))
(defun test-2 ()
(let (( x 111) (y 222))
;(declare (inline print-test) (special x y)); (inline print-test) 好像没有什么影响
(declare (special x y))
(setf x 123)
(print-test)
(test-3)))
(defun test-4 (x y)
(declare (special x y))
(print-test)
(test-3)
(let ((x 1111) (y 2222))
(format t "x=~a y=~a~%" x y); let variable binds
(locally (declare (special x y)) ;special binds
(format t "x=~a y=~a~%" x y))

(print-test)))

结果如下:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值