在 Haskell 中过滤斐波那契数列

发布于 2024-11-04 19:12:54 字数 1099 浏览 5 评论 0原文

我正在尝试过滤包含斐波那契数字的列表。

我需要的只是奇数,并且小于或等于N

这就是我到目前为止所拥有的:

fib   n | n == 0         = 0
        | n == 1         = 1
        | otherwise = fib (n-1) + fib (n-2)

fibs n = [a | a <- [fib x | x <- [1..]], odd a, a < n]

这会给我想要的东西,但同时该解决方案不起作用,因为我不知道如何停止fib 函数检索元素。当然,这是因为x <- [1..]

我考虑过两种选择:

  1. x <- [1..] 中设置限制(取决于 n
  2. 定义 fibs 递归这样我就可以知道何时停止(在写问题时思考)

我该怎么做?

我不是在寻找一种有效的方法

编辑:
这是我最后得到的两个解决方案:

fib   n | n == 0         = 0
        | n == 1         = 1
        | otherwise = fib (n-1) + fib (n-2)

fibsAux n k xs  | a < n     = fibsAux n (k+1) (xs ++ [a])
                | otherwise = xs
                where 
                    a = fib k
fibs n = filter odd $ fibsAux n 0 []

一个是使用@hammar的建议:

fibs x = takeWhile (< x) [a | a <- [fib x | x <- [1..]], odd n]

I'm trying to filter a list that contains the Fibonacci numbers.

What I need is only odd numbers, and less or equal than N.

This is what I have so far:

fib   n | n == 0         = 0
        | n == 1         = 1
        | otherwise = fib (n-1) + fib (n-2)

fibs n = [a | a <- [fib x | x <- [1..]], odd a, a < n]

That will give me what I want, but at the same time that solution won't work because I don't know how to stop retrieving elements from fib function. Of course, that is because of x <- [1..].

I have thought about two options:

  1. Placing a limit (that depends on n) in x <- [1..]
  2. Defining fibs recursive so I can know when to stop (thought about it while writing the question)

How could I do this?

I'm not looking for an efficient way

Edit:
Here are the two solutions I had at the end:

fib   n | n == 0         = 0
        | n == 1         = 1
        | otherwise = fib (n-1) + fib (n-2)

fibsAux n k xs  | a < n     = fibsAux n (k+1) (xs ++ [a])
                | otherwise = xs
                where 
                    a = fib k
fibs n = filter odd $ fibsAux n 0 []

and the one using the suggestion of @hammar:

fibs x = takeWhile (< x) [a | a <- [fib x | x <- [1..]], odd n]

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

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

发布评论

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

评论(2

乙白 2024-11-11 19:12:54

看看 Data.List 中的 takeWhile 函数(并由 Prelude 重新导出)。例如,

takeWhile (< 4) [1..] == [1, 2, 3]

请注意,即使列表是无限的,一旦找到不满足谓词的元素,该列表就会终止。

Have a look at the takeWhile function from Data.List (and re-exported by the Prelude). For example,

takeWhile (< 4) [1..] == [1, 2, 3]

Note that even though the list is infinite, this terminates once it finds an element that does not satisfy the predicate.

相守太难 2024-11-11 19:12:54

还有一种更有效的方法来计算斐波那契数:

fibs = 0 : 1 : zipWith (+) fibs (tail fibs)

可以对其进行过滤以仅给出奇数:

oddFibs = filter odd fibs

可以将其截断为小于或等于 N:

oddFibsLeN n = takeWhile (<= n) oddFibs

There's also a more efficient way to compute Fibonacci numbers:

fibs = 0 : 1 : zipWith (+) fibs (tail fibs)

Which can be filtered to give only the odd numbers:

oddFibs = filter odd fibs

Which can be truncated to less than or equal to N:

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