0

通常のリストのようにネストされたリストで機能する関数を追加しようとしています。エラーまたは追加されたリストを返すように、Either String (Nested a) を使用したいと考えています。しかし、それは失敗し続けます。NestedList[NestedList a] どこでもやっていません。[NestedList (NestedList a)] が期待されていると言うのはなぜですか

module Main where

data NestedList a=Elem a | List[NestedList a] deriving Show

flatten ::NestedList a->[a]
flatten (Elem x)=[x]
flatten (List(x:xs))=flatten(x)++flatten(List xs)
--flatten NestedList (x:xs)=flatten(x)++flatten(xs)
flatten(List [])=[]

------------------

count::[a]->Int
count a=length (a)

-----------------
append::NestedList a->NestedList a->Either String (NestedList a)
append (_) (Elem a)=Left "Elements are not allowed"
append (Elem a) (_)=Left "Elements are not allowed"
append (List a) (List b)=Right (List (flatten(List a)++flatten(List b)))
-------------------

main=do
        print(flatten (List[Elem 1, Elem 2,List[Elem 1, Elem 2 ]]))
        print(count(flatten (List[Elem 1, Elem 2,List[Elem 1, Elem 2 ]])))
        print(append (List[List[Elem 1, Elem 2 ]]) (List[Elem 1, Elem 2,List[Elem 1, Elem 2 ]]    ))  

このエラーが表示されます

   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)'
4

1 に答える 1

1
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 

あなたが質問に書いた追加は、2つのリストを単純化して連結したいだけのようですが、すべての構造を捨てているので、これはかなり無意味です。これが望ましい動作である場合は、通常のリストを使用してください。

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

free モナドの観点からデータ型を定義することもできます:

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 

この定義は、あなたが与えたものと同型です。

于 2014-01-17T03:47:38.407 に答える