5

这是我从 YAHT 锻炼的解决方案:

练习 4.6 编写一个数据类型 Tuple,它可以包含一个、两个、三个或四个元素,具体取决于构造函数(也就是说,应该有四个构造函数,每个参数数量一个)。还提供函数 tuple1 到 tuple4 ,它们接受一个元组并返回该位置的值,或者如果数字有效则返回 Nothing(即,您要求元组上的 tuple4 只包含两个元素)。

当我写第一行时,我对与 C# 相比的简单性感到兴奋

    数据元组 abcd = Tuple1 a | 元组2 ab | 元组3 abc | 元组4 abcd

    -- 类 Tuplex<a,b,c,d> {
    -- Tuplex(a p1){ _p1 = p1; }
    -- Tuplex(a p1, b p2){ _p1 = p1; _p2 = p2; }
    -- Tuplex(a p1, b p2, c p3){ _p1 = p1; _p2 = p2; _p3 = p3; }
    -- Tuplex(a p1, b p2, c p3, d p4){ _p1 = p1; _p2 = p2; _p3 = p3; _p4 = p4; }
    -- 公共 Nullable<a> _p1;
    -- 公共 Nullable<b> _p2;
    -- 公共 Nullable<c> _p3;
    -- 公共 Nullable<d> _p4;
    -- }

在 C# 中,我可以毫无问题地访问任何字段,但在这里我应该编写一个“访问器函数”,对吧?这里的代码量让我很难过。

我可以在这里有更短的代码吗?

    tuple1 ∷ 元组 abcd → 可能 a
    tuple2 ∷ 元组 abcd → 也许 b
    tuple3 ∷ 元组 abcd → 可能 c
    tuple4 ∷ 元组 abcd → 可能 d
    tuple1 (Tuple1 a) = 只是一个
    tuple1 (Tuple2 ab) = 只是一个
    tuple1 (Tuple3 abc) = 只是一个
    tuple1 (Tuple4 abcd) = 只是一个
    tuple2 (Tuple1 a) = 无
    tuple2 (Tuple2 ab) = 只是 b
    tuple2 (Tuple3 abc) = 只是 b
    tuple2 (Tuple4 abcd) = 只是 b
    tuple3 (Tuple1 a) = 没有
    tuple3 (Tuple2 ab) = 没有
    tuple3 (Tuple3 abc) = 只是 c
    tuple3 (Tuple4 abcd) = 只是 c
    tuple4 (Tuple1 a) = 无
    tuple4 (Tuple2 ab) = 没有
    tuple4 (Tuple3 abc) = 没有
    tuple4 (Tuple4 abcd) = Just d

    -- 单元测试
    prop_tx1 = tuple1 (Tuple1 4) ≡ 仅 4
    prop_tx2 = tuple1 (Tuple2 4 'q') ≡ 只有 4
    prop_tx3 = tuple2 (Tuple1 4) ≡ (Nothing ∷ Maybe Char)
    prop_tx4 = tuple2 (Tuple2 4 'q') ≡ Just 'q'

4

4 回答 4

7

这是一种方法:集中您的模式匹配。

unTuplex f1 f2 f3 f4 t = case t of
    Tuple1 a       -> f1 a
    Tuple2 a b     -> f2 a b
    Tuple3 a b c   -> f3 a b c
    Tuple4 a b c d -> f4 a b c d

tuple1 = unTuplex (\a -> Just a ) (\a _ -> Just a ) (\a _ _ -> Just a ) (\a _ _ _ -> Just a)
tuple2 = unTuplex (\_ -> Nothing) (\_ b -> Just b ) (\_ b _ -> Just b ) (\_ b _ _ -> Just b)
tuple3 = unTuplex (\_ -> Nothing) (\_ _ -> Nothing) (\_ _ c -> Just c ) (\_ _ c _ -> Just c)
tuple4 = unTuplex (\_ -> Nothing) (\_ _ -> Nothing) (\_ _ _ -> Nothing) (\_ _ _ d -> Just d)

或者,您可以显式表达嵌套结构:

{-# LANGUAGE NoMonomorphismRestriction #-}
data DONE = DONE -- could just use (), but this is a pretty descriptive name
type Tuplex a b c d = Maybe (a, Maybe (b, Maybe (c, Maybe (d, DONE))))

tuple1 x = x >>= return . fst -- or tuple1 = fmap fst
tuple2 x = x >>= tuple1 . snd
tuple3 x = x >>= tuple2 . snd
tuple4 x = x >>= tuple3 . snd

然后tuple1(除其他外)具有类型Tuplex a b c d -> Maybe a,并且在tuple4其上具有(再次,除其他外)类型Tuplex a b c d -> Maybe d

编辑:...实际上,这表明第一种方法的替代延续。

import Control.Monad

decrement :: Tuplex a b c d -> Maybe (Tuplex b c d t)
decrement (Tuple1 a) = Nothing
decrement (Tuple2 a b) = Just (Tuple1 b)
decrement (Tuple3 a b c) = Just (Tuple2 b c)
decrement (Tuple4 a b c d) = Just (Tuple3 b c d)

zero :: Tuplex a b c d -> a
zero (Tuple1 a) = a
zero (Tuple2 a b) = a
zero (Tuple3 a b c) = a
zero (Tuple4 a b c d) = a

tuple1 = Just . zero
tuple2 = decrement >=> tuple1
tuple3 = decrement >=> tuple2
tuple4 = decrement >=> tuple3
于 2012-08-28T13:18:52.580 回答
7

我会尽量保持简单:

data Tuplex a b c d = Tuple1 a | Tuple2 a b | Tuple3 a b c | Tuple4 a b c d

toMaybes (Tuple1 p)       = (Just p, Nothing, Nothing, Nothing)
toMaybes (Tuple2 p q)     = (Just p, Just  q, Nothing, Nothing)
toMaybes (Tuple3 p q r)   = (Just p, Just  q, Just  r, Nothing)
toMaybes (Tuple4 p q r s) = (Just p, Just  q, Just  r, Just  s)

tuple1 t = p where (p,_,_,_) = toMaybes t 
tuple2 t = q where (_,q,_,_) = toMaybes t 
tuple3 t = r where (_,_,r,_) = toMaybes t 
tuple4 t = s where (_,_,_,s) = toMaybes t
于 2012-08-28T20:52:49.423 回答
1

只需给您的元组字段名称!

data Tuplex a b c d = Tuple1 { tuple1 :: a }
                    | Tuple2 { tuple1 :: a
                             , tuple2 :: b }
                    | Tuple3 { tuple1 :: a
                             , tuple2 :: b
                             , tuple3 :: c }
                    | Tuple4 { tuple1 :: a
                             , tuple2 :: b
                             , tuple3 :: c
                             , tuple4 :: d }

因此,您具有以下类型的功能:

tuple1 :: Tuplex a b c d -> a
tuple2 :: Tuplex a b c d -> b
-- etc

在 Haskell 中使用这样的记录字段名称实际上不如您在 Haskell 中预期的那么常见,因为模式匹配很容易,并且至少在某些圈子中,RecordWildCard扩展的受欢迎程度允许您执行以下操作:

function (Tuples3 {..}) =
-- now you have variables tuple1 :: a, tuple2 :: b, etc.

(使用记录通配符时,最好将元组字段命名为更简单的名称,例如 tupA、tupB、tupC、tupD)

于 2012-08-28T05:14:21.110 回答
1
import Safe (atMay) -- from the 'safe' package

toList (Tuple1 a) = [a]
toList (Tuple2 a b) = [a, b]
toList (Tuple3 a b c) = [a, b, c]
toList (Tuple4 a b c d) = [a, b, c, d]

tuple n t = atMay (toList t) n

[tuple1, tuple2, tuple3, tuple4] = map tuple [1..4]

编辑:Vitus 正确地指出这仅适用于同质元组,因此这不是正确答案。在那种情况下,我会听从丹尼尔的回答。

于 2012-08-28T16:05:44.937 回答