タプルを生成し、全タプルのリストを生成したいです。
具体的には[(0,0,0), (0,0,1), ...]
のような感じです。
まずタプルを生成して返す関数を作りたいです。
initX :: Int -> Int initX n | n < 4 = 2 | otherwise = 4 initY :: Int -> Int initY n = 0 initZ :: Int -> Int initZ n = 0 initVector :: Int -> (Int, Int, Int) initVector n = (initX n, initY n, initZ n) print $ initVector 3 -- (2,0,0)
この関数をひとつにしたいのでdoを使いました。
initVectorIO :: Int -> IO (Int, Int, Int) initVectorIO n = do let x | n < 4 = 2 | otherwise = 4 let y = 0 let z = 0 return (x, y, z)
するとIO型になるので取り出す必要がでてきました。
print =<< initVectorIO 3 -- (4,0,0)
最終的にリストを返す関数はこちらです。
initPosition :: Int -> [(Int, Int, Int)] initPosition n | 0 < n = initPosition(n-1) ++ [initVector n] | 0 >= n = []
このinitPosition
のinitVector
の部分にinitVectorIO
を使いたいです。お願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2016/06/20 00:18