Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Reading that article inspired me to write a little generic recursion library. The idea is that you'd write a 'map' function for each recursive data structure and then generate recursive functions from non-recursive functions using 'induction' and 'coinduction'.

    // induction : ((t (Fix t), Fix t -> r) -> t r, t r -> r) -> Fix t -> r
    function induction(map, f) {
        // g : Fix t -> r
        function g(x) {
            return f(map(x.unfold(), g));
        };
        return g;
    };
    
    // coinduction : ((t s, s -> Fix t) -> t (Fix t), s -> t s) -> s -> Fix t
    function coinduction(map, f) {
        // g : s -> Fix t
        function g(s) {
            return {
    	        unfold: function() {
    	            return map(f(s), g);
                }
            };
        };
        return g;
    };

    // fold : t (Fix t) -> Fix t
    function fold(w) {
        return {
            unfold: function() { return w; }
        };
    };


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: