Haskell - 访问元组中的特定元素 [英] Haskell - Accessing a Specific Element in a Tuple

查看:120
本文介绍了Haskell - 访问元组中的特定元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Haskell-newbie reporting in。
问题如下:
在Haskell中,我们有 fst snd 返回2元组的第一个和第二个元素。为什么我们没有简单的方法来访问任何元组中的第i个元素?现在我有一个三元组,我想读第一个元素,完成这个任务的唯一方法是做模式匹配的诡计。为什么做不到这一点更容易?或者也许有一些简单的方法?

解决方案

什么阻止语言具有您想要的特殊结构是它的设计。设计师并没有把它放在这里,因为它会使语言定义复杂化,这很简单。 fst snd 是常见大对数对的库函数;您可以自己定义所有其他人,或者更好地定义数据的记录类型,以便数据成员具有适当的名称。有一个扩展来做到这一点,但我没有遇到过;请检查文档或询问邮件列表以确定。)


Haskell-newbie reporting in. Question is as follows: In Haskell, we have fst and snd that return the first and the second elements of a 2-tuple. Why don't we have an easy way of accessing the i-th element from any tuple? Right now I have a 3-tuple, I want to read the 1st element and the only way of accomplishing this task is doing pattern-matching trickery. Why can't this be done easier? Or maybe there is some easy way?

解决方案

What prevents the language from having the special construct you want is its design. The designers just didn't put this in, because it would complicate the language definition, which is quite minimalistic. fst and snd are library functions for the common case of pairs; you can define all the others yourself, or better, define record types for your data so that your data members have appropriate names.

(It may be that GHC has an extension to do this, but I haven't encountered one; check the docs or ask on the mailing list to be sure.)

这篇关于Haskell - 访问元组中的特定元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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