简体   繁体   中英

data List - checking if a list is empty

I have the following. It just simply checks if a List is empty. However if I try to run it with main , I get an error. How do I need to change the main function to run it properly?

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:

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.

{-# 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:

 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. The fromList function does nothing, but to convert the Haskell Listimplementation (here [1,2]) into your own, so you can call vnull on it. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM