4 ms·
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
by pufuwozu 14y 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 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))))
- tptacek 14y agoAll of structured programming consists of various controlled forms of goto.
- m0nastic 14y agoYesterday, I found an email from Jon Callas where he was giving advice on where SAML falls in the Chomsky hierarchy of languages: "If it has backwards gotos in any form, it's Turing-complete. Loops, recursion, etc. are backwards gotos. If-then-else is a forward goto." I hadn't ever thought of things that way, but I thought it was interesting.
- hvs 14y agoOnce you've worked in assembly language, you realize everything is just another form of jmp.
- awj 14y agoThat's the point of the article, though: callbacks don't offer enough structure to effectively reason about.