scheme - 返回所有其他元素列表的方案过程

标签 scheme

我在 Scheme 中实现这个程序时遇到了一些麻烦,尽管我认为我已经完成了 90%。不幸的是,由于这是一项家庭作业,我需要对此含糊其辞。 我想 (A B C D ) 返回 ( B D) 。但是我得到一个错误,说 The object (), passed as an argument to safe-car, is not a pair | “ 这是我的代码:

(DEFINE (other_el lis)
  (COND
   (( NULL? lis ) '())
   ((LIST? lis)
    (append (CADR lis) (other_el (CDR lis))))
   (ELSE (show " USAGE: (other_el [LIST])"))))

最佳答案

这个问题比您提出的上一个问题要简单得多。请记住,您不必在每一步都计算长度(这可能非常低效),或使用附加操作来解决它(使用 cons 代替);这是答案的结构,因为它看起来像作业我让你填空:

(define (every-other lst)
  (if (or <???>                    ; if the list is empty 
          <???>)                   ; or the list has a single element
      <???>                        ; then return the empty list
      (cons <???>                  ; otherwise `cons` the second element
            (every-other <???>)))) ; and recursively advance two elements

如果您需要先进行一些错误检查,请使用另一个函数并在确定参数正确后调用上述过程:

(define (other_el lst)
  (if (list? lst)
      (every-other lst)
      (error "USAGE: (other_el [LIST])")))

像这样使用它:

(other_el '(A B C D E G))
=> '(B D G)

关于scheme - 返回所有其他元素列表的方案过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13318388/

相关文章:

scheme - Racket Scheme 的 "design by contract"特征与 Eiffel 有何不同?

scheme - 是否可以根据要比较的数据生成相等函数?

lambda - Lisp 方案 : let to lambda

当从列表中编辑 'car' 时,方案功能不起作用

macros - Racket with-hash 宏和重命名转换器

scheme - 为什么 + 和 * 分别计算为 0 和 1?

Scheme 中的流 - 通过方案中的流映射定义整数

macros - scheme中如何用双引号写LISP宏

list - 从列表中生成方案中的所有可能性

使用 Scheme 解析字符串