Skip to content

Commit

Permalink
Rename and reformat amb/exists recipes
Browse files Browse the repository at this point in the history
  • Loading branch information
lassik committed Aug 10, 2021
1 parent c869c48 commit 746b63f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 46 deletions.
37 changes: 0 additions & 37 deletions recipes/find-if-element-exists-in-list.md

This file was deleted.

42 changes: 42 additions & 0 deletions recipes/find-stride-across-lists.md
Original file line number Diff line number Diff line change
@@ -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)
```
21 changes: 13 additions & 8 deletions recipes/amb.md → recipes/nondeterminism-using-amb.md
Original file line number Diff line number Diff line change
@@ -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* '())
Expand Down Expand Up @@ -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

Expand Down
6 changes: 5 additions & 1 deletion www-index.scm
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
Expand Down

0 comments on commit 746b63f

Please sign in to comment.