英文:
Haskell Type Problem with the ":" operator
问题
-
[]:[[[1]],[]]
-
[[],[1]]:[[]]
The problem is that GHCI gives out:
-
[[],[[1]],[]]
-
[[[],[1]],[]]
Now that doesn't make any sense to me, because I combine lists which do not contain just one kind of element like in 1, or combine two lists with the same type. I need in the front of ":" a non-list item like I wrote down in the top of this question, so why does this work?
英文:
I have to learn Haskell and one exercise was to say If a piece of Code would Work for the Compiler.
The ":" works as a -> [a] -> [a] with a some variable type
My Problem is now the following:
1. []:[[[1]],[]]
And
2. [[],[1]]:[[]]
The Probleme is that GHCI gives out:
1. [[],[[1]],[]]
And
2.[[[],[1]],[]]
Now that doesn't make any Sense to me ,because i combine Lists which dies Not cotaim Just one Kind of Element Like in 1. or in combine two Lists with the Same Type while i need in the Front of ":" a non list Item Like I wrote down in the top of this Question, so why does this Work?
答案1
得分: 3
非空括号只是对(:)
链的语法糖。
有两种创建列表的方法。要创建一个空列表,您可以使用[]
。要从现有对象列表y
和与y
元素相同类型的另一个值x
创建列表,您可以使用x:y
。
因此,括号的任何其他用法都可以转化为空括号和(:)
的组合。一些简单的例子:
[a] == a:[]
[a,b] == a:b:[]
[a,b,c] == a:b:c:[]
在上述每种情况下,如果a, b, c :: t
,那么[] : [t]
。
在您的情况下,您有列表的列表,所以开始变得有点混乱,因为无论类型变得多么“嵌套”,[]
仍然是空列表。所以对于#1,[]:[[[1]],[]]
,您有:
[]
:类型为Num a => [[a]]
的空列表[[[1]],[]]
:解糖为[[1]]:[]:[]
的列表,由两个类型为Num a => [[a]]
的列表组成,即[[1]]
和[]
。
因此,表达式的结果实际上是[[],[[1]],[]]
或[]:[[1]]:[]:[]
。
还有一个令人困惑的地方:在完全解糖的表达式[]:[[1]]:[]:[]
中,前两个[]
的类型与[[1]]
一样,都是Num a => [[a]]
。然而,第三个,作为最后一个(:)
构造函数的右操作数,具有类型Num a => [[[a]]]
。
英文:
Non-empty brackets are just syntactic sugar for chains of (:)
.
There are two ways to create a list. To create an empty list, you use []
. To create a list from an existing list of objects y
and another value x
of the same type as the elements of y
, you use x:y
.
So any other use of brackets can be desugared to a combination of empty brackets and (:)
. Some simple examples:
[a] == a:[]
[a,b] == a:b:[]
[a,b,c] == a:b:c:[]
In each of the above cases, if a, b, c :: t
, then [] : [t]
.
In your case, you have lists of lists, so it starts getting a little confusing, because no matter how "nested" the type gets, []
still the empty list. So with #1, []:[[[1]],[]]
, you have:
[]
: the empty list of typeNum a => [[a]]
[[[1]],[]]
: a list that desugars to[[1]]:[]:[]
, consisting of two lists of typeNum a => [[a]]
, namely[[1]]
and[]
.
The result of the expression, then, is in fact [[],[[1]],[]]
or []:[[1]]:[]:[]
.
One other confusing point: in the completely desugared expression []:[[1]]:[]:[]
, the first two occurrences of []
have type Num a => [[a]]
, just like [[1]]
. The third one, though, being the right-hand operand of the last (:)
constructor, has type Num a => [[[a]]]
.
答案2
得分: 0
以下是翻译好的部分:
也许类型提示将有助于说明发生了什么。
对于问题1:
([] :: [[Integer]])
:
[([[1]] :: [[Integer]])
,([] :: [[Integer]])
]
对于问题2:
([([] :: [Integer])
,([1] :: [Integer])
] :: [[Integer]]
)
:
([[]] :: [[[Integer]]])
你可以将这些示例粘贴到 REPL 中,以查看它们能够正常工作。
空列表的类型是 forall a. [a]
,其中 a
可以是任何类型,包括 [[Integer]]
,这就是为什么这些示例能够正常工作的原因。
英文:
Perhaps type hints will help illustrate what's going on.
For problem 1:
([] :: [[Integer]])
:
[ ([[1]] :: [[Integer]])
, ([] :: [[Integer]])
]
For problem 2:
([ ([] :: [Integer])
, ([1] :: [Integer])
] :: [[Integer]]
)
:
([[]] :: [[[Integer]]])
You should be able to paste each of those examples into a REPL to see that they work.
An empty List is of type forall a. [a]
where a
can be anything, including [[Integer]]
, which is why these examples work.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论