How to use mapM_ in this situation

319 Views Asked by At

I am trying to print different components of result of autocorrelation on different lines:

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; 

The output is:

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])

However, if I uncomment last line, I get error:

• 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;
               .... }

How can I print all parts of result on separate lines? Thanks for your help.

3

There are 3 best solutions below

3
On

The simplest thing is just to pattern match.

main = do
    let (a,b,c) = autocorrelation $ V.fromList sampleA
    print a
    print b
    print c
0
On

If you really don't want to pattern-match yourself, you can use the fixed-vector package to help you. Note that it comes with its own mapM_ that you'd have to use:

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

Alternatively, you could make a newtype and stick your tuple in there:

{-# 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
0
On

We can make use of the lens [Hackage] package to transform a tuple into a list:

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)))

But it is probably better to do pattern matching like @DanielWagner says, since then you make it more explicit what is happening.