问题
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:
- I changed
return l1 l2
to(return l1 l2)
in the firstcond
branch. - I changed
con
tocons
in the bottom line.
Good luck!
来源:https://stackoverflow.com/questions/35544606/remove-subsequence-function-deep-recursion-in-scheme-using-cps