[Haskell-cafe] Python vs Haskell in tying the knot

Robert Greayer robgreayer at gmail.com
Wed Jul 15 22:35:43 EDT 2009


On Wed, Jul 15, 2009 at 2:18 PM, Max Rabkin<max.rabkin at gmail.com> wrote:
> On Wed, Jul 15, 2009 at 7:33 PM, Cristiano
> Paris<cristiano.paris at gmail.com> wrote:
>> fib = 1:1:fib `plus` (tail fib) where plus = zipWith (+)
> ...
> ...
> This indicates that you think tying the knot should be impossible in
> Python. In my opinion this is not the case. By my definition of tying
> the knot, one needs *either* mutable variables or laziness (at least
> in simple cases). Since Python has the former, it is possible to tie
> the knot in Python.

Isn't tying the knot (in the way 'fib' does) straightforward with closures
a la Python/Ruby/Smalltalk (without mutation)?
Even in a syntactically clumsy language like Java, a
tying-the-knot implementation equivalent to the canonical Haskell one is
not difficult, e.g.

static L fibs = new L() {
    public int head() { return 1; }
    public L tail() {
        return  new L() {
            public int head() { return 1; }
            public L tail() {
                return new L() {
                    public int head() { return fibs.head() +
fibs.tail().head(); }
                    public L tail() { return zip(fibs.tail(),
fibs.tail().tail()); }
                };
            }
        };
    }
};

Given a definition of list L and zip...

interface L { int head(); L tail(); }
static L zip(final L l0, final L l1) {
    return new L() {
        public int head() { return l0.head() + l1.head(); }
        public L tail() { return zip(l0.tail(), l1.tail()); }
    };
}


More information about the Haskell-Cafe mailing list