Haskell - 为什么我收到 Not in scope: data constructor ‘ZipList’ 错误
Haskell - why am I getting Not in scope: data constructor ‘ZipList’ error
我正在学习 Haskell 中的 applicative functors 学习你-a-haskell 书。
但是每当我尝试在 ghci 中输入以下代码时:
:{
instance Applicative ZipList where
pure x = ZipList (repeat x)
ZipList fs <*> ZipList xs = ZipList (zipWith (\f x -> f x) fs xs)
:}
我得到三个错误:
<interactive>:137:22: error:
Not in scope: type constructor or class ‘ZipList’
<interactive>:139:9: error:
Not in scope: data constructor ‘ZipList’
<interactive>:139:24: error:
Not in scope: data constructor ‘ZipList’
我试过加载:
import Data.List
import Data.Char
我尝试 搜索 ZipList 但没有成功。
我试过 运行接下来几个没有实例声明的表达式:
getZipList $ (+) <$> ZipList [1,2,3] <*> ZipList [100,100,100]
但它们也失败并出现以下错误:
<interactive>:142:1: error:
Variable not in scope: getZipList :: f0 Integer -> t
<interactive>:142:22: error:
Data constructor not in scope: ZipList :: [Integer] -> f0 Integer
<interactive>:142:42: error:
Data constructor not in scope: ZipList :: [Integer] -> f0 Integer
我也尝试搜索并找到了这个答案:
但这对我没有帮助。
ZipList
已经存在于 Control.Applicative
中并使用那里定义的 Applicative
实例。您无法重新定义该实例。
>> import Control.Applicative
>> getZipList $ (+) <$> ZipList [1,2,3] <*> ZipList [100,100,100]
[101,102,103]
>> getZipList $ liftA2 (+) (ZipList [1,2,3]) (ZipList [100,100,100])
[101,102,103]
要定义您自己的,您必须定义一个 new ZipList'
我们读作“ZipList
素数”:
-- >> getZipList' $ (+) <$> ZipList' [1,2,3] <*> ZipList' [100,100,100]
-- [101,102,103]
newtype ZipList' a = ZipList' { getZipList' :: [a] }
instance Functor ZipList' where
fmap f (ZipList' as) = ZipList' (fmap f as)
instance Applicative ZipList' where
pure a = ZipList' (repeat a)
ZipList' fs <*> ZipList' xs = ZipList' (zipWith (\f x -> f x) fs xs)
您还可以推导出Functor
。我建议为 fmap
、pure
、(<*>)
:
编写实例签名
{-# Language DeriveFunctor #-}
{-# Language DerivingStrategies #-}
{-# Language InstanceSigs #-}
import Control.Applicative (liftA2)
newtype ZipList' a = ZipList' { getZipList' :: [a] }
deriving
stock Functor
-- instance Functor ZipList' where
-- fmap :: (a -> a') -> (ZipList' a -> ZipList' a')
-- fmap f (ZipList' as) = ZipList' (fmap f as)
instance Applicative ZipList' where
pure :: a -> ZipList' a
pure a = ZipList' (repeat a)
(<*>) :: ZipList' (a -> b) -> ZipList' a -> ZipList' b
ZipList' fs <*> ZipList' xs = ZipList' (zipWith ($) fs xs)
liftA2 :: (a -> b -> c) -> (ZipList' a -> ZipList' b -> ZipList' c)
liftA2 (·) (ZipList' as) (ZipList' bs) = ZipList' (zipWith (·) as bs)
您可以将 (\f x -> f x)
写成 ($)
或 id
。
我正在学习 Haskell 中的 applicative functors 学习你-a-haskell 书。 但是每当我尝试在 ghci 中输入以下代码时:
:{
instance Applicative ZipList where
pure x = ZipList (repeat x)
ZipList fs <*> ZipList xs = ZipList (zipWith (\f x -> f x) fs xs)
:}
我得到三个错误:
<interactive>:137:22: error:
Not in scope: type constructor or class ‘ZipList’
<interactive>:139:9: error:
Not in scope: data constructor ‘ZipList’
<interactive>:139:24: error:
Not in scope: data constructor ‘ZipList’
我试过加载:
import Data.List
import Data.Char
我尝试 搜索 ZipList 但没有成功。
我试过 运行接下来几个没有实例声明的表达式:
getZipList $ (+) <$> ZipList [1,2,3] <*> ZipList [100,100,100]
但它们也失败并出现以下错误:
<interactive>:142:1: error:
Variable not in scope: getZipList :: f0 Integer -> t
<interactive>:142:22: error:
Data constructor not in scope: ZipList :: [Integer] -> f0 Integer
<interactive>:142:42: error:
Data constructor not in scope: ZipList :: [Integer] -> f0 Integer
我也尝试搜索并找到了这个答案:
ZipList
已经存在于 Control.Applicative
中并使用那里定义的 Applicative
实例。您无法重新定义该实例。
>> import Control.Applicative
>> getZipList $ (+) <$> ZipList [1,2,3] <*> ZipList [100,100,100]
[101,102,103]
>> getZipList $ liftA2 (+) (ZipList [1,2,3]) (ZipList [100,100,100])
[101,102,103]
要定义您自己的,您必须定义一个 new ZipList'
我们读作“ZipList
素数”:
-- >> getZipList' $ (+) <$> ZipList' [1,2,3] <*> ZipList' [100,100,100]
-- [101,102,103]
newtype ZipList' a = ZipList' { getZipList' :: [a] }
instance Functor ZipList' where
fmap f (ZipList' as) = ZipList' (fmap f as)
instance Applicative ZipList' where
pure a = ZipList' (repeat a)
ZipList' fs <*> ZipList' xs = ZipList' (zipWith (\f x -> f x) fs xs)
您还可以推导出Functor
。我建议为 fmap
、pure
、(<*>)
:
{-# Language DeriveFunctor #-}
{-# Language DerivingStrategies #-}
{-# Language InstanceSigs #-}
import Control.Applicative (liftA2)
newtype ZipList' a = ZipList' { getZipList' :: [a] }
deriving
stock Functor
-- instance Functor ZipList' where
-- fmap :: (a -> a') -> (ZipList' a -> ZipList' a')
-- fmap f (ZipList' as) = ZipList' (fmap f as)
instance Applicative ZipList' where
pure :: a -> ZipList' a
pure a = ZipList' (repeat a)
(<*>) :: ZipList' (a -> b) -> ZipList' a -> ZipList' b
ZipList' fs <*> ZipList' xs = ZipList' (zipWith ($) fs xs)
liftA2 :: (a -> b -> c) -> (ZipList' a -> ZipList' b -> ZipList' c)
liftA2 (·) (ZipList' as) (ZipList' bs) = ZipList' (zipWith (·) as bs)
您可以将 (\f x -> f x)
写成 ($)
或 id
。