Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scheme 如何在方案中列出给定原子的所有实例_Scheme - Fatal编程技术网

Scheme 如何在方案中列出给定原子的所有实例

Scheme 如何在方案中列出给定原子的所有实例,scheme,Scheme,下面是一个关于我的问题的例子 Example: atom = a list2 = (a (b c a (a d))) output = (a a (b c a a (a a d))) 在方案中我该怎么做,谢谢我建议以下配方: 在处理没有子列表的平面列表时,如何解决此问题? 现在,使用相同的方法处理子列表。最初,您可能希望假定子列表是平面的,但最终您将需要处理子列表的子列表。 您需要考虑如何分别解决每个步骤。一旦您对用于解决该问题的一般结构有了清晰的认识,就不难编写该问题的解决方案。因为这看起

下面是一个关于我的问题的例子

Example:
atom = a
list2 = (a (b c a (a d)))

output = (a a (b c a a (a a d)))

在方案中我该怎么做,谢谢

我建议以下配方:

在处理没有子列表的平面列表时,如何解决此问题? 现在,使用相同的方法处理子列表。最初,您可能希望假定子列表是平面的,但最终您将需要处理子列表的子列表。
您需要考虑如何分别解决每个步骤。

一旦您对用于解决该问题的一般结构有了清晰的认识,就不难编写该问题的解决方案。因为这看起来像是一个家庭作业,所以我让你自己制定解决方案,只需填写以下空白:

(define (double-atoms lst atm)
  (cond ((null? lst)                            ; if the list is empty
         <???>)                                 ; then return the empty list
        ((not (pair? (car lst)))                ; else if the current element is an atom
         (if (equal? (car lst) atm)             ; if the current element == atm
             (<???> (double-atoms <???> atm))   ; then cons two copies of the current element
             (<???> (double-atoms <???> atm)))) ; else cons one copy of the current element
        (else                                   ; else the current element is a list
         (cons (double-atoms <???> atm)         ; then advance the recursion over the car
               (double-atoms <???> atm)))))     ; also advance the recursion over the cdr

很简单,两份:cons car lst cons car lst。一份副本:cons car lst。。。这是代码,我仍然可以让它工作…请你告诉我我犯了什么错误:定义双原子lst atm cond null?不是成对的吗?车辆lst是否相等?你忘了把cdr和一辆车加到双原子的lst atm上。整个代码是:定义双原子lst atm cond null?不是成对的吗?车辆lst是否相等?car lst atm cons car lst cons car lst双原子cdr lst atm cons car lst双原子cdr lst atm else cons双原子car lst atm双原子cdr lst atm@林君翰 Ats修复了代码中的错误,他的解决方案是正确的。我希望你自己先找到解决办法;请不要忘记通过单击左侧的复选标记接受对您最有帮助的答案。此答案不正确,并非所有原子都会加倍,只有作为参数接收的原子才会加倍。我希望林君翰 可以将几乎解决方案修改为完全解决方案。嗯,好吧,也许你应该在回答中说明,否则人们会认为你误解了这个问题;
(double-atoms '(a (b c a (a d))) 'a)
=> '(a a (b c a a (a a d)))
(define (double-atoms l)
  (cond
    ; nothing to double in an empty list
    [(empty? l) '()]
    ; the first element is a list 
    ; => double atoms in it 
    [(pair? (first l))
     (cons (double-atoms (first l))
           (double-atoms (rest l)))]
    ; the first element is an atom, double it
    [else (cons (first l)
                (cons (first l)
                      (double-atoms (rest l))))]))