简体   繁体   English

Scala 置换列表(递归类型不匹配)

[英]Scala permute list (recursive type mismatch)

I have the following code:我有以下代码:

      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

Which should be fairly straightforward code to permutate 2 sets of lists into a single set which has all possible permutations of both lists in order with no element appearing in front of an element in wasn't in front of in the list itself (so if we have list a = 1,2,3 and list b =a,b,c then it should return the Set{1,a,2,b,3,c-1,2,a,3,b,ca,1,2,3,b,c ext.}).这应该是相当简单的代码,可以将 2 组列表排列成一个集合,该集合具有两个列表的所有可能排列顺序,没有元素出现在元素前面,而不是在列表本身的前面(所以如果我们有列表 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 分机})。 However my code generates a few errors type mistmaches around the line.但是,我的代码在该行周围生成了一些错误类型的错误。

{Set{sofar:+l2}}

and

   x<-s1

Does anybody know how to fix this?有谁知道如何解决这个问题?

I'm not sure I grok all your code, but a few things I see are:我不确定我是否理解了您的所有代码,但我看到的一些内容是:

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

:+ is an extractor ( see the doc ) and it's not supposed to be used in that way :+是一个提取器( 参见文档),它不应该以这种方式使用

2: 2:

if (l1.equals (emtfoo)) // you should also check against ls.equals(emtfoo)
                        // as you're also dropping elements from l2
                        // in permutatefoo(l1,l2.drop(1),l2.take(1):::sofar))

3: 3:

the return type for permute is wrong. permute的返回类型是错误的。 The way you defined it, it returns a Set[Set[foo]] instead of a Set[foo] .你定义它的方式,它返回一个Set[Set[foo]]而不是Set[foo] I'm not sure I understand what you want it to do, but if you fix the return type it should at least type-check.我不确定我是否理解您想要它做什么,但是如果您修复返回类型,它至少应该进行类型检查。


This is a version of your code that compiles.这是编译的代码版本。 I'm not saying it works (as I said, I'm not sure I understand what all the expected inputs and outputs for your program should be), but it compiles.我并不是说它有效(正如我所说,我不确定我是否理解程序的所有预期输入和输出应该是什么),但它可以编译。

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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM