# 不可能就地（In-place）地删除列表的第一个元素

``````(defun delete-nth (n l)
(when (> n 0)
(let ((x (nthcdr (1- n) l)))
(setcdr x (cddr x)))))
``````

``````;; 删除第 2 个元素
(let ((l '(a b c d)))
(delete-nth 1 l)
l)
;; => (a c d)

;; 删除第 3 个元素
(let ((l '(a b c d)))
(delete-nth 2 l)
l)
;; => (a b d)
``````

``````(let ((l '(a b c d)))
(cl-assert (functionp 'delete-nth))
(delete-nth 0 l)
(cl-assert (equal l '(b c d))))
``````
1 个赞

1 个赞

``````(defun delete-head (list)
(prog1
(setcar list (cdr list))
(setcdr list nil)))
``````
``````(let ((l '(a b c d)))
l) ;; => ((b c d))
``````
``````(defun delete-head (list)
(setcdr list (cddr list))
list)
``````

``````(defun i-want-a-procedure (x) (setq x 3))
``````

1 个赞

``````ELISP> lexical-binding
t
ELISP> (let ((ok '(3 4 5)))
(funcall (lambda (ok)
(nreverse ok)) ok)
ok)
(3)

ELISP> (let ((ok (list 3 4 5)))
(funcall (lambda (ok)
(nreverse ok)) ok)
ok)
(3)
``````

@LdBeth ??

Elisp应该是pass by ref的吧

For nreverse, sequence might be destroyed and re-used to produce the result. The result might or might not be identical to sequence .

``````(nreverse l) =>  (3 2 1)
l =>  implementation-dependent
``````

for example, CLISP

``````> (let ((ok '(3 4 5)))
(funcall (lambda (ok)
(nreverse ok)) ok)
ok)
(5 4 3)
``````

Lisp 都是 pass by reference，但是沒有 Pascal/Ada/C 的那种 out parameter。虽然現在都不鼓励用 out parameter 了。

``````Procedure WriteToB(Out B : Integer);
begin
B:=2;
end;
``````

Call by value/call by reference/call by sharing/pass by reference 混在一起傻傻分不清。

The description “call by value where the value is a reference” is common (but should not be understood as being call by reference); another term is call by sharing. Thus the behaviour of call by value Java or Visual Basic and call by value C or Pascal are significantly different: in C or Pascal, calling a function with a large structure as an argument will cause the entire structure to be copied (except if it’s actually a reference to a structure), potentially causing serious performance degradation, and mutations to the structure are invisible to the caller. However, in Java or Visual Basic only the reference to the structure is copied, which is fast, and mutations to the structure are visible to the caller.

1 个赞

`M-x ielm`