这个“过滤”是否有效?功能存在吗?对于这样的函数,什么名字比较好呢?

发布于 2024-11-14 17:49:59 字数 1036 浏览 0 评论 0原文

我正在解决一个欧拉计划问题,该问题涉及具有特定属性的所有 n 位数字。似乎获取它们的最简单方法可能是获取具有该属性的所有数字的列表(这将是无限长的),然后选择具有正确位数的数字。像这样:

numsWithCoolProperty = filter hasCoolProperty [1..]

nDigitNumsWithCoolProperty n = takeWhile (< 10^n) $ dropWhile (<= 10^(n-1)) numsWithOtherCoolProperty

但现在如果我想用不同的属性做同样的事情,我会重复自己:

nDigitNumsWithOtherCoolProperty n = takeWhile (< 10^n) $ dropWhile (<= 10^(n-1)) numsWithOtherCoolProperty

所以我想要一个捕获 dropWhile/takeWhile 逻辑的函数。类似这样:

f :: (a -> Bool) -> [a] -> [a]
f pred = takeWhile pred . dropWhile (not . pred)

然后如果我有一个谓词 hasNDigits n m 如果 m 有 n 位数字则返回 true,我可以这样做:

nDigitNumsWithCoolProperty n = f (hasNDigits n) numsWithCoolProperty
nDigitNumsWithOtherCoolProperty n = f (hasNDigits n) numsWithOtherCoolProperty

无论如何,我的问题是关于函数 f 它有与 dropWhile 和 takeWhile 相同的类型:它是否已经存在于某处?如果不是的话,取什么名字比较好呢?我能想到的就是像 dropUntilTakeWhile 这样的东西,但我确信有一个更好的名字。

I'm solving a Project Euler problem which involves all the n-digit numbers with a certain property. It seems that the easiest way to get them might be to get a list of all the numbers with that property (which would be infinitely long), then select out the ones with the right number of digits. Like this:

numsWithCoolProperty = filter hasCoolProperty [1..]

nDigitNumsWithCoolProperty n = takeWhile (< 10^n) $ dropWhile (<= 10^(n-1)) numsWithOtherCoolProperty

But now if I want to do the same thing with a different property, I'll be repeating myself:

nDigitNumsWithOtherCoolProperty n = takeWhile (< 10^n) $ dropWhile (<= 10^(n-1)) numsWithOtherCoolProperty

so I want a function that captures the dropWhile/takeWhile logic. Something like:

f :: (a -> Bool) -> [a] -> [a]
f pred = takeWhile pred . dropWhile (not . pred)

and then if I have a predicate hasNDigits n m which returns true if m has n digits, I can do:

nDigitNumsWithCoolProperty n = f (hasNDigits n) numsWithCoolProperty
nDigitNumsWithOtherCoolProperty n = f (hasNDigits n) numsWithOtherCoolProperty

Anyway, my question is about the function f which has the same type as dropWhile and takeWhile: Does it already exist somewhere? If not, what would be a good name for it? All I can think of is something like dropUntilTakeWhile but I'm sure there's a better name out there.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无可置疑 2024-11-21 17:49:59

您的函数:

f pred = takeWhile pred . dropWhile (not . pred)

spanbreak 函数密切相关,如您所见:

span  :: (a -> Bool) -> [a] -> ([a], [a]) 

break :: (a -> Bool) -> [a] -> ([a], [a])       

让我们看一些示例:

> span (< 3) [1,2,3,4,1,2,3,4]
([1,2],[3,4,1,2,3,4])

> break (< 3) [1,2,3,4,1,2,3,4]
([],[1,2,3,4,1,2,3,4])

以及您的函数:

> f (< 3) [1,2,3,4,1,2,3,4]
[1,2]

现在,我们有一个与 相关的定律>spantakeWhiledropWhile

span p xs 相当于
(takeWhile p xs, dropWhile p xs)

因此我们可能会将您的函数视为 span的一部分>break 函数组。它还与词法分析有关,您可以在词法分析中收集一些与谓词匹配的标记。

因此,您可以将此函数称为 gatherlex 或类似的名称。

Your function:

f pred = takeWhile pred . dropWhile (not . pred)

Is strongly related to the span and break functions, as you can see:

span  :: (a -> Bool) -> [a] -> ([a], [a]) 

break :: (a -> Bool) -> [a] -> ([a], [a])       

Let's see some examples:

> span (< 3) [1,2,3,4,1,2,3,4]
([1,2],[3,4,1,2,3,4])

> break (< 3) [1,2,3,4,1,2,3,4]
([],[1,2,3,4,1,2,3,4])

and your function:

> f (< 3) [1,2,3,4,1,2,3,4]
[1,2]

Now, we have one law relating span to takeWhile and dropWhile,

span p xs is equivalent to
(takeWhile p xs, dropWhile p xs)

so we might consider your function part of the span and break group of functions. It is also related to lexing, where you gather some token that matches a predicate.

So, you might call this function gather or lex or something similar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文