Zeckendorf in Haskell

Published on 23 May 2026 (Updated: 23 May 2026)

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.

Current Solution

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.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.