Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why `minimum (1, 2)` returns 2 in Haskell? [duplicate]

Tags:

haskell

Why minimum (1, 2) is 2 in Haskell?

My test result in GHCi,

GHCi, version 8.2.1: http://www.haskell.org/ghc/  :? for help
Prelude> minimum (1, 2)
2

Shouldn't it return 1?

like image 325
ice1000 Avatar asked Jan 23 '26 07:01

ice1000


1 Answers

The type of minimum is

 (Ord a, Foldable t) => t a -> a

The Foldable instance for pairs refers only to the second element of the pair, which you can see with toList:

toList (1,2)
> [2]

the minimum element in that collection is 2.

like image 101
Lee Avatar answered Jan 26 '26 00:01

Lee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!