-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename and reformat amb/exists recipes
- Loading branch information
Showing
4 changed files
with
60 additions
and
46 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters