Difference between revisions of "Shootout/Takfp"

From HaskellWiki
Jump to navigation Jump to search
(moved)
 
m
 
Line 1: Line 1:
 
 
A ShootoutEntry for takfp
 
A ShootoutEntry for takfp
   
Line 16: Line 15:
 
import System
 
import System
   
main = putStrLn . show . (\n -> tak (3*n) (2*n) n) . read . head =<< getArgs
+
main = print . (\n -> tak (3*n) (2*n) n) . read . head =<< getArgs
   
 
tak x y z = if y>=x then z::Float else tak (tak (x-1) y z) (tak (y-1) z x) (tak (z-1) x y)
 
tak x y z = if y>=x then z::Float else tak (tak (x-1) y z) (tak (y-1) z x) (tak (z-1) x y)
Line 28: Line 27:
 
import System
 
import System
   
main = putStrLn . show . (\n -> tak (3*n) (2*n) n) . read . head =<< getArgs
+
main = print . (\n -> tak (3*n) (2*n) n) . read . head =<< getArgs
   
 
tak x y z | y >= x = z :: Float
 
tak x y z | y >= x = z :: Float
Line 46: Line 45:
   
 
main = do n <- getArgs >>= readIO.head
 
main = do n <- getArgs >>= readIO.head
putStrLn $ show $ tak (3*n) (2*n) n
+
print $ tak (3*n) (2*n) n
   
 
tak :: Float -> Float -> Float -> Float
 
tak :: Float -> Float -> Float -> Float

Latest revision as of 07:50, 13 December 2009

A ShootoutEntry for takfp

Shorter entry

Shortest entry in any language. Faster than old entry (by cranking up gcc)

{-# OPTIONS -O2 -optc-O3 #-}
-- http://shootout.alioth.debian.org/
--
-- GHC version of floating point Tak function
-- compile with ghc -O2 -fexcess-precision -o takfp takfp.hs
-- contributed by Greg Buchholz, optimized by Einar Karttunen and Don Stewart

import System

main = print . (\n -> tak (3*n) (2*n) n) . read . head =<< getArgs

tak x y z = if y>=x then z::Float else tak (tak (x-1) y z) (tak (y-1) z x) (tak (z-1) x y)

Current entry

Refactor musasabi's code. Still uses too much space.

import System

main = print . (\n -> tak (3*n) (2*n) n) . read . head =<< getArgs

tak x y z | y >= x    = z :: Float
          | otherwise = tak (tak (x-1) y z) (tak (y-1) z x) (tak (z-1) x y)

Old entry

-- http://shootout.alioth.debian.org/
--
-- GHC version of floating point Tak function
-- compile with ghc -O2 -fexcess-precision -o takfp takfp.hs
-- contributed by Greg Buchholz, optimized by Einar Karttunen

import System(getArgs)

main = do n <- getArgs >>= readIO.head
          print $ tak (3*n) (2*n) n

tak :: Float -> Float -> Float -> Float
tak x y z | y>=x      = z
          | otherwise = tak (tak (x-1) y z) (tak (y-1) z x) (tak (z-1) x y)