链接类型`ab [c]`和`acd`的箭头

Nik*_*kov 6 haskell arrows

我有一个箭头输出值列表(a b [c])和另一个箭头接受该类型的单个值(a c d).我需要的基本上是一种链接它们或提升第二个箭头的方法a [c] [d].

Vit*_*tus 5

只需Arrow键入class 就不能这样做.提升a b ca [b] [c]需要之间的选择[](:)情况.幸运的是,我们ArrowChoice提供了这个操作.

mapA :: ArrowChoice a => a b c -> a [b] [c]
mapA f = proc list -> case list of
    []   -> returnA -< []
    x:xs -> do
        y  <- f      -< x
        ys <- mapA f -< xs
        returnA      -< y:ys
Run Code Online (Sandbox Code Playgroud)

那么你的功能就是:

chain :: ArrowChoice a => a b [c] -> a c d -> a b [d]
chain f g = f >>> mapA g
Run Code Online (Sandbox Code Playgroud)

如果没有proc表示法,我们需要一个将列表构造函数转换为Either:

listCase :: [a] -> Either () (a, [a])
listCase []     = Left ()
listCase (x:xs) = Right (x,xs)

mapA f = arr listCase >>>
    arr (const []) ||| (f *** mapA f >>> arr (uncurry (:)))
Run Code Online (Sandbox Code Playgroud)