Fibonacci in Every Language
Tags: challenge, polyglot, beginners, opensource
Published: 2019-05-22 14:25:35

Once again, I thought it would be fun to kick off one of these challenges in as many programming languages as possible. Last time, [we tried our hand at Fizz Buzz in Every Language][3]. If you haven't gotten a chance to share a solution in your favorite language, go for it! Otherwise, we're going to tackle Fibonacci in Every Language today.

## The Challenge

As many of you are probably aware, Fibonacci is a term often associated with a particular sequence of numbers where the nth term is the sum of the previous two terms—or more formally: F<sub>n</sub> = F<sub>n-1</sub> + F<sub>n-2</sub>. For our purposes, we'll start the sequence with a pair of ones:

> 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...

The goal in this challenge is to write a program in a language of your choice to output the sequence up to the nth term. In other words, the program should be able to be executed like:

```shell
./fib 5
```

and, output the sequence above up to the fifth term:

```shell
1, 1, 2, 3, 5
```

If you're feeling adventurous, you could try making your program conform to [the requirements outlined in the Sample Programs repo][1]. In addition to outputting the sequence, you should also output the index of each term:

```shell
1: 1
2: 1
3: 2
4: 3
5: 5
```

Either style is fine for the purposes of this challenge. That said, if you follow the second convention, it'll be much easier to transition [into the repo][2]!

## The Solution

As always, I'll kick off the fun with a solution in Haskell brought to you by a couple of our contributors ([Parker Johansen][4] and [Panagiotis Georgiadis][5]):

```haskell
module Main where

import System.Environment
import Text.Read

fibonacci :: Int -> [Integer]
fibonacci = flip (take) fibs

fibs :: [Integer]
fibs = 1 : 1 : zipWith (+) fibs (tail fibs)

headMaybe :: [a] -> Maybe a
headMaybe []     = Nothing
headMaybe (x:xs) = Just x

-- Takes a list of values and returns a list of strings in the format "ONE_BASED_INDEX: VALUE"
printWithIndex :: (Show a) => [a] -> [[Char]]
printWithIndex = zipWith (\i x -> (show i) ++ ": " ++ (show x)) [1..]


-- Prints out the first N numbers from the fibonacci sequence
-- where N equals to the first command line argument.
main :: IO ()
main = do
  args <- getArgs
  let n = headMaybe args
  case n >>= readMaybe of
    Nothing -> putStrLn "Usage: please input the count of fibonacci numbers to output"
    Just n  -> mapM_ (putStrLn) $ (printWithIndex . fibonacci) n
```

For those of you that maybe haven't seen functional programming before, this solution probably looks pretty wild. That said, I think Haskell is far more readable than anything Lisp-based, so you can probably get the gist of how this works.

Unfortunately, we don't currently have this solution documented in our collection, so maybe one of you can help out. We'd appreciate the support! 


[1]: https://sample-programs.therenegadecoder.com/projects/fibonacci/
[2]: https://github.com/TheRenegadeCoder/sample-programs
[3]: https://dev.to/renegadecoder94/fizz-buzz-in-every-language-7o3
[4]: https://github.com/auroq
[5]: https://github.com/PanagiotisGeorgiadis