Good features and methods for working with haskell tuples?

Recently, I have worked a lot with tuples and lists of tuples, and I was wondering how much I can be reasonable.

Things feel awkward and awkward, which means to me that I'm doing something wrong.

For example, I wrote three convenient functions for getting the first, second, and third values ​​in a tuple of 3 values.

Is there a better way that I am missing?

Are there more general functions that allow you to compose and process tuple data?

Here are some things I'm trying to do, this feeling should be generalized.

Extracting values: I need to create a version of fst, snd, etc. For tuples of size two, three, four and five, etc ??

fst3(x,_,_) = x
fst4(x,_,_,_) = x

Manipulating values: Can you increase the last value of each tuple in the list of pairs, and then use the same function to increase the last value of each tuple in the list of triples?

Replacing and unpacking values: There are zip and zip3. Do I also need zip4? or is there some way to create a generic zip function?

Sorry if this seems subjective, I honestly don't know if this is possible, or if I spend my time writing 3 extra features every time I need a general solution.

Thanks for any help you can give!

+5
source share
7 answers

, Haskell , , ,

data LatticeOperations a = LO { bot :: a
                              , top :: a
                              , glb :: a
                              , lub :: a
                              , le  :: a
                              }

5-, , .

. , , , , ,

lattice { le = le', glb = glb', lub = lub' }

, , , -

data FatRecord = FR { count :: Int, ... }

fat = fat { count = count fat + 1 }

, zip unzip.

+7

, fstN . ?

Zipping Unzipping

Data.List zip7. zipN ZipList.

Haskell?.

. , , :

{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, FunctionalDependencies #-}

class Firstable a b c | a -> b, a -> c where
    firstOf :: a -> b
    restOf :: a -> c
    concatenate :: b -> c -> a

instance Firstable [a] a [a] where
    firstOf = head
    restOf = tail
    concatenate = (:)

instance Firstable (a,b) a b where
    firstOf = fst
    restOf = snd
    concatenate = (,)

instance Firstable (a,b,c) a (b,c) where
    firstOf (x,_,_) = x
    restOf (_,x,y) = (x,y)
    concatenate x (y,z) = (x,y,z)

instance Firstable (a,b,c,d) a (b,c,d) where
    firstOf (x,_,_,_) = x
    restOf (_,x,y,z) = (x,y,z)
    concatenate x (y,z,w) = (x,y,z,w)

instance Firstable (a,b,c,d,e) a (b,c,d,e) where
    firstOf (x,_,_,_,_) = x
    restOf (_,x,y,z,w) = (x,y,z,w)
    concatenate x (y,z,w,t) = (x,y,z,w,t)

incFirst :: (Num b, Firstable a b c) => a -> a
incFirst x = (1 + firstOf x) `concatenate` restOf x

main = do
    print $ map incFirst [(1,2),(3,4),(5,6)]
    print $ map incFirst [(1,3,6,7),(2,5,-2,4)]

(lastOf .)

?

+8

, 3 , / , .

+4

, Template Haskell. , haskell: , fst .. , zip.

+2

, {- # LANGUAGE RecordWildCards # -} - : R = R {a:: Int, b:: String, c:: Bool}

testA R {..} = a > 0

 .. = R {..}, a = 7; b = "x"; c = True

GHC!

+1

, : Tuple.

+1

Haskell fst snd , , . , , . , , , increment . @KennyTM zipN.

Haskell , .

0

All Articles