0

I asked a question about this earlier but I don't think I really knew what I was asking. I think I understand my problem slightly better now.

I'm using netwire, an arrowized FRP library, and having some trouble getting this arrowloop implemented.

I have

f :: ArrowLoop r =>  a  -> r [a]  a
g :: ArrowLoop r => [a] -> r ()  [a]

Such that g basically ties every element in the given list with the list of every other element using f...um...this is hard to really say using words so I'll give an example with a list of length 4:

g [x0, x1, x2, x3] = proc _ -> do
  rec
     y0 <- f x0 -< [y1, y2, y3]
     y1 <- f x1 -< [y0, y2, y3]
     y2 <- f x2 -< [y0, y1, y3]
     y3 <- f x3 -< [y0, y1, y2]
  returnA -< [y0, y1, y2, y3]

(I have a helper function selects :: [a] -> [(a,[a])], that turns something like [x,y,z] into [(x, [y,z]), (y, [x,z]), (x, [x,y])])

Now...I've compiled this hard-coded version and this already works and delivers exactly the results that I wanted mostly. It runs without any funky <> issues.

Does anyone know if it's possible to do this "exact" thing...but with a general number of list elements?


For reference, my implementation of selects comes from Simon Marlow,

selects :: [a] -> [(a,[a])]
selects = go []
  where
    go xs [] = []
    go xs (y:ys) = (y,xs++ys) : go (y:xs) ys
4

1 回答 1

2

我无法正确测试这个,因为我没有任何例子f可以运行它,但我相信这概括了你所拥有的箭头模式:

g :: ArrowLoop r => [a] -> r () [a]
g = listLoop . map f

listLoop :: ArrowLoop r => [r [a] a] -> r () [a]
listLoop l = const [] ^>> go l where
    go []     = arr (const [])
    go (r:rs) = proc bs -> do
        rec a  <- r     -< bs ++ as
            as <- go rs -< bs ++ [a]
        returnA -< a : as

因此,首先我们映射 withf以获取箭头列表,[f x0, f x1, ...]然后将其提供给我命名的组合器,该组合listLoop器采用箭头列表并定义一个递归箭头,该箭头与您的selects. bs是前面箭头的结果列表r(类似于xsin selects),as是后面箭头的结果r。对于每一个r,我们输入其他箭头的结果,而不是其r自身的结果(即a)。然后我们递归,附加a到前面的结果列表。

于 2014-01-18T15:43:54.737 回答