From f83f3ea71a897428902d78b217b207b62e7ad8d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Thu, 14 May 2020 11:01:44 +0200 Subject: Add label macro. --- module/util.scm | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'module/util.scm') diff --git a/module/util.scm b/module/util.scm index 0417d36f..10e36d4e 100644 --- a/module/util.scm +++ b/module/util.scm @@ -14,7 +14,7 @@ -> ->> set set-> aif awhen tree-map let-lazy let-env case* define-many - and=>> + and=>> label print-and-return ) #:replace (let* set! define-syntax @@ -256,6 +256,23 @@ (begin (def symbols value) ... (define-many def rest ...))])) +;; Attach a label to a function, allowing it to call itself +;; without actually giving it a name (can also be thought +;; of as letrec-1). +;; @example +;; ((label fact +;; (match-lambda +;; [0 1] +;; [x (* x (fact (1- x)))])) +;; 5) +;; @end example +(define-syntax label + (syntax-rules () + [(_ self proc) + (letrec ((self proc)) + proc)])) + + ;; This function borrowed from web-ics (calendar util) (define* (sort* items comperator #:optional (get identity)) "A sort function more in line with how python's sorted works" -- cgit v1.2.3