Em segunda-feira, 21 de novembro de 2016 16:08:40 UTC-2, Leif Andersen  
escreveu:
> Honestly, I personally like to use let/ec for this. I know it's still using 
> continuations, but it is much more lightweight, both syntactically and in 
> terms of run-time costs.
> 
> (define (do-something)
> 
>   (let/ec return
>     (unless (some-condition)
>       (return NO))
> 
>     (do-the-thing)))
> 
> 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))
> 
> 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...)
> 
> Hope that helps.
> 
> 
> 
> 
> 
> 
> 
> ~Leif Andersen
> 
> 
> On Mon, Nov 21, 2016 at 12:41 PM, David Storrs <david....@gmail.com> wrote:
> 
> 
> Edit:  I know I could also use call/cc and invoke the continuation to escape, 
> but that still adds another layer of indentation for something that in the 
> normal case won't be called.
> 
> It's not a big deal, but I was wondering about it.
> 
> 
> 
> 
> 
> On Mon, Nov 21, 2016 at 9:37 AM, David Storrs <david....@gmail.com> wrote:
> 
> 
> 
> 
> 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...@googlegroups.com.
> 
> For more options, visit https://groups.google.com/d/optout.

Just notice I replied the digest email instead of post a response. :p

So, let me try again and sorry for the mess...

I made this macro for exactly for the same reason (it's basically Leif's 
answer) :)

https://gist.github.com/ruliana/80d7bb46225a22a3682711ca8bd11a1d

@JohnClements, I guess you have a point :) but I think David is mostly talking 
about what we call "Guard Clauses" 
(http://refactoring.com/catalog/replaceNestedConditionalWithGuardClauses.html), 
good or bad, they are a tool for simplifying code. But I agree, don't overdo it 
^_^

Macro copied below for convenience (btw, sorry for the code, Racket is not my 
main programming language... yet)


#lang racket
(require racket/stxparam)

(provide return
         define/guard)

(define-syntax-parameter return
    (λ (stx)
      (raise-syntax-error (syntax-e stx) "can only used inside 
\"define/guard\"")))

(define-syntax-rule (define/guard (name args ...) body0 body ...)
    (define (name args ...)
      (let/cc ret
        (syntax-parameterize ([return (make-rename-transformer #'ret)])
          body0
          body ...))))

(define/guard (test2 a b)
  (when (> a b) (return 456))
  (displayln "hehehe")
  123)

-- 
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.

Reply via email to