Haskell:a function takes 2 inputs and returns the smallest solution found in the list

  • #1
'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.
 

Answers and Replies

  • #2
34
0


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.
 
  • #3
34
0


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.
 

Related Threads on Haskell:a function takes 2 inputs and returns the smallest solution found in the list

  • Last Post
Replies
11
Views
2K
  • Last Post
Replies
1
Views
11K
Replies
16
Views
6K
Replies
1
Views
5K
  • Last Post
Replies
15
Views
1K
Replies
3
Views
697
Replies
3
Views
775
Top