Created
October 16, 2016 22:09
-
-
Save morgaine/182ee74d9c77530d6ebf4d80908df0f4 to your computer and use it in GitHub Desktop.
Hyper-Exponential function in Haskell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/runhaskell | |
-- | |
-- NAME | |
-- hyper_exponential.hs | |
-- | |
-- DESCRIPTION | |
-- Calculates terms of the HyperExponential sequence: | |
-- 1 | |
-- 2^2 | |
-- (3^3)^3 | |
-- ((4^4)^4)^4 | |
-- (((5^5)^5)^5)^5 | |
-- ((((6^6)^6)^6)^6)^6 | |
-- ... | |
-- | |
-- The main function below limits the computation to 6 terms. | |
-- N = 10 is still reasonable, but higher terms rapidly tend | |
-- towards non-computability before heat death of the universe. | |
-- | |
-- AUTHOR | |
-- Morgaine Dinova | |
-- | |
module HyperExponential where | |
import Text.Printf | |
hyperExp :: Integer -> Integer | |
hyperExp n = hyperExp' n n | |
where | |
hyperExp' n 0 = 1 | |
hyperExp' n 1 = n | |
hyperExp' n m = (hyperExp' n (m-1))^n | |
hyperDo :: Integer -> Integer -> IO () | |
hyperDo from to = do | |
-- printf "Calculating term %d of HyperExponential sequence:\n" from | |
let hyperN = hyperExp from | |
printf "#\t%d\t%d\n" from hyperN | |
if from < to | |
then hyperDo (from+1) to | |
else return () | |
seqN :: (Integer -> Integer) -> Integer -> [Integer] | |
seqN f n = map f [1..n] | |
main :: IO () | |
main = do | |
-- Just the sequence as a list, no table. | |
-- print $ seqN hyperExp 5 | |
printf "#===================\n" | |
printf "#\tn\tf(n)\n" | |
printf "#===================\n" | |
hyperDo 1 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output for N in [1..6]: