Remove subsequence function (deep recursion) in Scheme using CPS

核能气质少年 提交于 2019-12-12 02:55:37

问题


removesub* takes a list of atoms and a general list. The first list is a subsequence of the second list. The method should return the second list with the first occurence of the subsequence removed. So, if the first list is '(a b c), the first a if the second list is removed, the first b that appears after the removed a is removed, and the first c that appears after the removed b is removed - no matter how deep the atoms are nested.

(removesub* '(a b) '(w (x b) ((a) ((y z))) b))

Expected Output: (w (x b) (() ((y z))))

I am trying to complete this function using continuous passing style (CPS). This is really difficult for me to grasp with a function of this complexity. With the help of a previous stackoverflow question, I was able to attempt the problem, but my attempt returns an empty list.

What am I doing wrong?

Attempt

(define removesub*
  (lambda (l1 l2 return)
    (cond
      ((or (not (pair? l1)) (not (pair? l2))) return l1 l2)
      ((pair? (car l2))
       (removesub* l1
                   (car l2)
                   (lambda (v1 v2car)
                     (removesub* v1
                                 (cdr l2)
                                 (lambda (v1 v2cdr)
                                   (return v1 (cons v2car v2cdr)))))))
      ((eq? (car l1) (car l2))
       (removesub* (cdr l1) (cdr l2) return))
      (else
       (removesub* l1
                   (cdr l2)
                   (lambda (v1 v2)
                     (return v1 (con (car l2) v2))))))))

回答1:


With two small changes to your code, I got something to work:

  1. I changed return l1 l2 to (return l1 l2) in the first cond branch.
  2. I changed con to cons in the bottom line.

Good luck!



来源:https://stackoverflow.com/questions/35544606/remove-subsequence-function-deep-recursion-in-scheme-using-cps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!