scala - How to implement `append[A](x: List[A], y: List[A]): List[A]` in tail-recursive version? -


here recursive version of append 2 lists:

def append[a](x: list[a], y: list[a]): list[a] = x match {   case nil => y   case h :: t => h :: append(t, y) } 

how convert tail-recursive version?

try this, though x prepended in reverse order. intended?

import scala.annotation.tailrec  @tailrec def append[a](x: list[a], y: list[a]): list[a] = x match {   case nil => y   case h :: t => append(t, h :: y) } 

if want prepend x in order comes with, you'ld have this:

import scala.annotation.tailrec  def append[a](x: list[a], y: list[a]): list[a] = {   @tailrec   def innerappend[a](x: list[a], y: list[a]): list[a] = x match {     case nil => y     case h :: t => innerappend(t, h :: y)   }    innerappend(x.reverse, y) } 

Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

Nuget pack csproj using nuspec -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -