From e5747b6c0d5751bbbcad902cc640e2ace00cd73c Mon Sep 17 00:00:00 2001 From: Jakub Jankiewicz Date: Wed, 25 Aug 2021 11:42:09 +0200 Subject: [PATCH 1/3] recipe: add let-optional --- ...create-bindinng-with-optional-arguments.md | 43 +++++++++++++++++++ www-index.scm | 1 + 2 files changed, 44 insertions(+) create mode 100644 recipes/create-bindinng-with-optional-arguments.md diff --git a/recipes/create-bindinng-with-optional-arguments.md b/recipes/create-bindinng-with-optional-arguments.md new file mode 100644 index 0000000..fb4155f --- /dev/null +++ b/recipes/create-bindinng-with-optional-arguments.md @@ -0,0 +1,43 @@ +# Create binding with optionnal arguments + +## Problem + +You have a list, example from a function, and you want to simplify checking optional parameters with default values. + +## Solution + +```scheme +(define-syntax let-optionals + (syntax-rules () + ((_ expr ((v d) ... . tail) . body) + ($let-optionals (v ...) () (d ...) () f tail expr body)))) + +(define-syntax $let-optionals + (syntax-rules () + + ((_ () (vt ...) _ (cl ...) f tail expr body) + (letrec ((f (case-lambda cl ... ((vt ... . tail) . body)))) + (apply f expr))) + + ((_ (vrf . vr*) (vt ...) (df . dr*) (cl ...) f . tailexprbody) + ($let-optionals vr* (vt ... vrf) dr* (cl ... ((vt ...) (f vt ... df))) f . tailexprbody)))) +``` + +Credit [@tallflier](https://www.reddit.com/user/tallflier/) + +**NOTE**: this macro is requirement for base implementation of [SRFI-1](https://github.com/scheme-requests-for-implementation/srfi-1). + +## Usage + +```scheme +(define (calc num . rest) + (let-optionals rest ((multipier 1) (factor 10)) + (/ (* num multipier) factor))) + +(calc 10) +;; ==> 1 +(calc 10 2) +;; ==> 2 +(calc 10 2 5) +;; ==> 4 +``` \ No newline at end of file diff --git a/www-index.scm b/www-index.scm index d710b9e..3bac713 100644 --- a/www-index.scm +++ b/www-index.scm @@ -1,5 +1,6 @@ (("Pairs and lists" "create-k-combinations-from-list" + "create-bindinng-with-optional-arguments" "find-depth-of-list" "find-index-of-element-in-list" "find-most-frequent-element-in-list" From 6e5921de891bd7ecf3c4f5a87655eb67b03bb016 Mon Sep 17 00:00:00 2001 From: Jakub Jankiewicz Date: Wed, 25 Aug 2021 12:31:57 +0200 Subject: [PATCH 2/3] typo --- recipes/create-bindinng-with-optional-arguments.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/recipes/create-bindinng-with-optional-arguments.md b/recipes/create-bindinng-with-optional-arguments.md index fb4155f..aad3bd2 100644 --- a/recipes/create-bindinng-with-optional-arguments.md +++ b/recipes/create-bindinng-with-optional-arguments.md @@ -31,8 +31,8 @@ Credit [@tallflier](https://www.reddit.com/user/tallflier/) ```scheme (define (calc num . rest) - (let-optionals rest ((multipier 1) (factor 10)) - (/ (* num multipier) factor))) + (let-optionals rest ((multiplier 1) (factor 10)) + (/ (* num multiplier) factor))) (calc 10) ;; ==> 1 From d6495a1be5be8d52e21c803058f3137ed33e4527 Mon Sep 17 00:00:00 2001 From: Jakub Jankiewicz Date: Wed, 25 Aug 2021 15:43:15 +0200 Subject: [PATCH 3/3] change name and title of optional arguments recipe --- ...-with-optional-arguments.md => bind-optional-arguments.md} | 4 ++-- www-index.scm | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) rename recipes/{create-bindinng-with-optional-arguments.md => bind-optional-arguments.md} (95%) diff --git a/recipes/create-bindinng-with-optional-arguments.md b/recipes/bind-optional-arguments.md similarity index 95% rename from recipes/create-bindinng-with-optional-arguments.md rename to recipes/bind-optional-arguments.md index aad3bd2..d729ff0 100644 --- a/recipes/create-bindinng-with-optional-arguments.md +++ b/recipes/bind-optional-arguments.md @@ -1,4 +1,4 @@ -# Create binding with optionnal arguments +# Bind optional arguments ## Problem @@ -40,4 +40,4 @@ Credit [@tallflier](https://www.reddit.com/user/tallflier/) ;; ==> 2 (calc 10 2 5) ;; ==> 4 -``` \ No newline at end of file +``` diff --git a/www-index.scm b/www-index.scm index 3bac713..f29f38e 100644 --- a/www-index.scm +++ b/www-index.scm @@ -1,6 +1,6 @@ (("Pairs and lists" "create-k-combinations-from-list" - "create-bindinng-with-optional-arguments" + "bind-optional-arguments" "find-depth-of-list" "find-index-of-element-in-list" "find-most-frequent-element-in-list"