2013-06-12 86 views
0

我有以下代码:斯卡拉置换列表(recusive类型不匹配)

 type foo= List[bar] 
     def emtfoo= List[bar]() 

    def permute(s1:Set[foo],s2:Set[foo]):Set[foo]={ 
    for{ 
     x<-s1 
     y<-s2 
    }yield permutatefoo(x,y,e,emtfoo) 



def permutatefoo(l1:foo,l2:foo,sofar:foo):Set[foo]={ 
    if (l1.equals (emtfoo)) { 
     Set{sofar:+l2} 
     } 
    else 
    { 
    combine(permutatefoo(l1.drop(1),l2,l1.take(1):::sofar), 
     permutatefoo(l1,l2.drop(1),l2.take(1):::sofar)) 
     } 
    } 
def combine(s1:Set[foo],s2:Set[foo]):Set[foo] = 
    for{ 
     x <- s1 
     y<- s2 
    } yield x ::: y 

这应该是相当简单的代码到重排列2套列表为一组同时具有列出的所有可能的permamutations在没有元素出现在元素前面的顺序不在列表本身的前面(所以如果我们有列表a = 1,2,3和列表b = a,b,c,那么它应该返回Set { 1,a,2,b,3,c-1,2,a,3,b,ca,1,2,3,b,c ext。})。 但是我的代码生成周围行了一些错误类型mistmaches

{Set{sofar:+l2}} 

x<-s1 

没有任何人知道如何解决这一问题?

+1

为了记录这个词是“置换”,而不是“置换”。 –

回答

1

我不知道我神交所有的代码,也有一些是我看到的是:

1:

{Set{sofar:+l2}} // should probably be Set(sofar ++ l2) 
       // as it seems you just want to concatenate sofar and l2 
       // which are both List[bar] (so your return value is a Set[foo] 
       // i.e. a Set[List[bar]] as the signature for 
       // permutatefoo requests 

:+是提取器(see the doc)和它的不应该

:以这种方式

2中可使用

3:

的返回类型permute是错误的。您定义它的方式将返回Set[Set[foo]]而不是Set[foo]。我不确定我是否理解你想要它做什么,但是如果你修复了返回类型,至少应该进行类型检查。


这是您编译的代码的一个版本。我不是说工程(正如我所说的,我不确定我是否理解你的程序的所有预期输入和输出应该是什么),但它编译。

type bar=Int 

type foo= List[bar] 
def emtfoo= List[bar]() 

def combine(s1:Set[foo],s2:Set[foo]) = 
    for{ 
     x <- s1 
     y <- s2 
    } yield x ++ y 

def permutatefoo(l1:foo, l2:foo, sofar:foo): Set[foo]={ 
    if (l1.equals (emtfoo) || l2.equals (emtfoo)) { 
     Set(sofar ++ l2) 
     } 
    else 
    { 
    combine(permutatefoo(l1.drop(1),l2,l1.take(1) ++ sofar), 
     permutatefoo(l1,l2.drop(1),l2.take(1) ++ sofar)) 
    } 
} 

def permute(s1:Set[foo],s2:Set[foo]) : Set[Set[foo]] = { 
    for { 
     x <- s1 
     y <- s2 
    } yield permutatefoo(x,y,emtfoo) 
}