From 746b63f6695f35666edd045c17701634740a57a8 Mon Sep 17 00:00:00 2001 From: Lassi Kortela Date: Tue, 10 Aug 2021 13:46:40 +0300 Subject: [PATCH] Rename and reformat amb/exists recipes --- recipes/find-if-element-exists-in-list.md | 37 ---------------- recipes/find-stride-across-lists.md | 42 +++++++++++++++++++ .../{amb.md => nondeterminism-using-amb.md} | 21 ++++++---- www-index.scm | 6 ++- 4 files changed, 60 insertions(+), 46 deletions(-) delete mode 100644 recipes/find-if-element-exists-in-list.md create mode 100644 recipes/find-stride-across-lists.md rename recipes/{amb.md => nondeterminism-using-amb.md} (77%) diff --git a/recipes/find-if-element-exists-in-list.md b/recipes/find-if-element-exists-in-list.md deleted file mode 100644 index a8aa958..0000000 --- a/recipes/find-if-element-exists-in-list.md +++ /dev/null @@ -1,37 +0,0 @@ -# Test whether a given property exists in a sequence of N lists - -## Problem - -I have sequence of lists and I want to check if the lists have single element -that return true when using given comparator. - -## Solution - -```scheme -(define (exists? p . a*) - (letrec - ((car-of - (lambda (a) - (map car a))) - (cdr-of - (lambda (a) - (map cdr a))) - (any-null - (lambda (a) - (memq '() a))) - (exists* - (lambda (a*) - (and (not (any-null a*)) - (or (apply p (car-of a*)) - (exists* (cdr-of a*))))))) - (exists* a*))) -``` - -Credit: [Nils M. Holm](http://t3x.org) (ref: [exists.scm](http://t3x.org/s9fes/exists.scm.html) - -## Usage - -```scheme -(exists < '(9 1) '(8 2) '(7 3)) -;; ==> #t because (< 1 2 3) -``` diff --git a/recipes/find-stride-across-lists.md b/recipes/find-stride-across-lists.md new file mode 100644 index 0000000..78be153 --- /dev/null +++ b/recipes/find-stride-across-lists.md @@ -0,0 +1,42 @@ +# Find stride across lists + +## Problem + +I store a matrix as a list of rows, where each row is a list of column +values for that row. I want to check if there is a column in the +matrix that satisfies a comparator `p`. + +`p` can actually return any non-`#f` value and that value is returned +as-is. And the lists don't actually have to represent a matrix: the +rows don't all need to have the same number of columns. + +## Solution + +```scheme +(define (find-stride p . a*) + (letrec + ((car-of + (lambda (a) + (map car a))) + (cdr-of + (lambda (a) + (map cdr a))) + (any-null + (lambda (a) + (memq '() a))) + (find* + (lambda (a*) + (and (not (any-null a*)) + (or (apply p (car-of a*)) + (find* (cdr-of a*))))))) + (find* a*))) +``` + +Credit: [Nils M. Holm](https://t3x.org) (ref: [exists.scm](https://t3x.org/s9fes/exists.scm.html)) + +## Usage + +```scheme +(find-stride < '(9 1) '(8 2) '(7 3)) +;; ==> #t ; because (< 1 2 3) +``` diff --git a/recipes/amb.md b/recipes/nondeterminism-using-amb.md similarity index 77% rename from recipes/amb.md rename to recipes/nondeterminism-using-amb.md index 29e29bf..4885bae 100644 --- a/recipes/amb.md +++ b/recipes/nondeterminism-using-amb.md @@ -1,14 +1,20 @@ -# Create McCarthy's AMB operator. +# Nondeterminism using amb ## Problem -I want to implement amb operator, so I can use nondeterminism in my code. +I want to use McCarthy's `amb` operator to express nondeterminism in +my code. ## Solution -This solution use code from: -* [/use-list-as-stack/](Use list as Stack) - `push!` and `pop!` -* [/find-if-element-in-list-exists](Find if element exists in list) - `exists?` +This solution depends on the `syntax-rules` ellipsis extension that is +present in R7RS and +[SRFI 46](https://srfi.schemers.org/srfi-46/srfi-46.html). + +It also depends on two other recipes: + +* [Use list as stack](/use-list-as-stack/) - `push!` and `pop!` +* [Find stride across lists](/find-stride-across-lists) - `find-stride` ```scheme (define *amb-stack* '()) @@ -54,11 +60,10 @@ This solution use code from: (amb)))))) ``` -Code requires syntax-rules to support [SRFI-46](https://srfi.schemers.org/srfi-46/srfi-46.html) required by R7RS. - Credit: [Jakub T. Jankiewicz](https://jcubic.pl/me) -based on code by [Nils M Holm](https://t3x.org/) (ref: [amb.scm](http://t3x.org/s9fes/amb.scm.html)) +based on code by [Nils M Holm](https://t3x.org/) +(ref: [amb.scm](https://t3x.org/s9fes/amb.scm.html)) ## Usage diff --git a/www-index.scm b/www-index.scm index b923d8d..def430e 100644 --- a/www-index.scm +++ b/www-index.scm @@ -1,8 +1,9 @@ (("Pairs and lists" "create-k-combinations-from-list" + "find-depth-of-list" "find-index-of-element-in-list" "find-most-frequent-element-in-list" - "find-depth-of-list" + "find-stride-across-lists" "index-list" "remove-duplicates-from-list" "remove-element-from-list" @@ -20,6 +21,9 @@ "use-list-as-set" "use-list-as-queue") + ("Algorithms" + "nondeterminism-using-amb") + ("Strings" "convert-any-value-to-string" "join-list-of-strings-with-delimiter"