Re: [racket-users] What is the Racket equivalent to 'return' for early exit?
On Nov 21, 2016, at 9:37 AM, David Storrswrote: > In Perl I would often write: > > sub do_something { > return unless ( some necessary condition is met ); > ... do the thing ... > } On Nov 21, 2016, at 10:07 AM, Leif Andersen wrote: > Although honestly, with this pattern, I find that errors work better here, as > they return early, and you can decide how they get handled with a > with-handlers. > > (define (do-something) > (unless (some-condition) > (error "NO")) > (do-the-thing)) > > (with-handlers ([exn:fail? (lambda (e) (displayln "I returned early"))]) > (do-something)) I agree with Leif. I've learned this pattern from studying well-written Racket code. As I understand it, the policy idea is that when a called function doesn't do what it was asked to do, then the caller of the function — not the called function — should get to decide how serious a problem it is. So `error` / `with-handlers` is the best idiom. I think of `return` as a way for a function to deliver its *expected result*, in the special circumstance where it has to bust out of a deeply nested block. IOW, `return` is not for error conditions. -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [racket-users] What is the Racket equivalent to 'return' for early exit?
Greetings. On 21 Nov 2016, at 18:07, Leif Andersen wrote: (define (do-something) (unless (some-condition) (error "NO")) (do-the-thing)) (with-handlers ([exn:fail? (lambda (e) (displayln "I returned early"))]) (do-something)) But that is specifically because I prefer the workflow of catching errors, rather than always having to manually check the output of the function (I'm looking at you C...) On reflection, I think this is a better answer than mine (and is probably what I'd do in fact). Apart from anything else, it means that (do-something) will only return the type of thing it's supposed to return, and not anything that needs checked. That would be still more important if you were using Typed Racket. All the best, Norman -- Norman Gray : https://nxg.me.uk SUPA School of Physics and Astronomy, University of Glasgow, UK -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [racket-users] What is the Racket equivalent to 'return' for early exit?
On Mon, Nov 21, 2016 at 10:30 AM, 'John Clements' via Racket Users < racket-users@googlegroups.com> wrote: > > > On Nov 21, 2016, at 09:37, David Storrswrote: > > > > In Perl I would often write: > > > > sub do_something { > > return unless ( some necessary condition is met ); > > ... do the thing ... > > } > > > > In Racket I could wrap the rest of the procedure in an (if), but that > adds an unnecessary level of indentation and feels clunky. Is there a > clean solution? > > Playing the role of irritating ideologue, it’s … me! > > Yes, there’s let/ec, but my (limited) experience in industry suggests that > reading functions that use ‘return’ liberally can be massively more > difficult than one that uses ‘if’ or a related form. I mostly agree with you. I've found that functions should have at most two return points: one at the top and one at the bottom. The one at the top is just for checking preconditions before you start doing real work, the one at the bottom is the happy path result. > BTW: yes, my soapbox is huge. Huge! > I think you meant 'yuje'. Or perhaps 'bigly'? ;> > > John > > > > -- > You received this message because you are subscribed to the Google Groups > "Racket Users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to racket-users+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [racket-users] What is the Racket equivalent to 'return' for early exit?
> On Nov 21, 2016, at 09:37, David Storrswrote: > > In Perl I would often write: > > sub do_something { > return unless ( some necessary condition is met ); > ... do the thing ... > } > > In Racket I could wrap the rest of the procedure in an (if), but that adds an > unnecessary level of indentation and feels clunky. Is there a clean solution? Playing the role of irritating ideologue, it’s … me! Yes, there’s let/ec, but my (limited) experience in industry suggests that reading functions that use ‘return’ liberally can be massively more difficult than one that uses ‘if’ or a related form. I would argue that the call/ec doesn’t actually make the code less clunky, it just hides the clunkiness, and makes the code harder to read, to boot. In the absence of return, you can generally easily deduce when control flow reaches a particular point (“we only get here if x is > 0 and the string is empty”). In the presence of return, this becomes “we only get here if x is > 0 and the string is empty and one of these 19 incomprehensible clauses didn’t trigger a return.” I do think that call/ec makes sense in some circumstances; for instance, if you want to bail out of a deeply nested call, and the intervening calls are to functions that someone else wrote. But generally, I try to bite the bullet and use the if (or cond), on the theory that when I come to read my own code next year I’ll have some idea what it’s doing. BTW: yes, my soapbox is huge. Huge! John -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[racket-users] What is the Racket equivalent to 'return' for early exit?
In Perl I would often write: sub do_something { return unless ( some necessary condition is met ); ... do the thing ... } In Racket I could wrap the rest of the procedure in an (if), but that adds an unnecessary level of indentation and feels clunky. Is there a clean solution? -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.