haskell - 在这种情况下如何使用 mapM_

标签 haskell types polymorphism map-function foldable

我正在尝试打印 autocorrelation 结果的不同组成部分在不同的线路上:

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

关于haskell - 在这种情况下如何使用 mapM_,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57931873/

相关文章:

java - 你如何存储日期范围,它们实际上是时间戳

c# - 在 C# 中将字符串转换为类型

C++:对抗多态开销

java - 使用重写的抽象方法从父类(super class)访问私有(private)变量

c# - If vs 重载 vs 反射

haskell - 重用多个值构造函数模式匹配时避免重复代码

Haskell 代码的优化

haskell - 为什么不能将 take 的类型更改为使用任何 Integral?

haskell - 我怎样才能通过 cabal new-test 的测试选项?

c++ - 编译器强制语义类型