Common Lisp-展平可能包含符号的列表 [英] Common Lisp - flatting a list that may contain symbols

查看:95
本文介绍了Common Lisp-展平可能包含符号的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是 99个Lisp问题:将一个列表转换为一个列表,方法是将每个列表以其元素替换(递归),从而将列表作为元素保留为扁平"列表.我尝试了几种解决方案,例如从# 2680864 从这里.它们都可以工作,但是如果我将包含引号的元素的列表展平,就会遇到问题.例如:

This is #7 of of 99 Lisp problems: transform a list, possibly holding lists as elements into a `flat' list by replacing each list with its elements (recursively). I have tried several solutions, e.g from #2680864 or from here. They all work, but I run into a problem if I am flattening a list containing a quoted element. E.g.:

> '(a 'b c)
(A 'B C)

> '(a (quote b) c)
(A 'B C)

> (flatten '(a 'b c))
(A QUOTE B C)

在后一种情况下,我想获得:

In the latter case I would like to get:

(A 'B C)

似乎'的内部表示方式妨碍了该任务! SBCL,CLISP,ECL等...它们的行为均相同.

It seems that the internal representation of ' gets in the way for this task! SBCL, CLISP, ECL, ... they all behave the same way.

推荐答案

列表中引用的元素?那通常是没有道理的.为什么要引用它?

Quoted elements in a list? That usually does not make sense. Why would you have it quoted?

(a b c)是三个符号的列表.为什么要引用列表中的元素?像在(a 'b c)中一样?为什么?报价的目的是什么?

(a b c) is a list of three symbols. Why would you quote elements in the list? like in (a 'b c)? Why? What would be the purpose of the quote?

在Common Lisp中,'是一个只读宏,它将'a扩展为(QUOTE A).由于这是一个普通列表,因此典型的展平操作会将符号QUOTEA收集到展平列表中.这是因为flatten函数通常会检查某物是否为原子.如果您不希望这样做,则您的flatten函数需要检查某事物是原子还是包含两个元素的列表,并且以QUOTE作为其第一个符号.

In Common Lisp ' is a readmacro which expands 'a into (QUOTE A). Since this is a normal list, a typical flatten operation will collect the symbols QUOTE and A into the flat list. This is because a flatten function typicall checks whether something is an atom or not. If you don't want this, your flatten function needs to check if something is an atom or a two-element list with QUOTE as its first symbol.

但是如上所述,默认用法只是使符号变平,因为带引号的符号在列表中通常没有用.否则,您需要扩展flatten函数.

But as I said above, the default usage is just to flatten symbols, since quoted symbols are usually not useful inside a list. You need to extend the flatten function otherwise.

例如:

(defun flatten (l &key (test #'atom))
  (cond ((null l) nil)
        ((funcall test l) (list l))
        (t (loop for a in l nconc (flatten a :test test)))))


CL-USER > (flatten '(a (('b) c) ('d) )
                   :test (lambda (item)
                           (or (atom item)
                               (and (eq (first item) 'quote)
                                    (null (cddr item))))))

(A (QUOTE B) C (QUOTE D))

这篇关于Common Lisp-展平可能包含符号的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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