免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4336 | 回复: 4
打印 上一主题 下一主题

一个宏的解释,lisp中“文件描述符”的处理 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-07-20 15:43 |只看该作者 |倒序浏览
本帖最后由 xdshting 于 2010-07-20 16:14 编辑

1,
(defmacro with-connected-socket ((var socket) &body body)
  "Bind `socket' to `var', ensuring socket destruction on exit.

`body' is only evaluated when `var' is bound to a non-null value.

The `body' is an implied progn form."
  `(let ((,var ,socket))
     (unwind-protect
         (when ,var
           (with-mapped-conditions (,var)
             ,@body))
         (when ,var
              (socket-close ,var)))))
红色的部分,没看出是什么意思
为什么要把,socket复制给var?
难道不复制就不能保证socket关闭( ensuring socket destruction on exit.)?

2,
(defun test-close ()
  (let ( (in (open "/home/linpeng/text"))
    (format t "~a" (read-line in))
    (let ((in2 in))
      (format t "~a" (read-line in2))
      (close in2)
      (format t "~a" (read-line in2)))   ;;;这里可以读文件,奇怪,上面明明关掉了,下面同样
    (format t "~a" (read-line in))
    (close in)
    (format t "~a" (read-line in))))
这里,我是想模拟上面那个宏的操作,但是发现,close根本没用,close之后,“文件描述符”依然可用,不知道是为什么?
谢谢

3,
其实这里我想问的根本问题是,“文件描述符”是怎么传递的,
比如在第二个例子中,用(let ((in2 in)))来吧in复制给in2,如果按照引用传递(in2,in指向相同的值)来理解,当close(in2) 只后,应该不能再在使用in读取文件了,但结果相反,更夸张的是还能用in2继续读取,就像close没起做用一样


环境是最新的sbcl

另外,问一个问题
在包的定义中
(defpackage #rg.mapcar.ftp.client
  (:use #:common-lisp
        #:split-sequence
        #:usocket)
  (:nicknames #:ftp.client #:ftp)
  (:export #:ftp-connection
           #:with-ftp-connection
           #:connect-to-server))
前面的#:是什么作用?谢谢

论坛徽章:
2
白羊座
日期:2013-10-29 13:29:222015亚冠之全北现代
日期:2015-10-25 08:13:02
2 [报告]
发表于 2010-07-20 19:42 |只看该作者
1. 不好解释,应该是让 var为空时,就什么都不做吧.
2.你试过了吗?真这样的话就很变态了! 不可能成立的.
3. '#'完全是用来装B的, 为了让程序员区分新的symbol. 有无#没关系.

我知道就这么多,一起学吧.

论坛徽章:
0
3 [报告]
发表于 2010-07-20 21:39 |只看该作者
你给的代码我测试过,close 是起作用的,跟你说的不一样。

ensuring socket destruction 是有点费解。但把 socket 绑定到 var 还是可以接受的。

论坛徽章:
0
4 [报告]
发表于 2010-07-22 14:36 |只看该作者
本帖最后由 xdshting 于 2010-07-22 15:16 编辑

文件内容
[linpeng@node1 ~]$ more text
line 1
line 2
line 3
line 4
line 5
line 6
[linpeng@node1 ~]$ pwd
/home/linpeng
[linpeng@node1 ~]$

程序(没变,上面拷贝的):
(defun test-close ()
  (let ( (in (open "/home/linpeng/text")))
    (format t "~a" (read-line in))
    (let ((in2 in))
      (format t "~a" (read-line in2))
      (close in2)
      (format t "~a" (read-line in2)))   
    (format t "~a" (read-line in))
    (close in)
    (format t "~a" (read-line in))))
程序的输出:
环境是sbcl最新版 + Eclipse + cusp + fedroa9
S-SYSDEPS>
(test-close)

line 1line 2line 3line 4line 5
NIL

奇怪了,请您看看我的步骤有错吗?


又有一个新问题
(loop  :for cons on '(1 2 3 4 5 6) :do (format t "~a" (car cons)) :when (cdr cons) :do (format t ","))
这条语句中 在for,do,when前面都加了冒号,其功能与没有冒号一样,请问为什么?

我觉得这可能与关键字都是定义在keyword包中有关,我还是不知道。。。?

论坛徽章:
0
5 [报告]
发表于 2010-07-23 09:31 |只看该作者
那就怪了

1.lisp 是直接复制你的代码,只修改了下路径

  1. (defun test-close ()
  2.   (let ( (in (open "text")))
  3.     (format t "~a" (read-line in))
  4.     (let ((in2 in))
  5.       (format t "~a" (read-line in2))
  6.       (close in2)
  7.       (format t "~a" (read-line in2)))   
  8.     (format t "~a" (read-line in))
  9.     (close in)
  10.     (format t "~a" (read-line in))))
复制代码
text 直接复制你的
line 1
line 2
line 3
line 4
line 5
line 6


我的执行方式为:打开 sbcl,load 1.lisp, 执行 test-close 函数

This is SBCL 1.0.29.11.debian, an implementation of ANSI Common Lisp.
More information about SBCL is available at <http://www.sbcl.org/>.

SBCL is free software, provided as is, with absolutely no warranty.
It is mostly in the public domain; some portions are provided under
BSD-style licenses.  See the CREDITS and COPYING files in the
distribution for more information.
* (load "1.lisp")

T
* (test-close)
line 1line 2
debugger invoked on a SB-INT:CLOSED-STREAM-ERROR in thread #<THREAD "initial thread" RUNNING {AA5E5D1}>:
  #<SB-SYS:FD-STREAM for "file /home/zxl/src/text" {AAE17F1}> is closed

Type HELP for debugger help, or (SB-EXT:QUIT) to exit from SBCL.

restarts (invokable by number or by possibly-abbreviated name):
  0: [ABORT] Exit debugger, returning to top level.

(SB-KERNEL:CLOSED-FLAME
#<SB-SYS:FD-STREAM for "file /home/zxl/src/text" {AAE17F1}>)[:EXTERNAL]
0]


我用 clisp 也是类似结果。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP