我如何从Haskell的元组中获得一个Int(How can I get an Int from the tuple in Haskell)

编程入门 行业动态 更新时间:2024-10-28 01:12:15
我如何从Haskell的元组中获得一个Int(How can I get an Int from the tuple in Haskell)

我是Haskell的新手,目前正在寻找一种方法来提取或删除元组中的第一个Int 。 有没有一个内置的Haskell函数来做到这一点? 例如:

tuple :: (Int) tuple = (1,2,3,4,5) tuple !! 0 =

I am new to Haskell, and currently looking for a way to extract or delete the first Int in a tuple. Is there a built-in Haskell function to do that? For instance:

tuple :: (Int) tuple = (1,2,3,4,5) tuple !! 0 =

最满意答案

正如在OP中定义的那样, tuple的类型是这样的:

Prelude> tuple = (1,2,3,4,5) Prelude> :t tuple tuple :: (Num t4, Num t3, Num t2, Num t1, Num t) => (t4, t3, t2, t1, t)

没有类型(Int) 。 相反,类型是(t4, t3, t2, t1, t) ,其中这五种类型中的每一个都必须是Num实例。 有多种类型可供选择,并且由于所有五种类型可以相互独立地变化,所以类型可以是(Int, Word, Float, Integer, Int) 。

原因是定义中的每个文字( 1等)都可以被解释为这些类型中的任何一种,并且当您编写这样的表达式时,编译器会让您的选项保持打开状态。

如果您希望它是仅由Int值组成的元组,您可以声明:

tuple :: (Int, Int, Int, Int, Int) tuple = (1,2,3,4,5)

如果你想要元组的第一个元素,你可以使用模式匹配来提取这些信息:

Prelude> (x, _, _, _, _) = tuple Prelude> x 1

上面的表达式使用通配符忽略元组的其他四个元素,但是如果您需要其中一个或多个元素,则也可以绑定到命名值:

Prelude> (x, _, z, _, _) = tuple Prelude> z 3

正如Thomas M. DuBuisson在对OP的评论中指出的那样,然而,与其他大型元组相比,您可能会更好地使用其他语言构造。

如果我冒险猜测,我认为OP实际上是关于列表而不是元组。 某些语言(特别是动态语言)在列表和元组之间没有很大的区别。 这在编译时类型未知的动态语言中有一定意义。

然而,Haskell是一种静态类型语言,列表和元组是不同的。

元组是具有固定长度的数据结构,但您可以在其中灵活选择每个单独元素的类型。 列表是具有固定元素类型的数据结构,但您可以在其中更改长度。

也许你正在寻找一个整数列表:

list :: [Int] list = [1,2,3,4,5]

鉴于这样一个列表,你可以,例如,使用!! 运算符提取第一个元素:

Prelude> list !! 0 1

请注意,但是,那!! 是不安全的,因为如果你给它一个超出范围的索引,它会抛出一个异常。

As defined in the OP, the type of tuple is this:

Prelude> tuple = (1,2,3,4,5) Prelude> :t tuple tuple :: (Num t4, Num t3, Num t2, Num t1, Num t) => (t4, t3, t2, t1, t)

There's no type (Int). Instead, the type is (t4, t3, t2, t1, t), where each of those five types has to be a Num instance. There's more than a single type to chose from, and since all five types can vary independently of each other, the type could be (Int, Word, Float, Integer, Int).

The reason for this is that each literal in the definition (1, 2, etc.) could be interpreted as any of those types, and when you write an expression like that, the compiler keeps your options open.

If you want it to be a tuple exclusively made up of Int values, you can declare that:

tuple :: (Int, Int, Int, Int, Int) tuple = (1,2,3,4,5)

If you want the first element of the tuple, you can use pattern matching to extract that information:

Prelude> (x, _, _, _, _) = tuple Prelude> x 1

The above expression uses wildcards to ignore the other four elements of the tuple, but if you need one or more of those, you can bind to named values as well:

Prelude> (x, _, z, _, _) = tuple Prelude> z 3

As Thomas M. DuBuisson points out in a comment to the OP, however, you'd probably be better off with other language constructs than large tuples.

If I may venture a guess, I suppose the OP is actually about lists, not tuples. Certain languages (particularly dynamic languages) don't make much of a distinction between lists and tuples. This makes some sense in a dynamic language where types are unknown at compile-time.

Haskell, however, is a statically typed language, and lists and tuples are different.

A tuple is a data structure with a fixed length, but where you can flexibly choose the type of each individual element. A list is a data structure with a fixed element type, but where you can vary the length.

Perhaps you're looking for a list of integers:

list :: [Int] list = [1,2,3,4,5]

Given such a list, you can, for instance, use the !! operator to extract the first element:

Prelude> list !! 0 1

Do note, however, that !! is unsafe, because if you give it an out-of-range index, it's going to throw an exception.

更多推荐

本文发布于:2023-08-02 23:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1382434.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组中   Haskell   tuple   Int

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!