删除倒数第二个ATOM [英] Deleting Second last ATOM

查看:134
本文介绍了删除倒数第二个ATOM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 (define(butSecondLastAtom lst1)
(cond
((null?lst1)'())
((null?(cdr lst1))lst1)
((null?(cddr lst1))
(cond ((not(pair?(car lst1)))(cdr lst1))
(else(cons(butLastAtom(car lst1))(cdr lst1)))))
(butLastAtom x)
(cond((null?(cdr x))
(cond((())(butSecondLastAtom(cdr lst1))))))

not(pair?(car x)))'())
(else(cons(butLastAtom(car x))'())))
(else(cons(car x)(butLastAtom (cdr x))))))

这段代码会删除第二个最后一个原子,条件 -
如果输入类似于(a(b(c(d)))),则输出应该为(a(b((d))))。
请更新我在哪里出错或有解决方案。

解决方案

这是一个基本上复制。从右侧移除给定位置的元素的技巧是每次处理叶时减少 n 。我们想要移除的元素必须是某对的车,所以重建对的那部分复制例程只需要能够监视何时发生。我们可以通过返回一些特殊值而不是 n th 项来发信号。我们可以使用什么特殊的价值?

 (define(rem) (树树)
;;返回一个类似于输入的新树,
;;但是没有右边的第n个树。 (cond
;;通过返回空树来复制空树
((null?tree)
'())
;;通过拷贝右边和然后将它们放回到一起;例外
;;是汽车是第n个元素(以及
的副本;;它是特殊值);在这种情况下,我们只需
;;返回右子树的副本。
((pair?tree)
(let((r(rem(cdr tree)));复制右子树
(l(rem(car tree))));复制左子树
(if(eq?l rem)
r
(cons lr))) )
;;当我们遇到一片树叶时,d增加柜台。
;;如果它为零(这意味着我们想放弃这个叶子),
;;然后返回特殊值。否则,返回
;;叶子。
(else
(set!n( - n 1))
(if(= n 0)rem tree)))))



 > (a(b(d)))

之后,可以很容易地定义更具体的版本:

 (define butSecondLastAtom lst1)
(rem 2 lst1))



 > (butSecondLastAtom'(ab(cd)((ef)(g))))
(ab(cd)((e)(g)))


I am trying to delete second last ATOM from the given list -

(define (butSecondLastAtom lst1)
 (cond 
   ((null? lst1) '())
   ((null? (cdr lst1)) lst1)
   ((null? (cddr lst1))
   (cond((not(pair? (car lst1))) (cdr lst1))
     (else (cons(butLastAtom (car lst1)) (cdr lst1)))))
(else (cons (car lst1) (butSecondLastAtom(cdr lst1))))))

(define (butLastAtom x)
    (cond ((null? (cdr x))
      (cond ((not(pair? (car x))) '())
           (else (cons (butLastAtom(car x)) '()))))
    (else (cons (car x) (butLastAtom(cdr x))))))

This code do delete the second last atom but fails for following condition - if input is like (a (b (c (d)))) then output should result in (a (b ((d)))). Please update where i am being wrong or with a solution.

解决方案

Here's a solution that's basically copying a tree. The trick to removing an element at a given position from the right is to decrement n each time we process a leaf. The element that we want to remove must be the car of some pair, so the part of the copying routine that rebuilds a pair just needs to be able to watch for when that happens. We can "signal" it by returning some special value instead of the nth item. What special value can we use? We've already defined an internal function that nothing else will have access to, so we can use it.

(define (rem n tree)
  ;; Returns a new tree similar to the input, 
  ;; but without the nth leaf from the right.
  (let rem ((tree tree))
    (cond
      ;; Copy the empty tree by returning the empty tree.
      ((null? tree)
       '())
      ;; Copy a pair by copying the right and left subtrees, 
      ;; and then putting them back together.  The exception 
      ;; is when the car is the nth element (and the "copy" of 
      ;; it is the special value).  In that case, we just
      ;; return the copy of the right subtree.
      ((pair? tree)
       (let ((r (rem (cdr tree)))  ; copy the right subtree
             (l (rem (car tree)))) ; copy the left subtree
         (if (eq? l rem)
             r
             (cons l r))))
      ;; When we encounter a leaf, decrement the counter.  
      ;; If it's zero (which means we want to discard this leaf),
      ;; then return the special value.  Otherwise, return
      ;; the leaf.
      (else
       (set! n (- n 1))
       (if (= n 0) rem tree)))))

> (rem 2 '(a (b (c (d)))))
(a (b ((d))))

After that, it's easy to define your more specific version:

(define (butSecondLastAtom lst1)
  (rem 2 lst1))

> (butSecondLastAtom '(a b (c d) ((e f) (g))))
(a b (c d) ((e) (g)))

这篇关于删除倒数第二个ATOM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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