LISP 的初学者在这里。我正在为即将到来的 LISP 考试做准备,但遇到了一个我无法解决的问题,所以我希望有经验的人可以帮助我。
无论如何,这是我的问题:给您一个列表,其中可能包含列表作为元素。你的任务是删除给定位置的原子元素。
列表和位置作为输入参数给出。
示例: Position=5 , List=(1 (2 3) ((4)) (5 (6))) ,应该返回 (1 (2 3) ((4)) ((6)))。
这是我到目前为止所得到的......(PS 下面的代码在 imMaw 的帮助下工作,您可以检查编辑以查看我之前的错误)。
(defun number_of_atoms(List)
(atoms List 0)
)
(defun atoms(List Number)
(cond
((null List) Number)
((atom (car List)) (atoms (cdr List) (+ 1 Number)))
((+ (atoms (car List) Number) (atoms (cdr List) 0)))
)
)
(defun deleteElement(Pos List)
(deleteElementAcc Pos 1 List)
)
(defun deleteElementAcc(Pos CurrPos List)
(cond
((null List) nil)
((and (atom (car List)) (not(eql CurrPos Pos))) (cons (car List) (deleteElementAcc Pos (+ CurrPos 1) (cdr List))))
((and (atom (car List)) (eql CurrPos Pos)) (deleteElementAcc Pos (+ CurrPos 1) (cdr List)))
((cons (deleteElementAcc Pos CurrPos (car List))
(deleteElementAcc Pos (+ CurrPos (number_of_atoms(car List))) (cdr List))))
)
)