【激突】関数型言語 VS オブジェクト指向言語2at TECH【激突】関数型言語 VS オブジェクト指向言語2 - 暇つぶし2ch■コピペモード□スレを通常表示□オプションモード□このスレッドのURL■項目テキスト622:デフォルトの名無しさん 12/04/30 08:02:32.77 なんだ。Rubyが最強ってことか。 623:デフォルトの名無しさん 12/04/30 09:21:02.55 >>546のCの関数を呼び出してみた from __future__ import print_function from ctypes import * clife = cdll.LoadLibrary('liblife.so') clife.life.argtypes = [POINTER(POINTER(c_int)), POINTER(POINTER(c_int)), c_int, c_int] x = [[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]] a, b = (POINTER(c_int) * 5)(), (POINTER(c_int) * 5)() for i in xrange(5): a[i], b[i] = (c_int * 5)(*x[i]), (c_int * 5)() clife.life(a, b, 5, 5) print('\n'.join(''.join(str(b[i][j]) for j in xrange(5)) for i in xrange(5))) 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