Sure, here’s the translation: Haskell中使用“:”运算符的类型问题

huangapple go评论65阅读模式
英文:

Haskell Type Problem with the ":" operator

问题

  1. []:[[[1]],[]]

  2. [[],[1]]:[[]]

The problem is that GHCI gives out:

  1. [[],[[1]],[]]

  2. [[[],[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 type Num a => [[a]]
  • [[[1]],[]]: a list that desugars to [[1]]:[]:[], consisting of two lists of type Num 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.

huangapple
  • 本文由 发表于 2020年1月3日 22:13:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580049.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定