使用CPS删除Scheme中的子序列函数(深度递归) [英] Remove subsequence function (deep recursion) in Scheme using CPS
问题描述
removesub*
包含一个原子列表和一个常规列表.第一个列表是第二个列表的子序列.该方法应返回第二个列表,并删除第一次出现的子序列.因此,如果第一个列表为'(abc),则删除第二个列表的第一个a,删除在删除的a之后出现的第一个b,以及删除的b之后出现的第一个c-无论如何原子嵌套的深度.
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))
(removesub* '(a b) '(w (x b) ((a) ((y z))) b))
预期输出: (w(x b)(()((y z))))
Expected Output: (w (x b) (() ((y z))))
我正在尝试使用连续传递样式(CPS)来完成此功能.对于这种复杂的功能,我真的很难掌握. 借助先前的stackoverflow问题,我得以尝试问题,但我的尝试返回了一个空列表.
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.
我在做什么错了?
(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))))))))
推荐答案
对您的代码进行了两处小的更改,我就能完成一些工作:
With two small changes to your code, I got something to work:
- 我在第一个
cond
分支中将return l1 l2
更改为(return l1 l2)
. - 我在底行将
con
更改为cons
.
- I changed
return l1 l2
to(return l1 l2)
in the firstcond
branch. - I changed
con
tocons
in the bottom line.
祝你好运!
这篇关于使用CPS删除Scheme中的子序列函数(深度递归)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!