简体   繁体   中英

Find any possible bondings of a list and predicate

I am really new to Haskell and I have almost no experience with this language. My problem is that I want to create a function that finds any possible bondings of a given list and a predicate but I can't find the right solution.

Furthermore, the function that I thought it will be useful for this problem is the following findBonding :: Eq a => (a -> a -> Bool) -> [a] -> Maybe [(a,a)] such that findBonding p ls takes p as predicate and ls as a list of integers.

For example, findBonding (\\x -> \\y -> odd(x+y)) [2,3,4,5,6,7] should return Just [(2,3),(3,2),(4,5),(5,4),(6,7),(7,6)]

Is it a good idea to declare findBonding with foldr of the ls (the list) and then p (the predicate) to be the function that should declare which pairs to find and then to loop over every two items to find the correct pairs and to return them as a list of lists.

Please don't hate me since I am really new to this language and I have absolutely no experience but I am eager to understand it although it is a really hard one to get going with.

You're not clear on what a "bonding" means, but based on your comments, I think this might work for your definition:

import Data.Foldable
import Data.Traversable

findBonding :: (a -> a -> Bool) -> [a] -> Maybe [(a,a)]
findBonding f xs = for xs $ \x -> (,) x <$> find (f x) xs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM