Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 数据列表-检查列表是否为空_List_Haskell_Syntactic Sugar - Fatal编程技术网

List 数据列表-检查列表是否为空

List 数据列表-检查列表是否为空,list,haskell,syntactic-sugar,List,Haskell,Syntactic Sugar,我有以下几点。它只是简单地检查列表是否为空。但是,如果我尝试使用main运行它,我会得到一个错误。我需要如何更改main函数才能正常运行 data List a = Nil | Cons a (List a) vnull :: List a -> Bool vnull Nil = True vnull _ = False main = do print (vnull [1,2]) 错误如下: Couldn't match expected type `List a0' with

我有以下几点。它只是简单地检查
列表是否为空。但是,如果我尝试使用
main
运行它,我会得到一个错误。我需要如何更改
main
函数才能正常运行

data List a = Nil | Cons a (List a)

vnull :: List a -> Bool
vnull Nil = True 
vnull _ = False 

main = do print (vnull [1,2])
错误如下:

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])
改为:

main = print $ vnull $ Cons 1 $ Cons 2 Nil
产生:

False
改为:

main = print $ vnull $ Cons 1 $ Cons 2 Nil
产生:

False

它的工作方式与实现的方式类似:

vnull Nil
True

vnull (Cons 1 Nil)
False

vnull (Cons 2 (Cons 1 Nil)
False

...
您可以在
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’
要使函数适用于
[]
参数,您需要以下内容:

vnull ::  [a] -> Bool
vnull [] = True
vnull _ = False

它的工作方式与实现的方式类似:

vnull Nil
True

vnull (Cons 1 Nil)
False

vnull (Cons 2 (Cons 1 Nil)
False

...
您可以在
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’
要使函数适用于
[]
参数,您需要以下内容:

vnull ::  [a] -> Bool
vnull [] = True
vnull _ = False
你可以:

实例可折叠列表,其中
foldMap f Nil=mempty
折叠贴图f(Cons x ls)=贴图pend(f x)(折叠贴图ls)
然后使用
(折叠[1,2])::List Int

您可以:
List a

实例可折叠列表,其中
foldMap f Nil=mempty
折叠贴图f(Cons x ls)=贴图pend(f x)(折叠贴图ls)
然后使用
(折叠[1,2])::List Int

List a

如果是两个不同的构造函数且数据类型不相同,则函数将适用于列表a类型,因此请尝试以下操作,而不是“[]”:

 vnull :: List a -> Bool
 vnull Nil = True
 vnull (Cons a expand) = False
那么大体上

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. 
这应该可以解决它

如果是两个不同的构造函数且数据类型不相同,则函数将适用于列表a类型,因此请尝试以下操作,而不是“[]”:

 vnull :: List a -> Bool
 vnull Nil = True
 vnull (Cons a expand) = False
那么大体上

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. 

这应该可以解决这个问题。

如果你想使用你的
列表
类型和通常的列表语法,你必须使用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

如果你想使用你的
列表
类型和通常的列表语法,你必须使用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

这就是你所缺少的:

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])
导出显示现在不是必需的,但当您真正想要打印列表而不是Bool时,它将是必需的。fromList函数什么都不做,只是将Haskell Listimplementation(这里是[1,2])转换成您自己的,这样您就可以对它调用vnull。你也可以打电话


main=do print$vnull(Cons 1(Cons 2(Nil))
这就是您缺少的内容:

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])
导出显示现在不是必需的,但当您真正想要打印列表而不是Bool时,它将是必需的。fromList函数什么都不做,只是将Haskell Listimplementation(这里是[1,2])转换成您自己的,这样您就可以对它调用vnull。你也可以打电话


main=do print$vnull(Cons 1(Cons 2(Nil))
print
需要您在
列表
的数据声明末尾添加
派生显示
:如果您只有一个
IO
操作(在您的情况下,
print
ing),则不需要
do
。btw<这样做时,code>fromList=foldr Cons Nil
是一个非常方便的功能。
print
需要您在
List
的数据声明末尾添加
deriving Show
:如果您只有一个
IO
操作(在您的情况下,
print
ing),则不需要
do
.顺便说一句<这样做时,code>fromList=foldr Cons Nil是一个非常方便的函数。该类称为
Foldable
。更重要的是,它的
fold
方法不能满足您的要求。该类称为
Foldable
。更重要的是,它的
fold
方法不能满足您的要求。