The elders' functional programming language
Compile and run on Glasgow Haskell Compiler(GHC) version 9.4.8 in Replit, unless otherwise specified
- Compile
ghc FibonacciSequence.hs - Run
./FibonacciSequence
-
Generate the Fibonacci sequence up to the specified number of terms
-
Code and Output
Code : FibonacciSequence.hs
-- | Calculates the nth Fibonacci number using recursion. fibonacci :: Int -> Int fibonacci n | n <= 1 = n | otherwise = fibonacci (n - 1) + fibonacci (n - 2) -- | Main function to calculate and print the first 10 Fibonacci numbers. main :: IO () main = do let fibNumbers = take 10 [fibonacci i | i <- [1..]] putStrLn $ unwords $ map show fibNumbers
Output
1 1 2 3 5 8 13 21 34 55