Restart Haskell Min Class

I am new to Haskell and I am writing a function similar to min, instead of just accepting the 2 values ​​that it will take. I designed the class as shown below:

min3 :: a -> a -> a -> a 

However, what would I put with the function itself after recording the number of input variables?

 min3 xyz = 

This is probably stupidly easy, and I missed something, but if you could help me, I would really appreciate it.

Thanks!

-1
source share
3 answers

One implementation

 min3 :: Ord a => a -> a -> a -> a min3 xyz = min x $ min yz 

Notes:

  • In the type definition, you should write Ord a => ... because your arguments must be ordable, i.e. the min function must be defined for them.

@Lee provides a point implementation that can also be written as

import Data.Composition ((. :))

 min3 :: Ord a => a -> a -> a -> a min3 = min .: min 

Note: f .: g is a shortcut for (f .) . g (f .) . g , which is defined in Data.Composition

+4
source

You can also write it as:

 min3 :: Ord a => a -> a -> a -> a min3 = ((min .) .) min 
+5
source

Another implementation:

 min3 xyz = head $ sort [x,y,z] 

And this can be easily generated by a function that can find the minimum in the list:

 minimum = head . sort 

Data.List gives another implementation of minimum .

+1
source

All Articles