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

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

问题描述

Haskell-newbie正在报告. 问题如下: 在Haskell中,我们有fstsnd返回两个元组的第一个和第二个元素.为什么我们没有从任何元组访问第i个元素的简便方法?现在我有一个三元组,我想阅读第一个元素,完成此任务的唯一方法是进行模式匹配技巧.为什么做起来不那么容易?还是有一些简单的方法?

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?

推荐答案

阻止该语言具有所需特殊结构的是其设计.设计师只是没有输入它,因为这会使语言定义复杂化,这非常简单. fstsnd是常见的配对对的库函数;您可以自己定义其他所有对象,或者更好地为数据定义记录类型,以便您的数据成员具有适当的名称.

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.

(可能是GHC可以执行此操作的扩展名,但我还没有遇到过扩展名;请检查文档或在邮件列表中询问以确保确定.)

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

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