2011-04-11 56 views
3

我有一個向量FloatgetVectorFloat功能創建的元素。 爲了進行一些測量,我需要使用deepSeqArray。 但是,我無法做到這一點。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

謝謝你的幫助

回答

1

問題來了,從不做你認爲的做。 當你做ws <- getVectorFloat len

ws沒有設置數組,但調用它的每個值。即數組的符號,與地圖類似。請看下面的例子

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

所以我不知道您這樣的使用是適當的。

+1

好的。 所以我在main之前設置了'ws = getVectorFloat len'。現在主要包含'do ws \'deepSeqArray \'return()'。 編譯正確。 謝謝! – 2011-04-11 12:47:39