这个问题在一定程度上重新接种到这个和这个对elisp的.基本上,如何读取和评估反向报价?正在发生什么过程?标准是否对此有所说明?
这是我所期望的,但它不会发生:符号`是一个读者宏 - 并被翻译成某种(BACKQUOTE ...)宏/特殊形式(类似于'被翻译(QUOTE ...)).这不会发生,事实上,Common Lisp甚至没有BACKQUOTE宏.
发生了什么(SBCL):
CL-USER> (defparameter *q* (read-from-string "`(a b ,c)"))
*Q*
CL-USER> *q*
`(A B ,C)
CL-USER> (car *q*)
SB-INT:QUASIQUOTE
CL-USER> (cdr *q*)
((A B ,C))
Run Code Online (Sandbox Code Playgroud)
与预期有所不同,但还可以.现在,,C它本身就是一个有趣的野兽:
CL-USER> (type-of (third (cadr *q*)))
SB-IMPL::COMMA
Run Code Online (Sandbox Code Playgroud)
如果没有逗号符号,则评估读取表达式是正常的:
CL-USER> (eval (read-from-string "`(a b c)"))
(A B C)
Run Code Online (Sandbox Code Playgroud)
但是如果我想用本地绑定来评估原始表达式C,则存在一个问题:
(let ((c 10)) (eval (read-from-string "`(a b ,c)")))
; in: LET ((C 10)) …Run Code Online (Sandbox Code Playgroud) 我需要用一些值填充矩阵(存储为数组数组).对于简单的扩散问题,矩阵是雅可比矩阵,看起来像这样:
J(1,1) = 1, J(N,N)=0
Run Code Online (Sandbox Code Playgroud)
并为1<n<N:
J(n,n) = -2k/dx^2 - 2*c(n)
J(n,n-1)=J(n,n+1) = k/dx^2
Run Code Online (Sandbox Code Playgroud)
其余的矩阵条目是零.
到目前为止,我有这种怪异:
(1 to: c size) collect: [ :n |
(1 to: c size) collect: [ :m |
n = 1 | (n = c size)
ifTrue: [ m = n ifTrue: [ 1.0 ] ifFalse: [ 0.0 ] ]
ifFalse: [ m = n
ifTrue: [ -2.0 * k / dx squared - (2.0 * (c at: n)) ]
ifFalse: [ m = …Run Code Online (Sandbox Code Playgroud) 我试图在常见的lisp中构建二叉搜索树.我使用CLOS定义了二进制搜索类,如下所示:
(defclass bst ()
((root :type node
:accessor tree-root
:initform nil
:initarg root)))
Run Code Online (Sandbox Code Playgroud)
我试图定义一个泛型函数,它接受树对象和一个键,如果树包含键则返回布尔值true,如果树不包含键,则返回nil.
现在我有一个泛型函数的以下定义:
(defgeneric contains ((tree bst) (key))
(:documentation "returns boolean of whether the given tree contains a particular key)
Run Code Online (Sandbox Code Playgroud)
当我将文件加载到REPL(我正在使用SBCL)时,我收到以下错误:
Required argument is not a symbol: (TREE BST)
Run Code Online (Sandbox Code Playgroud)
我误解了泛型函数的工作原理吗?我似乎无法正确定义功能.
我需要稍微概括子类中的默认槽值.
例:
(defclass class-a ()
((slot-1 :initarg :slot-1 :initform #'identity)
<...> other-slots<...>))
Run Code Online (Sandbox Code Playgroud)
它的子类是
(defclass class-b (class-a)
((slot-2 :initarg :slot-2 :initform 0)))
Run Code Online (Sandbox Code Playgroud)
但是#'IDENTITY作为默认值不够好,稍微更一般
(lambda (&rest x) x)
Run Code Online (Sandbox Code Playgroud)
因为它期望有多个论点(我认为它与Liskov原理并不矛盾)会更适合.覆盖的最佳方法:INITFORM是CLASS-B什么?
INITIALIZE-INSTANCE :AFTER的CLASS-B,看看是否SLOT-1被设置为#'IDENTITY和覆盖它.SLOT-1的CLASS-B?我想避免它,因为我必须重复它的所有插槽信息.以下程序(64 位 YASM)从标准输入读取 4 个字节并退出:
section .data
buf db " " ; Just allocate 16 bytes for string
section .text
global _start
_start:
mov rax, 0 ; READ syscall
mov rdi, 0 ; STDIN
mov rsi, buf ; Address of the string
mov rdx, 4 ; How many bytes to read
syscall
; Exit:
mov rax, 60
mov rdi, 0
syscall
Run Code Online (Sandbox Code Playgroud)
一旦编译
yasm -f elf64 -l hello.lst -o input.o input.asm
ld -o input input.o
Run Code Online (Sandbox Code Playgroud)
如果它像这样运行
./input
Run Code Online (Sandbox Code Playgroud)
比如说,123456\n作为用户输入,它将消耗1234 …
common-lisp ×3
clos ×2
lisp ×2
assembly ×1
bash ×1
c ×1
elisp ×1
linux ×1
pharo ×1
reader-macro ×1
smalltalk ×1
system-calls ×1