在 https://www.cliki.net/Naming+conventions我读过的页面
low-level, fast, dangerous function, or Lisp system specific implementation of foo
有人可以用这些上下文的例子再次将其翻译成这样吗?
最佳答案
拿这个功能:
(defun add (x y)
(+ x y))
这做一般加法。它支持整数、浮点数、复数和比率作为参数——Lisp 中定义的所有数字类型。
但是,如果您有:
(defun add (x y)
(declare (fixnum x y) (optimize speed (safety 0) (debug 0)))
(the fixnum (+ x y)))
这告诉编译器优化
fixnum
的函数适合汇编寄存器的值。当您使用编译为汇编的实现时,这会产生非常高效的代码,您可以通过 (disassemble (compile 'add))
进行检查。 .例如在 Allegro CL 中:cl-user(10): (disassemble 'add)
;; disassembly of #<Function add>
;; formals: x y
;; code start: #x10008ae4740:
0: 48 01 f7 addq rdi,rsi
3: f8 clc
4: 4c 8b 74 24 10 movq r14,[rsp+16]
9: c3 ret
然而,这个更快的代码是以没有任何错误检查为代价的:代码假设您传入 2
fixnum
参数,不多也不少,而且你保证结果不会溢出 fixnum
范围 - 所以不是例如(add most-positive-fixnum most-positive-fixnum)
.如果你通过传递像
(add 3.4 1.2)
这样的浮点数来打破这个 promise 或只有 arg (add 3)
你是在自找麻烦——这可能会破坏数据结构,甚至退出 Lisp。第二个函数可以调用
%add-2-fixnums
表明它是特殊的,不打算用于一般用途,而是专门用于某些参数,调用者需要非常小心。
关于common-lisp - Lisp %var 命名约定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56529769/