一种想法可能是使用水泥包<>。 我首先要缓和问题:允许产生重复价值。 在这种情况下,你可以使用:
import Data.List (intersect)
intersectInfinite :: Eq a => [a] -> [a] -> [a]
intersectInfinite = intersectInfinite 1
where intersectInfinite n = intersect (take n xs) (take n ys) ++ intersectInfinite (n+1)
换言之,我们声称:
A∩B = A1 ∩B1∪ A2>∩B> > ∪ ......
载于first is. (一) 没有任何命令,但可以说有某种命令。 如果这套内容包含less,则全部内容将归还。
<c> > 载于A (at indexi)和B (at indexj,c>> 将在segment(not index)上公布。
因此,无论特定名单是否定点,都总是产生一份明确清单(有一定数量的重复)。 唯一的例外是,当你给它一份空洞的清单时,它将永远搁置。 然而,我们在此确保,交汇点的每一部分至少可以一次汇出。
使结果确定下来(如果特定名单是无限的)
Now we can make our definition better. First we make a more advanced version of take
, takeFinite
(let s first give a straight-forward, but not very efficient defintion):
takeFinite :: Int -> [a] -> (Bool,[a])
takeFinite _ [] = (True,[])
takeFinite 0 _ = (False,[])
takeFinite n (x:xs) = let (b,t) = takeFinite (n-1) xs in (b,x:t)
我们现在可以直截了当地深化,直至<><>>> > 两个名单都到底:
intersectInfinite :: Eq a => [a] -> [a] -> [a]
intersectInfinite = intersectInfinite 1
intersectInfinite :: Eq a => Int -> [a] -> [a] -> [a]
intersectInfinite n xs ys | fa && fb = intersect xs ys
| fa = intersect ys xs
| fb = intersect xs ys
| otherwise = intersect xfa xfb ++ intersectInfinite (n+1) xs ys
where (fa,xfa) = takeFinite n xs
(fb,xfb) = takeFinite n ys
This will now terminate given both lists are finite, but still produces a lot of duplicates. There are definitely ways to resolve this issue more.