Shootout/Ack

From HaskellWiki
Revision as of 01:29, 8 October 2006 by DonStewart (talk | contribs) (port)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

A Shootout entry for the ackermann function

Code[edit]

All the following entries have the same performance. The difference is loc. The idea is to beat the SML entry of 4 loc. Performance is already excellent.

Interestingly, all run much faster when compiled with -fasm.

Proposal[edit]

-- http://shootout.alioth.debian.org/
-- shortened by Bryn Keller, Einar Karttunen and Don Stewart

import System

main = getArgs >>= \[n] -> putStrLn . (("Ack(3,"++n++"): ")++) . show . ack 3 $ read n
 
ack (0::Int) (n::Int) = n+1
ack  m        n       = ack (m-1) $ if n == 0 then 1 else ack m (n-1)

Original entry[edit]

7 loc

{-# OPTIONS -O2 -fasm #-}
-- $Id: ackermann-ghc.code,v 1.27 2006/01/08 22:44:56 igouy-guest Exp $
-- http://shootout.alioth.debian.org/
-- shortened by Bryn Keller and Einar Karttunen

import System(getArgs)

main = do ~[num] <- getArgs
          putStrLn ("Ack(3," ++ num ++ "): " ++ (show (ack 3 (read num))))

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))