Hacker News new | ask | show | jobs
by happy_dino 4708 days ago
> I am still seeking the legendary JVM implementation with TCO.

  scala> :paste
  // Entering paste mode (ctrl-D to finish)

  object TCO extends App {
    def stackSize = Thread.currentThread.getStackTrace.length
    def printStackSizeMessageOutside() =
      println(s"$stackSize stackframes outside of the mutually recursive calls")
    def printStackSizeMessageInside() =
      println(s"$stackSize stackframes inside the mutually recursive calls")

    def mutuallyRecursiveAdd1(a: Long, b: Long): Long =
      if (b == 0) {
        printStackSizeMessageInside()
        a
      } else
        mutuallyRecursiveAdd2(a+1, b-1)
  
    def mutuallyRecursiveAdd2(a: Long, b: Long): Long =
      if (b == 0) {
        printStackSizeMessageInside()
        a
      } else
        mutuallyRecursiveAdd1(a+1, b-1)
  
    printStackSizeMessageOutside()
    mutuallyRecursiveAdd1(23, 10000000)
  }

  // Exiting paste mode, now interpreting.

  defined object TCO

  scala> TCO main null
  We have 29 stackframes outside of the mutually recursive calls
  We have 30 stackframes inside the mutually recursive calls
1 comments

Sweet, where do I download it/which obscure flag to I pass to enable it?

  27 stackframes outside of the mutually recursive calls
  java.lang.StackOverflowError
	at TCO$.mutuallyRecursiveAdd2(<console>:22)
Oddly, your Thread.currentThread.getStackTrace.length seems to return strings like "We have 30". Mine is returning an Int over here.
Ah right. I refactored the messages a bit and didn't completely update the post to mirror that.
I am still very interested in learning about the runtime you are using :)

After a significant amount of effort (googling for stuff about scala, JVMs, and TCO mostly gets hits with people complaining about their code not working) I discovered something called Avian, which I can try out soon. I still think it is a bit weird to spend time engaging with a stranger who wants to know something very simple, but to expect the stranger to dig around for it for a few hours instead of telling him.

As a general policy, if I spend time explaining things I require people to do some homework on their own.

This way people who are lazy, whose interest is just strong enough to make ridiculous claims and complaints but don't actually care about the topic at hand, don't get things for free.

I protect my investment and everyone wins.