Accessing a Specific Element in a Tuple

后端 未结 7 2152
南笙
南笙 2020-12-11 00:17

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. Wh

7条回答
  •  天命终不由人
    2020-12-11 01:02

    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.)

提交回复
热议问题