Why is this not tail recursive?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Why is this not tail recursive?

Peter Wolf
Hi, why does @tailrec complain here?  It says getFoo(page+1) is not in tail recursive position. 

class Foo
def getFoo(page: Int, pageSize: Int): Seq[Foo] = ???

@tailrec
def getFoo(page: Int): Seq[Foo] = {
val foos = getFoo(page,pageSize)
if(foos.size >= pageSize) foos else foos ++ getFoo(page+1)
}

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Aw: [scala-user] Why is this not tail recursive?

Dennis Haupt-2
in "foos ++ getFoo(page+1)"
the last function to be called is "++", not "getFoo"
 
 
 
Gesendet: Donnerstag, 05. Januar 2017 um 22:51 Uhr
Von: "Peter Wolf" <[hidden email]>
An: scala-user <[hidden email]>
Betreff: [scala-user] Why is this not tail recursive?
Hi, why does @tailrec complain here?  It says getFoo(page+1) is not in tail recursive position. 
 
class Foo
def getFoo(page: Int, pageSize: Int): Seq[Foo] = ???

@tailrec
def getFoo(page: Int): Seq[Foo] = {
  val foos = getFoo(page,pageSize)
  if(foos.size >= pageSize) foos else foos ++ getFoo(page+1)
}

 

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Why is this not tail recursive?

Peter Wolf
Right!  Duh!  

Thanks
P

On Thu, Jan 5, 2017 at 4:53 PM, Dennis Haupt <[hidden email]> wrote:
in "foos ++ getFoo(page+1)"
the last function to be called is "++", not "getFoo"
 
 
 
Gesendet: Donnerstag, 05. Januar 2017 um 22:51 Uhr
Von: "Peter Wolf" <[hidden email]>
An: scala-user <[hidden email]>
Betreff: [scala-user] Why is this not tail recursive?
Hi, why does @tailrec complain here?  It says getFoo(page+1) is not in tail recursive position. 
 
class Foo
def getFoo(page: Int, pageSize: Int): Seq[Foo] = ???

@tailrec
def getFoo(page: Int): Seq[Foo] = {
  val foos = getFoo(page,pageSize)
  if(foos.size >= pageSize) foos else foos ++ getFoo(page+1)
}

 

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Loading...