dur*_*eta 3 lisp arrays common-lisp
我是Common Lisp的新手,我正在尝试学习如何使用网络套接字进行编程.在常见的lisp usocket api中,它指定了函数socket-send并socket-receive采用simple-array (unsigned-byte 8)缓冲区.
我对lisp太新了解如何在sbcl common lisp中实现这一点.看来我可以使用这些功能vector,make-array但不能使用simple-array,也不能指定类型unsigned-byte 8.
有类似以下的合理和类型安全吗?:
(let ((buffer (make-array (list-length input))) (input-length (list-length input)) )
(loop
for i upto input-length collect i do
(setf (nth i buffer) (parse-integer (nth i input))))
(usocket::socket-send socket buffer input-length)))
Run Code Online (Sandbox Code Playgroud)
如果没有,我如何完成制作我需要的那种缓冲区?
CL-USER 25 > (make-array 10
:element-type '(unsigned-byte 8)
:initial-element 0)
#(0 0 0 0 0 0 0 0 0 0)
CL-USER 26 > (describe *)
#(0 0 0 0 0 0 0 0 0 0) is a (SIMPLE-ARRAY (UNSIGNED-BYTE 8) (10))
0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
Run Code Online (Sandbox Code Playgroud)
制图:
CL-USER 32 > (map '(simple-array (unsigned-byte 8) (*)) #'char-code "foobarbaz")
#(102 111 111 98 97 114 98 97 122)
Run Code Online (Sandbox Code Playgroud)
或者更简单
CL-USER 33 > (map '(vector (unsigned-byte 8)) #'char-code "foobarbaz")
#(102 111 111 98 97 114 98 97 122)
Run Code Online (Sandbox Code Playgroud)
背部:
CL-USER 34 > (map 'string #'code-char #(102 111 111 98 97 114 98 97 122))
"foobarbaz"
Run Code Online (Sandbox Code Playgroud)