Add cartProd to util

This commit is contained in:
Nathan McCarty 2025-01-08 08:56:44 -05:00
parent fde322e772
commit 2c012c57db

View file

@ -7,6 +7,7 @@ module Util
import Data.SortedSet
import Data.String
import Data.List.Lazy
%default total
```
@ -98,3 +99,23 @@ We first check to see if the needle is a prefix of the top level haystack, befor
then True
else isSubstr' str | x
```
## Lazy List
### Cartesian product
Lazily take the cartesian product of two foldables
```idris
export
cartProd : Foldable a => Foldable b => a e -> b f -> LazyList (e, f)
cartProd x y =
let y = foldToLazy y
in foldr (\e, acc => combine e y acc) [] x
where
foldToLazy : Foldable a' => a' e' -> LazyList e'
foldToLazy x = foldr (\e, acc => e :: acc) [] x
combine : e -> LazyList f -> LazyList (e, f) -> LazyList (e, f)
combine x [] rest = rest
combine x (y :: ys) rest = (x, y) :: combine x ys rest
```