简体   繁体   English

查找列表和谓词的任何可能的绑定

[英]Find any possible bondings of a list and predicate

I am really new to Haskell and I have almost no experience with this language.我对 Haskell 真的很陌生,我几乎没有使用这种语言的经验。 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.此外,我认为对这个问题有用的函数是以下findBonding :: Eq a => (a -> a -> Bool) -> [a] -> Maybe [(a,a)]这样findBonding p ls将 p 作为谓词,将 ls 作为整数列表。

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)]例如, findBonding (\\x -> \\y -> odd(x+y)) [2,3,4,5,6,7]应该返回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.ls (列表)的foldrp (谓词)声明 findBonding 是一个函数,该函数应该声明要查找哪些对,然后循环每两个项目以找到正确的对并将它们作为列表列表返回。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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