微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 Haskell 中反转列表的无限类型错误

如何解决在 Haskell 中反转列表的无限类型错误

我正在尝试实现列表的反向:

myLast :: [a] -> a
myLast [] = error "No end for empty lists!"
myLast [x] = x
myLast (_:xs) = myLast xs

myReverse :: [a] -> [a]
myReverse (x:xs) = myLast xs + myReverse xs

但我收到此错误

/workspaces/hask_exercises/exercises/src/Lib.hs:42:32: error:
    * Occurs check: cannot construct the infinite type: a ~ [a]
    * In the second argument of `(+)',namely `myReverse xs'
      In the expression: myLast xs + myReverse xs
      In an equation for `myReverse':
          myReverse (x : xs) = myLast xs + myReverse xs
    * Relevant bindings include
        xs :: [a] (bound at src/Lib.hs:42:14)
        x :: a (bound at src/Lib.hs:42:12)
        myReverse :: [a] -> [a] (bound at src/Lib.hs:41:1)
   |
42 | myReverse (x:xs) = myLast xs + myReverse xs
   |                                ^^^^^^^^^^^^

cannot construct the infinite type: a ~ [a] 是什么意思?我经常收到这个错误,想了解它的含义。

解决方法

(+) :: Num a => a -> a -> a 函数将两个数字(相同类型)相加。例如,如果 a ~ Int,它会将两个 Int 相加,但不会将 Int[Int] 相加。

但是,即使 (+) 运算符例如将一个项目添加到列表中,它仍然无法正确反转列表:您的函数没有基本情况要为一个列表做什么空列表,并且您的递归列表对列表 x 的第一项 (x:xs) 不执行任何操作。

简单的反转方法:

myReverse :: [a] -> [a]
myReverse [] = []
myReverse (x:xs) = myReverse xs ++ [x]

但这效率不高:附加两个项目将花费左列表大小的线性时间。您可以使用累加器:每次进行递归调用时都会更新的参数。这看起来像:

myReverse :: [a] -> [a]
myReverse [] = go []
    where go ys (x:xs) = …
    where go ys [] = …

填写 部分的地方留作练习。

,

你有

myLast :: [a] -> a 
myReverse :: [a] -> [a]

myReverse (x:xs) = myLast xs    +     myReverse xs
                   \___a___/          \____[a]___/

          (x:xs)  :: [a]
          ---------------
           x      ::  a
             xs   :: [a]                        xs :: [a]               
      myLast      :: [a] -> a         myReverse    :: [a] -> [a]
     -------------------------       ----------------------------
      myLast xs   ::        a         myReverse xs ::        [a]

myReverse (x:xs)                                   ::        [a]

但是

> :t (+)
                               (+) :: Num a =>  a  ->  a  ->  a

表示+左边的事物的类型和右边的事物的类型必须相同。

但它们不能是:正如我们刚刚在上面看到的,在您的代码中,第一个(myLast xs 的)是某种类型的 a,第二个(myReverse xs 的)是[a] 那些相同 a列表

这两个不能相同,因为这意味着

              a  ~  [a]             OK,this is given to us,then
                     a  ~ [a]       we can use it,so that
                 --------------
              a  ~ [[a]]            this must hold;
                     a  ~ [a]       we know this,then
                 --------------
             a  ~ [[[a]]]           this must also hold; and
                     a  ~ [a]       ........
                 --------------     ........
             a ~ [[[[a]]]]          ........
          .........................

以此类推,从而使 a 成为“无限”类型。因此出现错误。

您可以通过将 + 替换为

来修复它
                              (+++) ::         a  ->  [a]  ->  [a]

并实施它来做你需要它做的事情。

您还需要修复您的逐个错误,从而完全忽略接收到的输入中的第一个元素 x

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。