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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top