Skip to content

Commit

Permalink
recipe: add exists? function
Browse files Browse the repository at this point in the history
  • Loading branch information
jcubic authored and lassik committed Aug 10, 2021
1 parent 7c85cf0 commit c869c48
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions recipes/find-if-element-exists-in-list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 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)
```

0 comments on commit c869c48

Please sign in to comment.