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 Haskel-实施<*&燃气轮机;名单_List_Haskell - Fatal编程技术网

List Haskel-实施<*&燃气轮机;名单

List Haskel-实施<*&燃气轮机;名单,list,haskell,List,Haskell,我想用Haskell实现我的列表。 但我不能这么做。执行的原因 data List a = a :+ (List a) | Empty deriving Show infixr 9 :+ instance Functor List where fmap _ Empty = Empty fmap f (a :+ xs) = f a :+ fmap f xs instance Applicative List where pure x = x :+ Emp

我想用Haskell实现我的列表。 但我不能这么做。执行
的原因

data List a = a :+ (List a) | Empty deriving Show
infixr 9 :+

instance Functor List where
  fmap _ Empty     = Empty
  fmap f (a :+ xs) = f a :+ fmap f xs

instance Applicative List where
  pure x           = x :+ Empty
  Empty     <*> _  = Empty
  (f :+ fs) <*> xs =  fmap f xs :+  (fs <*> xs) -- Error

main :: IO ()
main = do
  print $ 1 :+ 2 :+ 3 :+ Empty
  print $ fmap (^2) (1 :+ 2 :+ 3 :+ Empty)
  print $ ((+1) :+ (*2) :+ (^2) :+ Empty) <*> (1 :+ 2 :+ 3 :+ Empty)
数据列表a=a:+(列表a)|空派生显示
infixr 9:+
实例函子列表,其中
fmap uu空=空
fmap f(a:+xs)=f a:+fmap f xs
实例应用程序列表,其中
纯x=x:+空
空的
(f:+fs)xs=fmap f xs:+(fs xs)--错误
main::IO()
main=do
打印$1:+2:+3:+Empty
打印$fmap(^2)(1:+2:+3:+Empty)
打印$(+1):+(*2):+(^2):+空)(1:+2:+3:+空)
错误是

无法将预期类型“b”与实际类型“列表b”匹配
“b”是一个刚性类型变量,由…

绑定,正如zaquest指出的,您是在预加而不是串联

因此,首先实施concat:

infixr 5 +:+
(+:+) :: List a -> List a -> List a
Empty +:+ ys = ys
(x :+ xs) +:+ ys = x :+ (xs +:+ ys)
然后用它来代替

(f :+ fs) <*> xs =  fmap f xs +:+  (fs <*> xs)
(f:+fs)xs=fmap f xs+:+(fs-xs)

(注:当然你可以重命名它-我认为
+:+
看起来最像
:+
这里)

你试图使用
:+
构造函数连接两个列表。谢谢!我现在明白了。谢谢你的实施!