From 2c012c57dbade80e92c21f784fd694fa50940ce9 Mon Sep 17 00:00:00 2001 From: Nathan McCarty Date: Wed, 8 Jan 2025 08:56:44 -0500 Subject: [PATCH] Add cartProd to util --- src/Util.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/Util.md b/src/Util.md index 3c2388e..1523f8f 100644 --- a/src/Util.md +++ b/src/Util.md @@ -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 +```