[英]data List - checking if a list is empty
I have the following. 我有以下几点。 It just simply checks if a
List
is empty. 它只是检查
List
是否为空。 However if I try to run it with main
, I get an error. 但是,如果尝试使用
main
运行它,则会出现错误。 How do I need to change the main
function to run it properly? 我如何更改
main
功能才能正常运行?
data List a = Nil | Cons a (List a)
vnull :: List a -> Bool
vnull Nil = True
vnull _ = False
main = do print (vnull [1,2])
The error is the following: 错误如下:
Couldn't match expected type `List a0' with actual type `[Integer]'
In the first argument of `vnull', namely `[1, 2]'
In the first argument of `print', namely `(vnull [1, 2])'
In a stmt of a 'do' block: print (vnull [1, 2])
Changing to: 更改为:
main = print $ vnull $ Cons 1 $ Cons 2 Nil
produces: 产生:
False
it is working like it is implemented: 它像实现了一样工作:
vnull Nil
True
vnull (Cons 1 Nil)
False
vnull (Cons 2 (Cons 1 Nil)
False
...
you can try following commands in ghci
, to get all information about []
datatype: 您可以尝试使用
ghci
以下命令来获取有关[]
数据类型的所有信息:
Prelude> :t []
[] :: [t]
Prelude> :i []
data [] a = [] | a : [a] -- Defined in ‘GHC.Types’
instance Eq a => Eq [a] -- Defined in ‘GHC.Classes’
instance Monad [] -- Defined in ‘GHC.Base’
instance Functor [] -- Defined in ‘GHC.Base’
instance Ord a => Ord [a] -- Defined in ‘GHC.Classes’
instance Read a => Read [a] -- Defined in ‘GHC.Read’
instance Show a => Show [a] -- Defined in ‘GHC.Show’
instance Applicative [] -- Defined in ‘GHC.Base’
instance Foldable [] -- Defined in ‘Data.Foldable’
instance Traversable [] -- Defined in ‘Data.Traversable’
instance Monoid [a] -- Defined in ‘GHC.Base’
for your function to be applicable to []
parameter you need something like: 为了使函数适用于
[]
参数,您需要类似以下内容:
vnull :: [a] -> Bool
vnull [] = True
vnull _ = False
If you want to be able to use your List
type with the usual list syntax, you'll have to use GHC extensions. 如果您希望能够使用通常的列表语法使用
List
类型,则必须使用GHC扩展名。
{-# LANGUAGE OverloadedLists, TypeFamilies #-} -- at the very top of the file
import qualified GHC.Exts as E
import Data.Foldable
data List a = Nil | Cons a (List a) deriving (Show, Eq, Ord)
instance Foldable List where
foldr _ n Nil = n
foldr c n (Cons x xs) = x `c` foldr c n xs
instance E.IsList List where
type Item (List a) = a
fromList = foldr Cons Nil
toList = toList
List a
and 和
[]
Are two different constructors and not the same data type, The function would work for a List a type, so instead of " [] " try this: 是两个不同的构造函数,而不是相同的数据类型,该函数适用于List类型,因此请尝试使用“ [] ”代替:
vnull :: List a -> Bool
vnull Nil = True
vnull (Cons a expand) = False
Then in main 然后在主要
main = do print (vnull $ Cons 1 (Cons 2 Nil)) --This is just an example you can throw in a -List a- type of any length.
And this should fix it. 这应该可以解决。
This is what you are missing: 这是您所缺少的:
data List a = Nil | Cons a (List a) deriving Show
fromList = foldr Cons Nil
vnull :: List a -> Bool
vnull Nil = True
vnull _ = False
main = do print (vnull $ fromList [1,2])
The deriving Show is not necessary now, but will be, when you actually want to print a List and not a Bool. 现在不需要派生的Show,但是当您实际要打印List而不是Bool时,将需要Show。 The fromList function does nothing, but to convert the Haskell Listimplementation (here [1,2]) into your own, so you can call vnull on it.
fromList函数不执行任何操作,只是将Haskell Listimplementation(此处[1,2])转换为您自己的,因此您可以在其上调用vnull。 You could have also called
您可能还打电话
main = do print $ vnull (Cons 1 (Cons 2 (Nil)))
您可以: instance Foldeable List where foldMap f Nil = mempty foldMap f (Cons x ls) = mappend (fx) (foldMap ls)
然后使用(fold [1,2])::List Int
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.