News for hackers. Stuff that matters.
Re:Can a simple algebra test predict programming aptitude? by Retric on Saturday December 13, 2014 @02:17PM (#8746027)
It's trivial to blow the stack even on languages with tail call optimization.
Re:Can a simple algebra test predict programming aptitude? by sklogic on Sunday December 14, 2014 @09:22AM (#8748333)
Ok, try to blow the stack in a stackless language implementation.
Re:Can a simple algebra test predict programming aptitude? by Retric on Monday December 15, 2014 @03:25PM (#8754211)
F(x) = F(x) + F(x)

Eventually you hit OOM or some death limit even on a stackless language.

Re:Can a simple algebra test predict programming aptitude? by sklogic on Monday December 15, 2014 @04:03PM (#8754448)
This is a memory leak now, not a stack overflow.
Re:Can a simple algebra test predict programming aptitude? by Retric on Tuesday December 16, 2014 @09:27AM (#8757232)
Thus demonstrating a finite stack. "Stackless" just a more efficent way to use memory. You could get the same things from dynamicly allocating and dealocating stack space as needed.

Put another way, allocating 1GB of stack space on a machine with 2GB of RAM is at worst the equivelent of using a stackless lanugage on a machine limited 1 GB of RAM.

PS: It's actually worse as stackless languages are stricly slower than actually having the equivelent stack space pre alocated and you need to put an pointer for each stack call pluss overhead for alocating memory.

Re:Can a simple algebra test predict programming aptitude? by mytochar on Saturday December 13, 2014 @04:32PM (#8746518)
By writing incorrect recursion? Or in some other way?
Re:Can a simple algebra test predict programming aptitude? by Retric on Saturday December 13, 2014 @07:30PM (#8747035)
Both, the obvious example is the fibinatchi sequence f(x) = f(x-1) + f(x-2). There are lots of efficient ways of coding it but the most obvious O(n^2) approch is n depth and it can't be tail-call optimized.

Granted, you can write much more efficient code that can be tail call optimized, but there complex and less obvious. There are also languages which cache previous results so the native approch becomes O(n) with fairly elegant code that's still depth n.

PS: Anyway, tail call optimization only works when you can rewrite the code as a loop for more complex structures it's less useful.

Re:Can a simple algebra test predict programming aptitude? by sklogic on Sunday December 14, 2014 @09:25AM (#8748339)
> Anyway, tail call optimization only works when you can rewrite the code as a loop

You cannot turn a virtual tail call into a static jump.

Re:Can a simple algebra test predict programming aptitude? by Retric on Monday December 15, 2014 @03:27PM (#8754221)
Sure, it needs bookkeeping but it's still trivial to create a loop equivalent of any tail call optimizable code that your compiler recognizes.

PS: Feel free to look for any counter example.

Re:Can a simple algebra test predict programming aptitude? by sklogic on Monday December 15, 2014 @04:02PM (#8754442)
> PS: Feel free to look for any counter example.

Ok, create a loop equivalent for `(define (f g x) (g x))`.

Re:Can a simple algebra test predict programming aptitude? by Retric on Tuesday December 16, 2014 @09:50AM (#8757342)
x

Though the mechanical equivalent would be something like:

  huge(input)
  {
  output, continue, current_function = f;
  do{
  if (current_function == f)
  {input = x ; continue = true; current_function=g}
  if (current_function == g)
  { output = input; continue = false;}
  }while(continue);
  return output;
  }
You would then add any function you would tail call optimize into that function. Granted, with the right structure (inside > outside >... > inside) it can show up more than once on the stack but the same thing can happen despite tail call optimization.
Re:Can a simple algebra test predict programming aptitude? by sklogic on Tuesday December 16, 2014 @11:55AM (#8757926)
Mini-interpreter implementations do not count, for performance reasons. You could have called a VM bytecode interpreter loop a "loop alternative to tail call" here.
Re:Can a simple algebra test predict programming aptitude? by Retric on Tuesday December 16, 2014 @01:19PM (#8758426)
My first example of (f x) is about as fast as you can get.

The longer example is not great the point is it's a very language agnostic tradeoff of speed vs memory. Replace the ‘if’ conditionals with a case statement and its much faster you can speed it up further by using continue.

You can speed it up even more by having a jump at the end of each statement, but that stops looking like a loop and is basically just the tail call optimization.

Anyway, the important part is its low memory utilization and compiler independent.

PS: I in no way suggest tail call optimization was not useful; just you can mechanically simulate it when not available. You can usually beat that mechanical approach if you need to speed things up further.

Re:Can a simple algebra test predict programming aptitude? by sklogic on Wednesday December 17, 2014 @06:17AM (#8761894)
> as fast as you can get.

But yet 10x slower than a single indirect jump.

> just you can mechanically simulate it when not available.

It's too slow to ever be anywhere near practical - for this reason, almost no JVM language implementation really does anything like this, besides kawa, bigloo and alike, which are slower than some of the dumb interpreters like SISC.

Re:Can a simple algebra test predict programming aptitude? by Retric on Wednesday December 17, 2014 @09:50AM (#8762422)
I don't think I was clear enough, it simplifies to zero code (not even a jump) just the original input. (I thought about saying noop but even that's wasteful.)

>"it's slow"

Again, it's just a technique; think writing embedded code with a really dumb compiler. Anyway, saying its slow is not really a counter argument if your limited to say 2,000 bytes of ram you will make lot's of tradeoffs between efficiency and speed. Perhaps you have a select statement perhaps you don't, but starting off with pure ASM is often a pain.

EDIT: This is all from the perspective of dealing with tools that don't do tail call optimization, not writing a compiler / VM etc.