Finding the Smallest Solution with Condition a: minSol Function

  • Thread starter Thread starter anonymous_w
  • Start date Start date
  • Tags Tags
    Condition Function
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 2K views
anonymous_w
Messages
1
Reaction score
0
'Condition' is a function which determines whether an element of some type a satisfies some property, and returns True if only if it does. A polymorphic data type 'Condition a' is defined:

data Condition a = Con (a -> Bool)

Define a function minSol::Ord a => Condition a -> [a] -> Maybe a which takes a condition as its first argument, a list of possible solutions as its second argument, and returns the smallest solution found in the list which satisfies the condition. If no solution is found, the function should return the value Nothing.

I tried to use lambda but I'm not sure how to recursively apply it to every element in the list and I think the smallest element can be determined by (<) or min.
 
Physics news on Phys.org


Is this for a class? If so, what are you covering now, or what have you covered? There are several ways to check each element of the list. Although, since you mentioned recursion, have you tried pattern matching?

I've got a solution, but I'd like to see what you come up with first. Have you attempted any code at all? If so, post it.
 


Sorry for the double post, but are you using GHC? If so, this can be written nicely in two lines of code when you import a couple of modules from the standard library.