ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Lazy Fibonacci Sequence Generation

Haskell

Goal -- WPM

Ready
Exercise Algorithm Area
1module LazyFibonacci where
2
3-- | Generates an infinite lazy list of Fibonacci numbers.
4-- The sequence starts with 0, 1, 1, 2, 3, 5, ...
5fibonacciSequence :: [Integer]
6fibonacciSequence = 0 : 1 : zipWith (+) fibonacciSequence (tail fibonacciSequence)
7
8-- | Takes the first n elements from the Fibonacci sequence.
9-- Handles cases where n is non-positive.
10takeFibonacci :: Int -> [Integer]
11takeFibonacci n
12| n <= 0 = []
13| otherwise = take n fibonacciSequence
14
15-- | Main function to demonstrate the lazy Fibonacci sequence.
16main :: IO ()
17main = do
18putStrLn "First 10 Fibonacci numbers:"
19print $ takeFibonacci 10
20
21putStrLn "First 5 Fibonacci numbers:"
22print $ takeFibonacci 5
23
24putStrLn "First 0 Fibonacci numbers:"
25print $ takeFibonacci 0
26
27putStrLn "First 1 Fibonacci number:"
28print $ takeFibonacci 1
29
30putStrLn "First 2 Fibonacci numbers:"
31print $ takeFibonacci 2
Algorithm description viewbox

Lazy Fibonacci Sequence Generation

Algorithm description:

This Haskell code generates the Fibonacci sequence as an infinite, lazy list. The `fibonacciSequence` definition is recursive and uses `zipWith (+) fibonacciSequence (tail fibonacciSequence)` to efficiently compute subsequent numbers by summing the previous two. The `takeFibonacci` function then allows retrieving a finite prefix of this infinite sequence. This approach is highly efficient for generating large numbers of Fibonacci terms without computing them all upfront, making it suitable for scenarios where only a portion of a sequence is needed.

Algorithm explanation:

The `fibonacciSequence` is defined recursively. It starts with `0 : 1 : ...`. The rest of the sequence is generated by `zipWith (+) fibonacciSequence (tail fibonacciSequence)`. This means each subsequent element is the sum of the corresponding elements from the `fibonacciSequence` itself and its tail. For example, the third element is `fibonacciSequence[0] + fibonacciSequence[1]` (0 + 1 = 1), the fourth is `fibonacciSequence[1] + fibonacciSequence[2]` (1 + 1 = 2), and so on. This lazy evaluation means numbers are only computed when they are needed. The `takeFibonacci` function uses Haskell's built-in `take` function, which is also lazy. It handles the edge case where `n` is less than or equal to 0 by returning an empty list. The time complexity to generate the first `n` Fibonacci numbers is O(n) because each number is computed once. The space complexity is also O(n) to store the generated numbers up to `n`.

Pseudocode:

define fibonacciSequence as an infinite list:
  start with 0
  then 1
  then for each subsequent element, sum the previous two elements from the sequence itself

define takeFibonacci(n):
  if n is less than or equal to 0:
    return empty list
  else:
    return the first n elements of fibonacciSequence

main:
  print first 10 fibonacci numbers
  print first 5 fibonacci numbers
  print first 0 fibonacci numbers
  print first 1 fibonacci number
  print first 2 fibonacci numbers