简体   繁体   English

我对 Haskell 'length' 函数的重新定义不起作用

[英]My redefinition of Haskell 'length' function won't work

Can someone please explain how I can fix my program.有人可以解释我如何修复我的程序。 Very new to Haskell, been trying to create a length function that calculates the length of a list of any type. Haskell 的新手,一直在尝试创建一个length函数来计算任何类型列表的长度。

I am aiming to do this using data as I want to create a brand new type to do so (this is the area of Haskell that I'm currently learning, which is why it might not be the most efficient implementation of this function)我的目标是使用data来做到这一点,因为我想创建一个全新的类型来做到这一点(这是我目前正在学习的 Haskell 领域,这就是为什么它可能不是这个功能最有效的实现)

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


len :: List a -> Int
len Nil         = 0
len (Cons _ xs) = 1 + len xs

If I run it on len [1,2,3] I get the error:如果我在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]

The function definition is correct, but [1,2,3] is not a List a object, it is a [a] (or more canonical [] a ).函数定义是正确的,但[1,2,3]不是List a对象,它是[a] (或更规范的[] a )。 A list like [1,2,3] as List Int is:[1,2,3]作为List Int是:

len (Cons 1 (Cons 2 (Cons 3 Nil)))

Alternatively, you can make List a an instance of the IsList type class, and then use the -XOverloadedLists extension:或者,您可以使List a IsList类型类的实例,然后使用-XOverloadedLists扩展:

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

Then we can use the OverloadedLists extension:然后我们可以使用OverloadedLists扩展:

$ 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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