Return the smallest even number of 3 arguments, or the largest uneven number if there are no even numbers

I have a semi-public Haskell homework and need help on how to solve it. A task:

Write a Haskell function

evenmin a b c

      

which returns the smallest even number of the arguments, or the largest uneven if there is no even number in the arguments.

I know I can do this with many guards, but I'm sure there is a much better way! Please don't write out the solution, but if you can, nudge me in the right direction. Thank!

+3


source to share


2 answers


Hint: Instead of 3 arguments, let's say your input is a non-empty list of integers, i.e.

evenmin' :: [Int] -> Int

      

Suppose you have a function phi

that splits the input like this:



phi [1, 2, 3, 4, 5, 6] == ([1,3,5],[2,4,6])

      

What will be the definition evenmin'

? Then define evenmin a b c = evenmin' [a, b, c]

.

+4


source


Ordering integers like this:

  • even integers are ordered <=

  • odd integers are ordered by >=

  • even whole numbers are always less than odd ones.

Define myCompare :: Int -> Int -> Ordering

.

Understand that you want the minimum in the order above.



How do I calculate at least two wrt objects common order?

How do I expand this to three objects?

Bonus: how to expand this to lists?

+2


source







All Articles