You noted this with both Common Lisp and Racket, which are two completely different languages. If you use Racket or Scheme and want to return from the function earlier, you can do this using the continuation:
(define (my-function x y)
(call-with-current-continuation
(lambda (return)
(when x (return y))
;; Rest of code not evaluated if X is true
)))
, Racket, call-with-current-continuation call/cc, call-with-current-continuation - .
, cond. , , define, return:
(define-syntax define/return
(syntax-rules ()
((_ (name . args) . body)
(define (name . args)
(capture-vars (return)
(call/cc (lambda (return) . body)))))))
capture-vars, .
EDIT: Leppie define/return, , capture-vars:
(define-syntax define/return
(lambda (x)
(syntax-case x ()
[(_ (name . args) . body)
(with-syntax
([return (datum->syntax
(call/cc (lambda (return) . body))))])))
EDIT 2: , return , define/return .
return , , :
(define/return (my-function x y)
(when x (return y))
;;; more code...
)
, Common Lisp, . Common Lisp, (return y) block nil. nil, loop. nil return-from . , defun, , , :
(defun my-function (x y)
(when x (return-from my-function y))
;;; more code
)