Haskell比较所有列表项

Haskell比较所有列表项

本文介绍了Haskell比较所有列表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:很难描述我想要做什么,但是这里有一个尝试(来自评论):



我正在构建一个wordfeud求解器,所以我有一个字,还有一些字母(都是char列表)。我应用了这个()到两个列表中以获取所有字母的频率。我现在所做的是遍历'word'char列表,​​并检查所有字符是否在'letters'字符列表中充分发生。



我写了一个Haskell函数通过对两个列表的项目应用函数并比较结果来比较两个列表。



比较过程如下所示:

  hasLetters':: [Char]  - > [Char]  - > Bool 
hasLetters'word letters =(getCharOccurrence(head word)(getCharOccurrences word))

这只会比较单词第一个字母的出现次数。但所有的单词都应该进行比较(并且结果对于所有单词都应为TRUE)。



我真的不知道如何完成这个任务。我找到了可以让我定义一个谓词的'all'方法,这非常好。它看起来像这样:

  all(< =(getCharOccurrence(head word)(getCharOccurrences letters)))

(我认为这是正确的)
它确保进入列表的每个项目都小于或等于所提供的函数的结果。



但是:'all'方法需要另一个参数。这将是定义应该与谓词进行比较的源参数。这只是一个列表,这很容易,那么我会做这样的事情:

  all(< =(getCharOccurrence (head word)(getCharOccurrences letters)))[0..10] 

但问题是:我没有这样的结果列表,我需要比较它的结果:

 (getCharOccurrence(head word) (getCharOccurrences letters))

我想我可以将这个函数应用于'word'中的每个字符char列表与'地图'功能,但我不知道如何使用它。我开始像这样:
$ b $ pre $ map(getCharOccurrence(head word)(getCharOccurrences word))word



但是那是错误的。

所以我(想我)需要:apply上面的函数给'word'char列表中的所有字符,并将它与谓词进行比较。

但是,也许我只是在想错误的方式。我是绝对的Haskell /函数式编程新手。请帮助我: - )

解决方案

所以从我的理解你有一个字符串 code>与你想要形成的单词以及一个字符字母的列表,代表你可以使用的方块。你想检查这个单词是否可以由瓷砖组成。



这里我假设你提到的函数有类型$ / $>

  getCharOccurrence :: Char  - > [(Char,Integer)]  - >整数
getCharOccurrences :: [Char] - > [(Char,Integer)]

首先,您需要修改 hasLetters' 取一个Char参数,而不是使用头字

  hasLetters':: Char  - > [Char]  - > [Char]  - > Bool 
hasLetters'c word letters =(getCharOccurrence c(getCharOccurrences word))< =(getCharOccurrence c(getCharOccurrences letters))

然后你可以把上面的代码与一个主函数(让我们称之为 sufficientTiles )和

  sufficientTiles :: [Char]  - > [Char]  - > Bool 
sufficientTiles word letters = and $ map(\c - > hasLetters'c word letters)word

我们在这里完成的是将 hasLetter'函数映射到 word 的每个字符。这会给我们一个布尔名单。然后,我们使用来检查列表中的所有元素是 True


Edit: it's hard to describe what I'm trying to do, but here's a try (from the comments):

I am building a wordfeud solver, so I have a word, and some letters (both char list). I applied this ( How to find the frequency of characters in a string in Haskell? ) to both lists to get the frequency of all letters. What I'm doing now is iterating though the 'word' char list, and checking if all chars occur sufficiently in the 'letters' char list.

I have written a Haskell function that compares two lists by applying a function to the items of both lists, and comparing the results.

The comparison is done like this:

hasLetters' :: [Char] -> [Char] -> Bool
hasLetters' word letters = (getCharOccurrence (head word) (getCharOccurrences word)) <= (getCharOccurrence (head word) (getCharOccurrences letters))

This only compares the occurrences of the first letter of the word. But ALL words should be compared (and the result should be TRUE for all of them).

I don't really know how to accomplish this. I found the 'all' method that lets me define a predicate, that's pretty good. It looks like this:

all (<= (getCharOccurrence (head word) (getCharOccurrences letters)))

(I think that's correct)It makes sure that every item that goes into the list is smaller than or equal to the result of the provided function.

BUT: the 'all' method needs another parameter. This would be the 'source' parameter that defines what should be compared to the predicate. This would be easy when this were just a list, then I would do something like this:

all (<= (getCharOccurrence (head word) (getCharOccurrences letters))) [0..10]

But the problem is: I dont have a list of results like this, I need to compare it to the result of:

(getCharOccurrence (head word) (getCharOccurrences letters))

I figured that I could apply this function to every character in the 'word' char list with the 'map' function, but I dont know how to use it. I started like this:

map (getCharOccurrence (head word) (getCharOccurrences word)) word

But that's wrong.

So what I (think I) need: apply the above function to all characters of the 'word' char list, and compare it to the predicate.

But maybe I'm just think the wrong way. I'm an absolute Haskell/functional programming newbie. Please help me out :-)

解决方案

So from what I understand you have a string word with the word you would like to form and a list of chars letters representing the tiles at your disposal. You want to check whether the word may be formed by the tiles or not.

Here I'm assuming the functions you've mentioned have the types

getCharOccurrence :: Char -> [(Char, Integer)] -> Integer
getCharOccurrences :: [Char] -> [(Char, Integer)]

First, you need to modify hasLetters' to take a Char parameter instead of using head word:

hasLetters' :: Char -> [Char] -> [Char] -> Bool
hasLetters' c word letters = (getCharOccurrence c (getCharOccurrences word)) <= (getCharOccurrence c (getCharOccurrences letters))

Then you can combine the above to a master function (let's call it sufficientTiles) with

sufficientTiles :: [Char] -> [Char] -> Bool
sufficientTiles word letters = and $ map (\c -> hasLetters' c word letters) word

What we've done here is to map the hasLetter' function to each character of word. This will give us a list of Bools. We then use and to check that all elements of that list are True.

这篇关于Haskell比较所有列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 18:41