12

我有以下 Haskell 元组:

       [("string",1,1)]

我需要提取其中的第一个元素,显然使用 'fst' 在这里不起作用,因为有 3 个组件。

最好的使用方法是什么?塞尔

4

5 回答 5

17

您可以为此键入自己的函数(我们将使用模式匹配):

fst3 :: (a, b, c) -> a
fst3 (x, _, _) = x

你像这样使用它:

fst3 ("string", 1, 1)
于 2013-03-28T14:04:05.093 回答
9

sel可以通过以下方式使用:

$ cabal install tuple
$ ghci
>>> :m +Data.Tuple.Select
>>> sel1 ("string",1,1)
"string"

它与任何其他功能一样工作map

>>> map sel1 [("One",1,0),("Two",2,0),("Three",3,0)]
["One","Two","Three"]

主要优点是它适用于更大的元组

>>> sel1 ("string",1,1,1)
"string"

以及标准元组

>>> sel1 ("string",1)
"string"

因此无需单独处理它们。


更多示例:

>>> map sel2 [("One",1,0),("Two",2,0),("Three",3,0)]
[1,2,3]
(0.06 secs, 4332272 bytes)
>>> map sel3 [("One",1,0),("Two",2,0),("Three",3,0)]
[0,0,0]
(0.01 secs, 2140016 bytes)
>>> map sel4 [("One",1,0),("Two",2,0),("Three",3,0)]

<interactive>:6:5:
.... error
于 2013-03-28T14:20:19.743 回答
5

您还可以使用 镜头包:

> import Control.Lens
> Prelude Control.Lens> view _1 (1,2)  -- Or (1,2) ^. _1
1
> Prelude Control.Lens> view _1 (1,2,3) -- Or (1,2,3) ^. _1
1
> Prelude Control.Lens> view _1 (1,2,3,4) -- Or (1,2,3,4) ^. _1
1
> Prelude Control.Lens> view _1 (1,2,3,4,5) -- Or (1,2,3,4,5) ^. _1
1

这不仅适用于第一个元素

> import Control.Lens
> Prelude Control.Lens> view _2 (1,2)  -- Or (1,2) ^. _2
2
> Prelude Control.Lens> view _3 (1,2,3) -- Or (1,2,3) ^. _3
3
> Prelude Control.Lens> view _4 (1,2,3,4) -- Or (1,2,3,4) ^. _4
4
> Prelude Control.Lens> view _5 (1,2,3,4,5) -- Or (1,2,3,4,5) ^. _5
5

我还写了一个类似问题的答案,不仅涵盖元组: https ://stackoverflow.com/a/23860744/128583

于 2013-03-28T16:22:44.037 回答
4

我只想定义一个函数

fst3 :: (a,b,c) -> a
fst3 (x,_,_) = x

这很容易理解并且没有奇怪的类型(类型可能会令人困惑sel1Sel1 a b => a -> b

或者您可以通过模式匹配提取您感兴趣的值,如[x | (x,_,_) <- myThreeTupleList.

最后,最好的解决方案是使用更结构化的数据类型!当然,字符串和两个整数具有更多含义,以某种方式对其进行编码是个好主意……

于 2013-03-28T14:08:49.893 回答
3

你可以这样做:

Prelude> let [(a,_,_)]=[("string",1,1)]
Prelude> a
"string"
于 2013-03-28T14:06:13.483 回答