【问题标题】:eliminate all subsequent numbers in lisp消除lisp中的所有后续数字
【发布时间】:2017-04-24 19:42:47
【问题描述】:

我是 lisp 新手,我在作业中遇到了一个问题,要求消除所有后续数字,只有第一个数字会出现在列表中 例如(1 1 2 1 3 1 1 1) 将是(1 2 1 3 1)

我的代码是:

;the input is (1 1 2 1 3 1 1 1)
 (defun eli(x)
   ; this condition will check if x is empty or has only one element
  (if (or(null x)(null (cdr x))) x 
    ; if the first element is 1 but the second element is not 1
    (if (and (= 1 (car x))(not (= 1 (car (cdr x)))))
         ; if true then append 1 and call the function with the rest of the list
        (cons (car x)(eli(cdr x)))
         ; if false call the function recursivaly
        (eli(cdr x))
      )))
         ; the output is (1 2 1 3 1 1)

这段代码生成(1 2 1 3 1 1)

知道我哪里做错了吗?

【问题讨论】:

  • 不清楚哪个输入生成哪个输出。还不清楚您的代码应该做什么。您可能想评论您的代码。
  • 我注释我的代码更清晰,注意真正的输出必须是(1 2 1 3 1)。我无法得到它
  • 请告诉我们 Lisp 在哪里为某些输入生成输出。发布实际的 Lisp 交互。将互动添加到您的问题中。
  • 设置问题的人可能希望它适用于 1 以外的数字...

标签: list lisp


【解决方案1】:

你的问题是这个谓词:

(and (= 1 (car x))(not (= 1 (car (cdr x))))))

这只是检查第一个元素是否是1 而第二个不是。您应该检查第一个元素是否为numberp,然后检查前两个元素是否为eql,然后跳过该元素。

【讨论】:

    猜你喜欢
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-12
    • 1970-01-01
    • 2017-02-12
    • 2014-11-04
    相关资源
    最近更新 更多