Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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

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_Recursion_Type Mismatch_Algebraic Data Types - Fatal编程技术网

List 我对哈斯克尔的重新定义';长度';功能赢得';行不通

List 我对哈斯克尔的重新定义';长度';功能赢得';行不通,list,haskell,recursion,type-mismatch,algebraic-data-types,List,Haskell,Recursion,Type Mismatch,Algebraic Data Types,有人能解释一下我如何修复我的程序吗。 Haskell是个新手,他一直在尝试创建一个length函数来计算任何类型列表的长度 我的目标是使用数据来实现这一点,因为我想创建一个全新的类型来实现这一点(这是我目前正在学习的Haskell领域,这就是为什么它可能不是此功能的最有效实现) 如果我在len[1,2,3]上运行它 我得到一个错误: • Couldn't match expected type ‘List a0’ with actual type ‘[Int

有人能解释一下我如何修复我的程序吗。 Haskell是个新手,他一直在尝试创建一个
length
函数来计算任何类型列表的长度

我的目标是使用
数据来实现这一点,因为我想创建一个全新的类型来实现这一点(这是我目前正在学习的Haskell领域,这就是为什么它可能不是此功能的最有效实现)

如果我在
len[1,2,3]上运行它
我得到一个错误:

 • Couldn't match expected type ‘List a0’
                  with actual type ‘[Integer]’
    • In the first argument of ‘len’, namely ‘[1, 2, 3]’
      In the expression: len [1, 2, 3]
      In an equation for ‘it’: it = len [1, 2, 3]

函数定义是正确的,但
[1,2,3]
不是
列表a
对象,它是
[a]
(或更规范的
[]a
)。类似于
[1,2,3]
as
list Int
的列表是:

len (Cons 1 (Cons 2 (Cons 3 Nil)))
然后我们可以使用
重载列表
扩展名:

{-# LANGUAGE TypeFamilies #-}

import GHC.Exts(IsList(Item, fromList, toList))

instance IsList (List a) where
    type Item (List a) = a
    fromList = foldr Cons Nil
    toList Nil = []
    toList (Cons x xs) = x : toList xs
$ ghci -XOverloadedLists -XTypeFamilies
GHCi, version 8.0.2: http://www.haskell.org/ghc/  :? for help
Loaded GHCi configuration from /home/kommusoft/.ghci
Prelude> data List a = Nil | Cons a (List a)
Prelude> import GHC.Exts(IsList(Item, fromList, toList))
Prelude GHC.Exts> :{
Prelude GHC.Exts| instance IsList (List a) where
Prelude GHC.Exts|     type Item (List a) = a
Prelude GHC.Exts|     fromList = foldr Cons Nil
Prelude GHC.Exts|     toList Nil = []
Prelude GHC.Exts|     toList (Cons x xs) = x : toList xs
Prelude GHC.Exts| :}
Prelude GHC.Exts> :{
Prelude GHC.Exts| len :: List a -> Int
Prelude GHC.Exts| len Nil         = 0
Prelude GHC.Exts| len (Cons _ xs) = 1 + len xs
Prelude GHC.Exts| :}
Prelude GHC.Exts> len [1,2,3]
3

函数定义是正确的,但
[1,2,3]
不是
列表a
对象,它是
[a]
(或更规范的
[]a
)。类似于
[1,2,3]
as
list Int
的列表是:

len (Cons 1 (Cons 2 (Cons 3 Nil)))
然后我们可以使用
重载列表
扩展名:

{-# LANGUAGE TypeFamilies #-}

import GHC.Exts(IsList(Item, fromList, toList))

instance IsList (List a) where
    type Item (List a) = a
    fromList = foldr Cons Nil
    toList Nil = []
    toList (Cons x xs) = x : toList xs
$ ghci -XOverloadedLists -XTypeFamilies
GHCi, version 8.0.2: http://www.haskell.org/ghc/  :? for help
Loaded GHCi configuration from /home/kommusoft/.ghci
Prelude> data List a = Nil | Cons a (List a)
Prelude> import GHC.Exts(IsList(Item, fromList, toList))
Prelude GHC.Exts> :{
Prelude GHC.Exts| instance IsList (List a) where
Prelude GHC.Exts|     type Item (List a) = a
Prelude GHC.Exts|     fromList = foldr Cons Nil
Prelude GHC.Exts|     toList Nil = []
Prelude GHC.Exts|     toList (Cons x xs) = x : toList xs
Prelude GHC.Exts| :}
Prelude GHC.Exts> :{
Prelude GHC.Exts| len :: List a -> Int
Prelude GHC.Exts| len Nil         = 0
Prelude GHC.Exts| len (Cons _ xs) = 1 + len xs
Prelude GHC.Exts| :}
Prelude GHC.Exts> len [1,2,3]
3

[1,2,3]
不是
列表
[1,2,3]
不是
列表
。是的,这很有意义。谢谢你的帮助是的,有道理。谢谢你的帮助