【问题标题】:Homework: Sublist? checking if an item is a sublist of the first one作业:子列表?检查一个项目是否是第一个项目的子列表
【发布时间】:2012-09-15 11:22:34
【问题描述】:

所以我有这个程序需要使用具有以下属性的 Racket 在 Scheme 中编写,我很难过。该函数称为sublist?,有两个输入SL,它们都是列表。它检查 S 是否是L 的子列表并返回#t#f

示例类似于:

sublist? of (A A) and (A B C) is #f
sublist? of (A B C) and (A B D A B C D) is #t
sublist? of (A (B)) and (C ((A (B))) (C)) is #t

需要创建一个名为extractLists 的小函数来提取列表,(atomicSublist S L) 用于检查两个提取的列表以查看S 的每个元素是否在L 中。

目前为止

(define (atomicSublist S L)
  (cond ((null? L) #f)
        ((equal? S (car L)) #t)
        (else (atomicSublist S (cdr L)))))

第二部分并没有真正做任何事情,甚至不输出S的提取值。

更新代码: 只是为了测试,我现在使用atomicSublist 来检查。

【问题讨论】:

    标签: scheme racket atomic sublist


    【解决方案1】:

    从一个更简单的问题开始,然后进行概括。

    您将如何编写一个函数来检查符号 'a 是否是一个列表?

    【讨论】:

    • 嗯,我最初的想法更像是: (define (atomicSublist S L) (cond ((null? L) #f) ((equal? (car L) S) #t) ( else(atomicSublist S (cdr L))) ))
    • 这是一个好的开始。在 else 部分,您需要使用 (或 ...) 等。以下是一些可能性:1) L 等于 S。2) S 在 (cdr L) 中。跨度>
    • 试过了,好像没用。一些测试用例有效,但目前并非全部有效。
    • 用您现在拥有的代码更新您的问题,并说明哪些测试用例有效,哪些无效。
    • 我也无法将我的 extractLists 应用到我的嵌套子列表?也可以发挥作用。
    【解决方案2】:

    我不认为你想要这张支票((equal? S (car L) ) #t),因为 L 的车永远不会等于列表 S。

    这就是我想出的 atomicSublist 的内容。

        (define (atomicSublist S L)
            (cond
                [(null? S) #t]
                [(member? (car S) L) (atomicSublist (cdr s) L)]
                [else #f]))
    

    【讨论】:

    • 不要介意那部分现在可以工作,但是当我尝试使用子列表时仍然会抛出错误?而且有些结果还是不正确的。
    • 忘记了括号,抱歉,已修复。
    • 例如 (atomicSublist '(A A) '(A B C)) 应该是假的,不会弹出真。
    • 我想我需要一个嵌套循环来遍历第一个列表,然后是第二个。
    • extractLists 缺少括号,所以我修复了它。您将要编写一个函数来删除在 L 中找到的字母。
    【解决方案3】:

    这个问题有点模棱两可。这应该返回什么? (子列表?'(a (b)) '(a b c d e)) ??

    不管怎样,这是我写的:

    (define (sublist? s l)  
      (cond ((null? s) true)
            ((atom? (car s))
             (cond ((exists? (car s) l) (sublist? (cdr s) (remove-elm (car s) l)))
                   (else false)))
            (else 
             (cond ((sublist? (car s) l) (sublist? (cdr s) (remove-elm (car s) l)))
                   (else false)))))
    
    
    (define (exists? elm l)
      (cond ((null? l) false) 
            ((atom? (car l))
             (cond ((symbol=? elm (car l)) true)
                   (else (exists? elm (cdr l)))))
            (else
             (cond ((exists? elm (car l)) true)
                   (else (exists? elm (cdr l)))))))
    
    
    
    (define (remove-elm elm l)
      (cond ((null? l) '())
            ((null? elm) l)
            ((atom? elm)
             (cond ((atom? (car l)) 
                    (cond ((symbol=? elm (car l)) (cdr l))
                          (else (cons (car l) (remove-elm elm (cdr l))))))
                   (else
                    (cons (remove-elm elm (car l)) (remove-elm elm (cdr l))))))
            (else
             (remove-elm (cdr elm) (remove-elm (car elm) l)))))
    
    
    (define (atom? elm)
      (and (not (null? elm)) (not (pair? elm))))
    

    (sublist? '(a a) ('a b c d e)) 返回#f。 (sublist? '(a b c) '(a d b e c f)) 返回 #t。 (sublist? '(a (b)) '(c ((a (b)) e f))) 返回#t。 (sublist? '(a (b) b) '(c ((a (b)) e f))) retrns #f。但是,(sublist? '(a (b)) '(a b c d)) 返回 #t。

    【讨论】:

    • 每次我看到这个我都惊讶于你们如何阅读这些东西;-)
    • 是的。也许通过练习,我将能够写得更清晰。你能提供一个更好看的代码吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-26
    • 2021-01-04
    • 1970-01-01
    • 1970-01-01
    • 2022-08-24
    • 2017-02-10
    • 2015-07-31
    相关资源
    最近更新 更多