News for hackers. Stuff that matters.
Re:Corecursion by gamegoblin on Thursday May 08, 2014 @07:52PM (#7718468)
Here is the call stack for 1, 3, and 5

    fib 1
      1

    fib 3
      fib 2
        fib 1
          1
        fib 0
          1
      2
      fib 1
        1
      3

    fib 5
      fib 4
        fib 3
          fib 2
            fib 1
              1
            fib 0
              1
            2
          fib 1
            1
          3
        fib 2
          fib 1
            1
          fib 0
            1
          2
        5
      fib 3
        fib 2
          fib 1
            1
          fib 0
            1
          2
        fib 1
          1
        3
      8
The number of calls grows exponentially. Roughly 1.618^n, to be specific.
Re:Corecursion by dustingetz on Friday May 09, 2014 @12:38AM (#7719414)
Haskell is lazy and pure, why can't it auto memoize for us so we can code it like we would in math?
Re:Corecursion by albertzeyer on Friday May 09, 2014 @03:35AM (#7719795)
A while ago, I asked exactly that question:

http://stackoverflow.com/questions/5749039/automatic-memoizi...

I think the answer is basically that there is no good heuristic about what to store, how much and how long.

Re:Corecursion by cabalamat on Friday May 09, 2014 @09:01AM (#7720575)
> I think the answer is basically that there is no good heuristic about what to store, how much and how long.

In Python you can just put a @memoize decorator before a function you want memoized; would it be possible to have something similar in Haskell?

Re:Corecursion by albertzeyer on Friday May 09, 2014 @09:08AM (#7720605)
Yes, see here: http://www.haskell.org/haskellwiki/Memoization

Note that in Python, you also have the issue that you still need to specify some cleverness about how long something is stored and what is stored. Many example memoize implementations just store everything (e.g.: https://wiki.python.org/moin/PythonDecoratorLibrary#Memoize) but of course that is not a solution in practice.

Re:Corecursion by gizmo686 on Friday May 09, 2014 @11:22AM (#7721337)
I don't think the methods mentioned in the haskell wiki are quite equivelent to the python decorator, as they require you to modify the data structure itself to support memoization.

It should be possible to use UnsafePerformIO to create a generic function memoize function (at least for inputs that implelemt Eq or Ord), but that is almost definantly overkill and asking for space leaks.

Re:Corecursion by nodejsguy on Thursday May 08, 2014 @11:32PM (#7719177)
If this was reddit, you would get gold for that reply
Re:Corecursion by gamegoblin on Friday May 09, 2014 @12:11AM (#7719311)
In the time it took me to do it by hand I am 100% positive I could have written a Python program to do it probably 3 times.

Yyyyep just tested it:

    def fibs(n, tabs=0):
        if n < 2:
            print "\t"*tabs + "1"
            return 1
        print "\t"*tabs+"fibs %d"%n
        a = fibs(n-1, tabs+1)
        b = fibs(n-2, tabs+1)
        print "\t"*tabs+str(a+b)
        return a+b
Took exactly 71 seconds to write. I probably spent 3 minutes on it by hand getting all the spaces right.