haskell无法构造无限类型 [英] haskell can not construct infinite type

查看:51
本文介绍了haskell无法构造无限类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Haskell的新手.我写了一个简单的代码.但这是行不通的.我收到此无法构造无限类型"错误.如何解决.

I'm new to haskell. I wrote a simple code. But it does not work. I'm getting this 'can not construct infinite type' error. How does it fix.

reverse' list
        | null list = []
        | otherwise = (reverse' (tail list)) : (head list) 

推荐答案

问题是由于使用类型为

:运算符引起的

The problem arises from your use of the : operator, which has the type

(:) :: a -> [a] -> [a]

因此它需要一个元素和一个列表,并返回一个以该元素为前缀的新列表.你在哪里

So it takes an element and a list, and returns a new list with that element prepended on. Where you have

reverse' (tail list) : head list
-- parentheses removed since they're not needed

reverse'(尾部列表)的类型为 [a] 头列表的类型为 a ,因此编译器会尝试使其变成 a〜[a] ,这显然是行不通的.相反,您可以使用 ++ 运算符并将 head list 放入列表本身:

the type of reverse' (tail list) is [a], and the type of head list is a, so the compiler tries to make it so that a ~ [a], which obviously can't work. Instead, you can use the ++ operator and put head list into a list itself:

reverse' (tail list) ++ [head list]

但是请记住,这不是一个非常有效的解决方案,因为Hassell列表是单链接列表,所以将它们串联到Haskell列表的末尾很慢.

But keep in mind that this is not a very efficient solution, concatenation onto the end of Haskell lists is slow since they're singly linked lists.

这篇关于haskell无法构造无限类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆