【激突】関数型言語 VS オブジェクト指向言語2at TECH【激突】関数型言語 VS オブジェクト指向言語2 - 暇つぶし2ch■コピペモード□スレを通常表示□オプションモード□このスレッドのURL■項目テキスト624:デフォルトの名無しさん 12/04/30 15:16:50.31 Haskell(GHC)からC関数を使用 import Foreign.Marshal (newArray, mallocArray, peekArray, free) import Foreign.C.Types (CInt) import Foreign.Ptr (Ptr, plusPtr) import Foreign.Storable (sizeOf, peek) import Data.List (unfoldr) foreign import ccall "life" cLife :: Ptr (Ptr CInt) -> Ptr (Ptr CInt) -> CInt -> CInt -> IO () main = do x' <- newArray $ concat xss :: IO (Ptr (CInt)) y' <- mallocArray (m * n) x <- f x' =<< peek x' y <- f y' =<< peek y' cLife x y (fromIntegral m) (fromIntegral n) z <- peekArray (m * n) y' mapM_ free [x, y] mapM_ free [x',y'] print $ unfoldr (\xs -> if null xs then Nothing else Just $ splitAt n xs) z where xss = [[0,1,1,1,0], [0,1,0,0,0], [0,0,1,0,0], [0,0,0,0,0], [0,0,0,0,0]] m = length xss n = length (concat xss) `div` m f p h = newArray [plusPtr p (sizeOf h * i * n) | i <- [0..m-1]] 次ページ最新レス表示レスジャンプ類似スレ一覧スレッドの検索話題のニュースおまかせリストオプションしおりを挟むスレッドに書込スレッドの一覧暇つぶし2ch