-
-
Save Microtribute/8d93c20dc9617bdc506ebc329818d222 to your computer and use it in GitHub Desktop.
Ackermann functions in Haskell (from http://stackoverflow.com/questions/16115815/ackermann-very-inefficient-with-haskell-ghc)
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
acks :: [[Int]] | |
acks = [ [ case (m, n) of | |
(0, _) -> n + 1 | |
(_, 0) -> acks !! (m - 1) !! 1 | |
(_, _) -> acks !! (m - 1) !! (acks !! m !! (n - 1)) | |
| n <- [0..] ] | |
| m <- [0..] ] | |
main :: IO () | |
main = print $ acks !! 4 !! 1 |
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
module Main where | |
data P = P !Int !Int | |
main :: IO () | |
main = print $ ack (P 4 1) id | |
where | |
ack :: P -> (Int -> Int) -> Int | |
ack (P 0 n) k = k (n + 1) | |
ack (P m 0) k = ack (P (m-1) 1) k | |
ack (P m n) k = ack (P m (n-1)) (\a -> ack (P (m-1) a) k) |
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
main = print $ ack 4 1 | |
where ack :: Int -> Int -> Int | |
ack 0 n = n+1 | |
ack m 0 = ack (m-1) 1 | |
ack m n = ack (m-1) (ack m (n-1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment