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 +```