0

I am trying to implement the following specification:

busca.v.xs = <Min i : 0 ≤ i < #xs ∧ xs.i = v : i>

I wrote something like the following.

busca :: Int -> [Int] -> Int

busca v [] = 1000000
busca v (x:xs) | x==v = min x (busca v xs)
               | otherwise = busca v xs

When I was deriving, the case [] is infinity, so I tried making something that is somewhat similar. The expected result should be the minimun number in a list. For example, busca 2 [4,3,2,2,7] should return 2. Can i use Maybe and simulate that infinity in a way?

1
  • 4
    That specification is for getting the index of the first occurrence of the element v in the list xs, not for getting the minimum number in the list. Commented Apr 20, 2022 at 7:26

1 Answer 1

3

You could make a fresh type for which min did what you ask like this:

data PosInfty a = Finite a | Infinite deriving (Eq, Ord, Read, Show)

Derived Ord instances treat values made with earlier constructors as unconditionally smaller than values made with later constructors, so all Finite values will be less than Infinite.

This type is also available (with significantly more instances) from monoid-extras.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.