Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

list - Haskell type with tuples

This question is focused specifically on list of tuples.

I was using sort to sort a list of tuples; at first I thought ghci would raise an error or something, but instead I received a sorted list based on the first element of my tuple!

Prelude Data.List> :t sort
sort :: Ord a => [a] -> [a]
Prelude Data.List> sort [3,1,2]
[1,2,3]
Prelude Data.List> sort [(3,'c'), (1,'a'),(2,'b')]
[(1,'a'),(2,'b'),(3,'c')]
Prelude Data.List> sort [(3,'c',1), (1,'a',2),(2,'b',3)]
[(1,'a',2),(2,'b',3),(3,'c',1)]

The same happens to functions with similar behaviors like minimum but not to those like any. So I guess this is a syntax sugar (Haskell always has some syntaxes that I have no idea about), but I'm not sure if this applies to other (Foldable t, Ord a) => t a types, nor if this is a more generic feature.

How does Haskell treat a tuple of type (a,b) so that f :: ([a] -> [c]) can be apply to l :: [(a, b)]? And does this way apply to other data structures or this is more like list-only?

question from:https://stackoverflow.com/questions/65849879/haskell-type-with-tuples

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Short answer

There isn't any magic here. That is because functions you have considered are sort :: Ord a => [a] -> [a] and minimum :: (Foldable t, Ord a) => t a -> a. Here you can see context Ord a, and in your case this a is a tuple (a, b). There is instance for tuple (Ord a, Ord b) => Ord (a, b). That's why you can execute (1,3) < (2,4). That's why it's working. And this functions will work with, for example, list of Maybe a.

any has type Foldable t => (a -> Bool) -> t a -> Bool, and it's can work with list of tuples. For example, like so:

any ((== 1) . fst) [(1,3), (2,4), (3,5)]

But not so:

any (== 1) [(1,3), (2,4), (3,5)]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...