haskell或字符串(嵌套列表a)-为什么不';这不管用吗
我正在尝试附加一个函数,该函数可以在嵌套列表(如常规列表)上工作。我想使用一个字符串(嵌套的a),以便它返回错误或附加列表。但它一直在失败。我没有在任何地方做NestedList[NestedList a]。为什么它说它期望[NestedList(NestedList a)] 给了我这个错误haskell或字符串(嵌套列表a)-为什么不';这不管用吗,haskell,either,Haskell,Either,我正在尝试附加一个函数,该函数可以在嵌套列表(如常规列表)上工作。我想使用一个字符串(嵌套的a),以便它返回错误或附加列表。但它一直在失败。我没有在任何地方做NestedList[NestedList a]。为什么它说它期望[NestedList(NestedList a)] 给了我这个错误 flatten_list.hs:18:52: Couldn't match type `a' with `NestedList a' `a' is a rigid type vari
flatten_list.hs:18:52:
Couldn't match type `a' with `NestedList a'
`a' is a rigid type variable bound by
the type signature for
append :: NestedList a
-> NestedList a -> Either String (NestedList a)
at flatten_list.hs:15:9
Expected type: [NestedList (NestedList a)]
Actual type: [NestedList a]
In the first argument of `List', namely `a'
In the first argument of `flatten', namely `(List a)'
In the first argument of `(++)', namely `flatten (List a)'
flatten_list.hs:18:69:
Couldn't match type `a' with `NestedList a'
`a' is a rigid type variable bound by
the type signature for
append :: NestedList a
-> NestedList a -> Either String (NestedList a)
at flatten_list.hs:15:9
Expected type: [NestedList (NestedList a)]
Actual type: [NestedList a]
In the first argument of `List', namely `b'
In the first argument of `flatten', namely `(List b)'
In the second argument of `(++)', namely `flatten (List b)'
你在问题中写的附加似乎只是想将两个列表展平并连接起来,这是毫无意义的,因为你正在抛弃所有的结构。如果这是需要的行为,则只需使用常规列表:
append' :: NestedList a -> NestedList a -> Either String [a]
append' (Elem x) (Elem y) = Right $ [x,y]
append' (Elem _) _ = Left ""
append' _ (Elem _) = Left ""
append' a b = Right $ flatten a ++ flatten b
您还可以根据以下内容定义数据类型:
此定义与您给出的定义同构。问题在于
列表
数据构造函数接受类型为[NestedList a]
的参数,但在右侧的行(列表(展平(列表a)+展平(列表b))
中,您为其提供了类型为[a]
的值(即展平(列表a)的结果)++展平(列表b)
)。这就是为什么它试图推断类型[NestedList(NestedList a)]
。我尝试了它附加的位置(List xs)(List ys)=fmap List$sequenceA$zipWith append xs ys\n\n\n。不确定为什么我得到了这个调用打印的“元素不允许”(append(List[List[Elem 1,Elem 2]])(List[Elem 1,Elem 2,List[Elem 1,Elem 2]])?如果任何列表不是列表而是元素,我想打印“Elements not allowed”,我认为append()(Elem a)=Left“Elements is not allowed”append(Elem a)(=Left“Elements is not allowed”正在执行,append(list xs)(list ys)中没有递归=fmap List$sequenceA$zipWith append xs ys。但在append
中存在递归。调用zipWith
调用append
,因此append
调用自身。这意味着它将始终减少为元素(我相信),而append将始终提供左。存在递归,但我需要在开始时检查列表,但列表中可以也将有元素:(您可以创建另一个函数,在开始时检查列表,然后如果它不是Elem
调用append
-----------------checkappend::NestedList a->NestedList a->任一字符串(NestedList a)checkappend(list_u)(Elem_u)=Left“不允许元素”checkappend(Elem_u)(list_u)=Left“元素不允许”勾选“元素不允许”勾选“元素不允许”勾选“元素不允许”勾选“元素不允许”勾选“元素不允许”勾选“元素不允许”勾选“元素不允许”勾选“元素不允许”
import Data.Traversable
append :: NestedList a -> NestedList a -> Either String (NestedList a)
append (Elem x) (Elem y) = Right $ List [Elem x, Elem y]
append (Elem _) _ = Left ""
append _ (Elem _) = Left ""
append (List xs) (List ys) = fmap List $ sequenceA $ zipWith append xs ys
append' :: NestedList a -> NestedList a -> Either String [a]
append' (Elem x) (Elem y) = Right $ [x,y]
append' (Elem _) _ = Left ""
append' _ (Elem _) = Left ""
append' a b = Right $ flatten a ++ flatten b
import Control.Monad.Free
type NestedList = Free []
flatten :: NestedList a -> [a]
flatten = retract
append :: NestedList a -> NestedList a -> Either String (NestedList a)
append (Pure x) (Pure y) = Right $ Free [Pure x, Pure y]
append (Pure _) _ = Left ""
append _ (Pure _) = Left ""
append (Free xs) (Free ys) = fmap Free $ sequenceA $ zipWith append xs ys