gpt4 book ai didi

haskell - 链接类型为 `a b [c]` 和 `a c d` 的箭头

转载 作者:行者123 更新时间:2023-12-04 15:13:35 25 4
gpt4 key购买 nike

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

最佳答案

你不能只用 Arrow类型类。起重 a b c进入 a [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

你的功能很简单:
chain :: ArrowChoice a => a b [c] -> a c d -> a b [d]
chain f g = f >>> mapA g

没有 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 (:)))

关于haskell - 链接类型为 `a b [c]` 和 `a c d` 的箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13665879/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com