A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Zeckendorf in Haskell page! Here, you'll find the source code for this program as well as a description of how the program works.
module Main where
import Data.Maybe (listToMaybe)
import Data.Function ((&))
import Data.List (intercalate)
import Control.Monad (guard)
import Text.Read (readMaybe)
import System.Environment (getArgs)
listToString :: [Int] -> String
listToString = intercalate ", " . map show
fib :: [Int]
fib = 1 : 1 : zipWith (+) fib (drop 1 fib)
zeckendorf :: Int -> [Int]
zeckendorf n =
takeWhile (<= n) fib
& foldr (\m (n, l) -> if m <= n then (n-m, m:l) else (n, l)) (n, [])
& snd
& reverse
parseArgs :: [String] -> Maybe Int
parseArgs args = do
n <- listToMaybe args
n' <- readMaybe n
guard (n' >= 0)
return n'
main :: IO ()
main = do
args <- getArgs
case parseArgs args of
Nothing -> putStrLn "Usage: please input a non-negative integer"
Just n -> putStrLn $ listToString (zeckendorf n)
Zeckendorf in Haskell was written by:
If you see anything you'd like to change or update, please consider contributing.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.