|
|
|
|
|
by pufuwozu
4981 days ago
|
|
Callbacks are different than gotos in that they are aren't even remotely close to gotos. The analogy is that callbacks create non-linear control flow. Using a monadic syntax like in Roy, we can easily have callbacks without having them look non-linear: let deferred = {
return: \x ->
let d = $.Deferred ()
d.resolve x
d.promise ()
bind: \x f -> x.pipe f
}
let v = do deferred
hello <- $.ajax 'examples/helloworld.roy'
alias <- $.ajax 'examples/alias.roy'
return (hello ++ alias)
v.done console.log
Which compiles into continuation passing: var deferred = {
"return": function(x) {
var d = $.Deferred();
d.resolve(x);
return d.promise();
},
"bind": function(x, f) {
return x.pipe(f);
}
};
var v = (function(){
var __monad__ = deferred;
return __monad__.bind($.ajax('examples/helloworld.roy'), function(hello) {
return __monad__.bind($.ajax('examples/alias.roy'), function(alias) {
return __monad__.return((hello + alias));
});
});
})();
v.done(console.log);
Anyway, continuations (with call/cc) are definitely a controlled form of goto. Take a look at an example from Paul Graham:http://lib.store.yahoo.net/lib/paulgraham/cint.lisp ((call/cc
(lambda (goto)
(letrec ((start
(lambda ()
(print "start")
(goto next)))
(froz
(lambda ()
(print "froz")
(goto last)))
(next
(lambda ()
(print "next")
(goto froz)))
(last
(lambda ()
(print "last")
(+ 3 4))))
start))))
|
|