我正在尝试用另一个符号示例替换列表中的符号: (取代'a''(猫坐在垫子上)) ==> (一只猫坐在垫子上)所以“the”应该用“a”代替
这是我的代码,
(defun replace (item new-item list)
(cond ((null list)
list
)
((eq (first list) (item))
((rplaca list new-item)
(replace (rest list))))
))
;rplace replace the first of the cons with obj
;(defparameter *some-list* (list* 'one 'two 'three 'four)) => *some-list*
;*some-list* => (ONE TWO THREE . FOUR)
;(rplaca *some-list* 'uno) => (UNO TWO THREE . FOUR)
当我在aligra中编译它时会给我以下错误
Error: Function position must contain a symbol or lambda expression: (RPLACA LIST NEW-ITEM)
[condition type: PARSE-ERROR]
我不明白为什么要给出这个错误,因为rplace函数有两个参数。
答案 0 :(得分:2)
您的代码中存在多个不同的错误:
item
不是函数,所以不应该用括号括起来rplaca
电话周围有额外的括号,这是报告错误的实际原因(defun replace (item new-item list)
(cond ((null list)
list)
((eq (first list) item)
(rplaca list new-item)
(replace item new-item (rest list)))
(t
(replace item new-item (rest list)))))
(setq l '(a cat sat on a mat))
(replace 'a 'the l)
l ;; -> (the cat sat on the mat)
另外,正如评论中所指出的那样,习惯上不是文字;您可能想要构建一个新列表,例如:
(defun replace-1 (item new-item list)
(mapcar (lambda (car)
(if (eq car item)
new-item
car))
list))
(replace-1 'a 'the '(a cat sat on a mat))
;; -> (the cat sat on the mat)