Haskell 在这种情况下如何使用mapM_2;

Haskell 在这种情况下如何使用mapM_2;,haskell,types,polymorphism,map-function,foldable,Haskell,Types,Polymorphism,Map Function,Foldable,我试图在不同的行上打印结果的不同部分: import Data.Vector as V import Statistics.Autocorrelation import Data.Typeable sampleA = [1.0, 2.0, 3.0, 4.0, 1.0, 2.0, 3.0, 4] main = do let res = autocorrelation $ V.fromList sampleA putStr "Type of result of autocorre

我试图在不同的行上打印结果的不同部分:

import Data.Vector as V
import Statistics.Autocorrelation
import Data.Typeable

sampleA = [1.0, 2.0, 3.0, 4.0, 1.0, 2.0, 3.0, 4]

main = do
    let res = autocorrelation $ V.fromList sampleA
    putStr "Type of result of autocorrelation test: "
    print $ typeOf res
    print res
    -- Prelude.mapM_ print res      -- not working; 
输出为:

Type of result of autocorrelation test: ((Vector Double),(Vector Double),(Vector Double))

([1.0,2.5e-2,-0.45,-0.325,0.5,0.125,-0.15],[1.0,-1.3255000000000001,-1.4039375473415425,-1.442999810318651,-1.5311377955236107,-1.5364636906417393,-1.544097864842309],[1.0,1.0755000000000001,1.1539375473415425,1.192999810318651,1.2811377955236107,1.2864636906417393,1.294097864842309])
但是,如果取消注释最后一行,则会出现错误:

• No instance for (Foldable ((,,) (Vector Double) (Vector Double)))
    arising from a use of ‘Prelude.mapM_’
• In a stmt of a 'do' block: Prelude.mapM_ print res
  In the expression:
    do { let res = autocorrelation $ fromList sampleA;
         putStr "Type of result of autocorrelation test: ";
         print $ typeOf res;
         print res;
         .... }
  In an equation for ‘main’:
      main
        = do { let res = ...;
               putStr "Type of result of autocorrelation test: ";
               print $ typeOf res;
               .... }

如何在单独的行上打印结果的所有部分?谢谢你的帮助。

最简单的事情就是模式匹配

main = do
    let (a,b,c) = autocorrelation $ V.fromList sampleA
    print a
    print b
    print c
如果你真的不想和自己的模式匹配,你可以用它来帮助你。请注意,它有自己的
mapM
,您必须使用:

import Data.Vector as V
import qualified Data.Vector.Fixed as F
import Statistics.Autocorrelation
import Data.Typeable

sampleA = [1.0, 2.0, 3.0, 4.0, 1.0, 2.0, 3.0, 4]

main = do
    let res = autocorrelation $ V.fromList sampleA
    putStr "Type of result of autocorrelation test: "
    print $ typeOf res
    print res
    F.mapM_ print res
或者,您可以创建一个
newtype
,并将元组粘贴在其中:

{-# LANGUAGE DeriveFoldable #-}

import Data.Vector as V
import Statistics.Autocorrelation
import Data.Typeable

newtype Triple a = Triple (a, a, a) deriving(Foldable)

sampleA = [1.0, 2.0, 3.0, 4.0, 1.0, 2.0, 3.0, 4]

main = do
    let res = autocorrelation $ V.fromList sampleA
    putStr "Type of result of autocorrelation test: "
    print $ typeOf res
    print res
    Prelude.mapM_ print $ Triple res
我们可以利用包将元组转换为列表:

import Control.Lens(toListOf, each)
import qualified Data.Vector as V
import Statistics.Autocorrelation

sampleA = [1.0, 2.0, 3.0, 4.0, 1.0, 2.0, 3.0, 4]

main :: IO ()
main = mapM_ print (toListOf each (autocorrelation (V.fromList sampleA)))
导入控制。镜头(托利斯托夫,每个)
导入符合条件的数据。向量为V
导入统计。自相关
samreake=[1.0,2.0,3.0,4.0,1.0,2.0,3.0,4]
main::IO()
main=mapM_uu打印(每个的toListOf(自相关(V.fromList-samreak)))

但是,像这样进行模式匹配可能更好,因为这样你可以更明确地说明发生了什么。

我可以以某种方式使用mapM或mapM吗?@rnso在这种情况下,是的,(a,b,c)->mapM打印[a,b,c],因为
a,b,c
恰好是同一类型,
Vector Double
。谢谢。最好在你的答案中也加上这个。