处理两个复杂输入(删除列表中重复符号的列表)

发布于 2024-10-09 16:14:43 字数 1701 浏览 1 评论 0原文

这个问题来自htdp<的练习17.6.5 /a>.我需要一些有关如何确定列表列表中一对符号是否唯一的指导。

这是我到目前为止得到的结果:

;; pair : symbol, list of symbols -> list of list of symbols

;; an auxiliary function that returns a list of possibly non-unique 
;; lists of pairings from a name (symbol) and a list of names (list of symbols).

;; output from (pair 'Alice '(Bob Charlie))
;; (list 
;; (list 'Alice 'Bob) (list 'Bob 'Alice) (list 'Alice 'Charlie)
;; (list 'Charlie 'Alice) (list 'Bob 'Charlie) (list 'Charlie 'Bob))

(define (pair name alist)
  (cond
  [(empty? alist) empty]

  [(cons? alist) (append (pair name (first alist))
                         (pair name (rest alist))
                         (pair (first alist) (rest alist)))]

  [else (list (append (list name)(list alist))
              (append (list alist)(list name)))]))

如果将此函数应用于问题(5 个名称),它会生成 30 个符号对列表,其中 10 个是重复的。去除它们的适当方法是什么?在其他语言中,解决此问题的一种方法是通过创建数据结构并将每个项目插入该结构中来引入副作用,但前提是该项目尚未包含在该结构中。

我考虑过排序,但意识到我无法对符号进行排序,因为我只能测试是否相等,所以我几乎被困在这里,希望能得到任何见解。

编辑: 我添加了“非相同”的函数定义,它使用名称(符号)并生成对列表。

(define (non-same name alist)
  (cond
    [(empty? alist) empty]
       [(equal? name (first (first alist))) (cons (first alist) (non-same name (rest     alist)))]
       [else (non-same name (rest alist))]))

以下测试通过:

(non-same 'Mary (pair 'Mary '(Jane Laura Dana Louise)))
;; outputs (list (list 'Mary 'Jane) (list 'Mary 'Laura) (list 'Mary 'Dana) (list 'Mary 'Louise))

所以看来我走在正确的轨道上。不幸的是,我似乎是通过机缘巧合找到了解决方案,现在必须思考它是如何工作的,哈哈。

感谢所有阅读此问题的人,特别是克里斯的有用评论。

This question is from Exercise 17.6.5 of htdp. I need some guidance on how to determine if a pair of symbols is unique in a list of lists.

Here's what I've got thus far:

;; pair : symbol, list of symbols -> list of list of symbols

;; an auxiliary function that returns a list of possibly non-unique 
;; lists of pairings from a name (symbol) and a list of names (list of symbols).

;; output from (pair 'Alice '(Bob Charlie))
;; (list 
;; (list 'Alice 'Bob) (list 'Bob 'Alice) (list 'Alice 'Charlie)
;; (list 'Charlie 'Alice) (list 'Bob 'Charlie) (list 'Charlie 'Bob))

(define (pair name alist)
  (cond
  [(empty? alist) empty]

  [(cons? alist) (append (pair name (first alist))
                         (pair name (rest alist))
                         (pair (first alist) (rest alist)))]

  [else (list (append (list name)(list alist))
              (append (list alist)(list name)))]))

If this function is applied to the question (5 names), it yields 30 lists of pairs of symbols, of which 10 are duplicates. What is an appropriate way to remove them? In other languages one way to solve this problem is to introduce a side-effect by creating a data structure and to insert each item into the structure, but only if it is not already in it.

I've considered sorting but realized that I'm unable to sort symbols since I can only test for equality, so I'm pretty much stuck here and would appreciate any insight.

Edit:
I've added my function definition of "non-same" which consumes a name (symbol) and produces lists of pairs.

(define (non-same name alist)
  (cond
    [(empty? alist) empty]
       [(equal? name (first (first alist))) (cons (first alist) (non-same name (rest     alist)))]
       [else (non-same name (rest alist))]))

The following test passes:

(non-same 'Mary (pair 'Mary '(Jane Laura Dana Louise)))
;; outputs (list (list 'Mary 'Jane) (list 'Mary 'Laura) (list 'Mary 'Dana) (list 'Mary 'Louise))

So it seems I'm on the right track. Unfortunately it seems I've arrived at the solution through serendipity and now must wrap my brain around how it works LOL.

Thanks for all who read this question and especially to Chris for his helpful comments.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倚栏听风 2024-10-16 16:14:43

从读到这个问题,我有一种感觉,你找错了树。

您只需要三个函数:random-picknon-samearrangements。最后一个已在练习 12.4.2 中定义。

我从你的问题推测你想实现非相同。您将需要实现一个传递给 filter 的函数(或任何 HtDP 等效项)。然后,就很简单了:(

(define (non-same names arrangements)
  (define (usable? arrangement)
    ...)
  (filter usable? arrangements))

当然,您自己填写 ... 。该函数只需要返回一个布尔值,并且您不必触摸 append完全(正如您在问题中所做的那样)。)

我希望这足以提供暗示。如果您需要其他提示,请询问。 :-)

From reading the question, I have a feeling that you're barking up the wrong tree.

You only need three functions: random-pick, non-same, and arrangements. The last one is already defined in exercise 12.4.2.

I presume from your question that you want to implement non-same. You will want to implement a function that you pass into filter (or whatever the HtDP equivalent is). Then, it's as simple as:

(define (non-same names arrangements)
  (define (usable? arrangement)
    ...)
  (filter usable? arrangements))

(Fill in the ... yourself, of course. That function needs only return a boolean value, and you should not have to touch append at all (as you did in your question).)

I hope that's enough of a hint. If you need another hint, just ask. :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文