Je le code C suivant:Passe pointeur vers un tableau dans Haskell à C Fonction
#include <sys/times.h>
#include <time.h>
float etime_(float *tarray)
{ struct tms buf;
times(&buf);
tarray[0] = 1.0 * buf.tms_utime/CLOCKS_PER_SEC;
tarray[1] = 1.0 * buf.tms_stime/CLOCKS_PER_SEC;
return tarray[0] + tarray[1];
}
Essayer de porter ce code Fortran à Haskell:
PROGRAM Test
IMPLICIT NONE
REAL t, ta(2), etime
INTEGER i
DOUBLE PRECISION x
do i = 1, 10000
x = sin(cos(i * 1.0 d0))
print *, x
enddo
ta(1) = 0.0d0
ta(2) = 0.0d0
t = etime(ta)
PRINT *, 'user time: ', ta(1)
PRINT *, 'system time: ', ta(2)
PRINT *, 'process time: ', t
END
Comment puis-je définir tableau et ! ou !!! pour que le code ci-dessous fonctionne?
module Main where
import GHC.Ptr
import GHC.Prim
import System.IO.Unsafe
import Control.Monad
foreign import ccall etime_ :: Ptr Double → IO Double
etime = etime_
main :: IO Int
main = do
mapM_ (print . sin . cos . (* (1.0 :: Double)) . fromIntegral) [1..10000 :: Int]
ta ← array 2
t ← etime ta
putStrLn $ "user time: " ++ show (ta !!! 0)
putStrLn $ "system time: " ++ show (ta !!! 1)
putStrLn $ "process time: " ++ show t
return 0
array :: Int → IO (Ptr a)
array size = undefined
(!) :: Ptr a → Int → IO a
(!) = undefined
(!!!) :: Ptr a → Int → a
(!!!) = undefined
Y a-t-il une différence de précision entre float et double en haskell? sont les deux primitives ou est flottant pour doubler comme entier à int? –
Oui: le Prelude définit Float et Double pour les nombres à virgule flottante simple et double précision respectivement. Voir la section Numéros du Rapport Haskell: http://www.haskell.org/onlinereport/basic.html#sect6.4 –