检测#<未指定>.在计划列表中 [英] Detecting #<unspecified> in Scheme list

查看:79
本文介绍了检测#<未指定>.在计划列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回值列表的函数.其中一些值本身可能是空列表,而有些则不是.但是,在每个列表的末尾都有一个#<unspecified>值.我了解该函数不返回任何内容时将返回此值.

I have a function which return a list of values. Some of these values may be empty lists themselves, while some are not. However, at the end of every list, there is a #<unspecified> value present. I understand that this value is returned when the function does not return anything.

我要修剪此值以及其他空列表.

I want to trim this value, along with other null lists.

我的列表是这样的:
(() () MD- MC+. #<unspecified>)

My list is like this:
(() () MD- MC+. #<unspecified>)

我打算将过滤器功能应用于此列表. 我将要应用的标准是null?.
但是,当将此值应用于#<unspecified>值时,它给我带来了错误.如何从列表中删除#<unspecified>值?

I intend to apply a filter function to this list. The criteria that I will be applying is null?.
However, when this is applied to the #<unspecified> value, it gives me false. How can I remove the #<unspecified> value from the list?

应用过滤器功能后,此列表的输出应为: (MD- MC+)

The output of this list after applying the filter function should be: (MD- MC+)

我该怎么做?

推荐答案

您的列表不是正确的列表,而是点分列表.所有更高阶的函数,例如filterfoldmap,...,都要求列表正确,因此无法使用此类列表.

Your list is not a proper list but a dotted list. All higher order functions like filter, fold, map, ... require the lists to be proper so such list cannot be used.

我想知道您是否受困于此类列表的原因可能是由于生成该列表的过程中存在错误.通常,如果您有递归过程..

I'm wondering if perhaps the reason you are stuck with such list is because of a bug in the procedure that produced the list. Usually if you have a recursive procedure..

(define (list-add1 lst)
  (if (pair? lst) 
      (cons (add1 (car lst)) (list-add1 (cdr lst)))))

现在,每个计划者都立即看到这与以下内容相同:

Now, every schemer sees right away that this is the same as:

(define (list-add1 lst)
  (if (pair? lst) 
      (cons (add1 (car lst)) (list-add1 (cdr lst)))
      'UNDEFINED-IMPLEMENTATION-SPECIFIED-VALUE))

使用后,您将把适当的列表更改为虚线列表:

And that when used you will change a proper list into a dotted list:

(list-add1 '(1 2 3)) ; ==> (2 3 4 . UNDEFINED-IMPLEMENTATION-SPECIFIED-VALUE)

解决方法是修复使点列表处理if的两个分支的过程.例如.

The fix is to fox the procedure that makes the dotted list to handle both branches of if. eg.

(define (list-add1 lst)
  (if (pair? lst) 
      (cons (add1 (car lst)) (list-add1 (cdr lst)))
      '()))

(list-add1 '(1 2 3)) ; ==> (2 3 4)

当然,如果不是这样,您可以通过将最终值保留在其自身的缺点中或将其删除来将点转换为适当的值:

Of course if thats not the case, you can transform a dotted to proper by either keeping the final value in its own cons or dropping it:

(define (dotted->proper lst keep-value)
  (cond ((pair? lst) (cons (car lst) (dotted->proper (cdr lst) keep-value)))
        ((null? lst) '())
        (keep-value (cons lst '()))
        (else '())))

(dotted->proper '(1 2 . 3) #f) ; ==> (1 2)
(dotted->proper '(1 2 . 3) #t) ; ==> (1 2 3)

这篇关于检测#&lt;未指定&gt;.在计划列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆