3

我有一个由函数Float创建的元素向量。getVectorFloat为了进行一些测量,我需要使用deepSeqArray. 但是,我无法做到。

这是我的例子:

import Data.Array.Repa as R
import Data.Array.Repa.Algorithms.Randomish

len :: Int
len = 3000000

main = do
    ws <- getVectorFloat len
    ws `deepSeqArray` return()

getVectorFloat :: Int -> Array DIM1 Float
getVectorFloat len = R.map (toFloat) (getVectorDouble len)

toFloat :: Double -> Float
toFloat a = realToFrac a

getVectorDouble :: Int -> Array DIM1 Double
getVectorDouble len = randomishDoubleArray (Z :. len) (-100) 100 1

我得到的错误是:

haskell.hs:9:9:
    Couldn't match expected type `Array sh0 a0'
                with actual type `Float'
    In the first argument of `deepSeqArray', namely `ws'
    In the expression: ws `deepSeqArray` return ()
    In the expression:
      do { ws <- getVectorFloat len;
             ws `deepSeqArray` return () }

我不明白为什么Array sh0 a0不能匹配到Array Dim1 Float.

谢谢您的帮助

4

1 回答 1

1

问题来自于没有按照你的想法做的事情。当你这样做ws <- getVectorFloat len

ws 不是用数组设置的,而是为它的每个值调用的。即数组的do 表示法类似于地图。考虑以下示例

prelude > do x <- [1,2,3]; return (x*10)
[10, 20, 30]

或更有趣;-)

Prelude> do <-[1,2,3];y<-[10,20,30]; return(x,y)                                                                                                                                         
[(1,10),(1,20),(1,30),(2,10),(2,20),(2,30),(3,10),(3,20),(3,30)]   

所以我不确定你对 do 的使用是否合适。

于 2011-04-11T10:41:14.190 回答