Finding the Smallest Solution with Condition a: minSol Function

  • Thread starter Thread starter anonymous_w
  • Start date Start date
  • Tags Tags
    Condition Function
AI Thread Summary
The discussion centers on defining a function `minSol` that takes a polymorphic data type `Condition a` and a list of potential solutions, returning the smallest element that satisfies the condition. The `Condition a` type is defined as a function that checks if an element meets a specified property. Participants discuss the implementation challenges, particularly around using recursion and pattern matching to evaluate each list element against the condition. There is mention of using lambda functions and the comparison operators to find the minimum. One participant hints at a concise solution using GHC and suggests importing standard library modules to simplify the code. The conversation encourages sharing code snippets for further collaboration and learning.
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.
 
Technology 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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Back
Top