【问题标题】:LISP: remove all occurances of a particular variable in a listLISP:删除列表中特定变量的所有出现
【发布时间】:2015-09-25 07:33:14
【问题描述】:

我必须编写一个函数,我可以从中删除列表中特定变量的所有出现
我正在 Ubuntu 文本编辑器中进行编程
我的 Lisp 代码看起来是这样的:

   #! /usr/bin/clisp


(defun my-remove(lst var1)
  (cond
   ((eq lst nil) nil)
   ((eq (car (cdr lst)) var1)(cons (car lst) (my-remove (cdr(cdr lst)) var1)))
    ( t (my-remove (cdr lst) var1))
  )
)

问题是它没有从列表中删除任何元素

【问题讨论】:

    标签: lisp clisp


    【解决方案1】:

    您需要查看列表中的第一个元素。 (car (cdr lst)) 不是列表中的第一个元素。

    如果该元素与您要删除的元素相同,则您希望跳过它并继续沿列表向下移动。如果它们不相等,您希望将该元素包含在从列表中继续向下获得的结果中。

    在这两种情况下,“继续列表”位应该相同,变化在于是否包含您正在查看的元素。

    此外,您可能希望使用eqlequal 来比较元素,而不是eqeql 是传统的默认比较器。

    这看起来像家庭作业,所以我不会直接给你代码,但希望这可以帮助你朝着正确的方向前进。

    【讨论】:

    • 我尝试了很多仍然无法弄清楚...如果您可以帮助我编写代码.....我可以对其进行逆向工程并理解它
    【解决方案2】:

    你想做什么:

    if you have an empty list, return the empty list and do no more
    if the very first element of the list has the value to be purged:
      return the result of calling the "purge this value" on the rest of the list
    otherwise:
      create a new list with:
        the head of the list
        the result of purging the value from the rest of the list
    

    你在做什么:

    if there's an empty list, return the empty list and do no more
    if the second element is the value to purge:
      create a new list consisting of:
        the head of the list
        the result of purging the value from the rest of the rest of the list
    otherwise:
      return the result of purging the value from the rest of the list
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-23
      • 1970-01-01
      • 1970-01-01
      • 2010-11-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多