English 中文(简体)
配对
原标题:scala pattern matching
  • 时间:2011-08-07 20:18:40
  •  标签:
  • scala

看来Nil的类型并不多变。 我如何纠正这一职能:

scala> def last[A](a:List[A]) : A =   
     | a match {                      
     |   case _ :: tail  => last(tail)
     |   case Nil => Nil[A]           
     | }                              
<console>:8: error: object Nil does not take type parameters.
     case Nil => Nil[A]

最新资料:

scala> def last[A](a : List[A] ) : Option[A] =
     | a match {                              
     |   case head :: Nil => Some(head)       
     |   case _ :: tail => last(tail)  
     |   case Nil => None                     
     | }                                      
最佳回答

<代码>Nil是一个物体,不是一种类型。 http://code>Nil[A].n t值不变。

空洞清单没有最后的内容。 因此,在<代码>Nil上援引<>last的,应当留下错误。

def last[A](l: List[A]): A = l match {                      
  case x :: Nil => x
  case _ :: tail => last(tail)
  case Nil => sys.error("An empty list")
} 

Alternatively you could have last return an Option[A] as shown below:

def last[A](l: List[A]): Option[A] = l match {                      
  case x :: Nil => Some(x)
  case _ :: tail => last(tail)
  case Nil => None
} 
问题回答

错误是,你先宣布返回<代码>的方法。 A:

def last[A](a:List[A]) : A
                        ^^^

www.un.org/Depts/DGACM/index_french.htm A。

(And(作为副注),正如错误信息所言,Nil不采用类型参数。)


<代码>终端功能较好的回归类型很可能是<代码>[A]。 您的方法定义,但Option[A]将考虑:

scala> def last[A](a: List[A]): Option[A] =
     | a match {                           
     |     case x :: Nil => Some(x)        
     |     case _ :: tail => last(tail)    
     |     case Nil => None                
     | }                                   
last: [A](a: List[A])Option[A]

您需要告诉汇编员什么类型的<代码> 注

Nil: List[A]

但是,正如奥贝比指出的,这不解决你的问题。 <代码>Nil是一个空表,而不是名单的最后部分。

标的<代码>Nil 延伸List[Nothing]Nothing是所有事项的一种子类(同为)。 任何均为一切的超级类型,List为共同使用(编号为List[+A]),这意味着 Nil将作为每个清单的终点。

然而,由于这一建筑,有些情况是,汇编者可以推断出准确的空缺名单类型,而且你需要打手(如本·詹姆斯所示)。 BTW,同样适用于作为<编码>的子级的物体代码





相关问题
How to flatten a List of different types in Scala?

I have 4 elements:List[List[Object]] (Objects are different in each element) that I want to zip so that I can have a List[List[obj1],List[obj2],List[obj3],List[obj4]] I tried to zip them and I ...

To use or not to use Scala for new Java projects? [closed]

I m impressed with Twitter and investigating to use Scala for a new large scale web project with Hibernate and Wicket. What do you think about Scala, and should I use it instead of Java? EDIT: And, ...

Why does Scala create a ~/tmp directory when I run a script?

When I execute a Scala script from the command line, a directory named "tmp" is created in my home directory. It is always empty, so I simply deleted it without any apparent problem. Of course, when I ...

Include jar file in Scala interpreter

Is it possible to include a jar file run running the Scala interpreter? My code is working when I compile from scalac: scalac script.scala -classpath *.jar But I would like to be able to include a ...

Scala and tail recursion

There are various answers on Stack Overflow which explain the conditions under which tail recursion is possible in Scala. I understand the limitations and how and where I can take advantage of tail ...