方案Rswap功能

用户名

有人请帮我这个功能吗?

使用方案功能,其行为类似于交换的递归版本。

(reswap '((h i)(j k) l (m n o)))

应该回来

((k j) (i h) (n m o) l) ;

(reswap '((a b) c (d (e f)) g (h i)))

应该回来

(c (b a) g ((f e) d) (i h))) 
萨胡

试试这个:

(define (rswap lst)

  ;; Create a helper function to do the recursive work.
  (define (helper in out)

    ;; If the input is not a list, simply return it.
    ;; There is nothing to be done to rswap it.
    (if (not (list? in))
      in

      ;; If in is an empty list, simply return the out.
      (if (null? in)
        out

        ;; If in is a list with only one item, append
        ;; the result of calling rswap on the item to 
        ;; out and return it.
        (if (null? (cdr in))
          (append out (list (rswap (car in))))

          ;; This is where the recursion continues.
          ;; Take two items off in before the next call.
          ;; rswap the two items and add them to out.
          (helper
            (cddr in)
            (append out (list (rswap (cadr in)) (rswap (car in)))))))))

  (helper lst '()))

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章