2012-09-12 40 views
-3

這個程序是關於計算單詞,但它給頭中的錯誤,我無法修復它。它說沒有找到編譯器,並要求我使用-v,但這也給出了一個錯誤。什麼是我需要用於向量的其他文件?導入未找到

這裏是我想要編譯代碼:

{-# LANGUAGE BangPatterns, MagicHash #-} 

import qualified Data.Vector.Unboxed as VU 
import Data.Vector.Unboxed ((!)) 
import qualified Data.Vector.Generic as VG --this one 
import GHC.Base (Int(..), quotInt#, remInt#) 

ones, tens, teens :: [String] 
ones = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"] 
tens = ["", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"] 
teens = ["ten", "eleven", "twelve", "thirteen", 
"fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"] 

wordify :: Int -> String 
wordify n 
    | n < 10   = ones !! n 
    | n < 20   = teens !! (n-10) 
    | n < 100  = splitterTen 
    | n < 1000  = splitter 100 "hundred" 
    | n < 1000000 = splitter 1000 "thousand" 
    | otherwise  = splitter 1000000 "million" 
     where 
     splitterTen = let (t, x) = n `quotRem` 10 
         in (tens !! t) ++ wordify x 
     splitter d suffix = let (t, x) = n `quotRem` d 
          in (wordify t) ++ suffix ++ wordify x 

wordLength :: Int -> Int 
wordLength i = go 0 i 
    where 
    go !pad !n 
     | n < 10   = lenOnes `VG.unsafeIndex` n + pad 
     | n < 20   = lenTeens `VG.unsafeIndex` (n-10) + pad 
     | n < 100  = go (lenTens `VG.unsafeIndex` (n//10) + pad) (n%10) 
     | n < 1000  = go (go (7+pad) (n//100)) (n%100) 
     | n < 1000000 = go (go (8+pad) (n//1000)) (n%1000) 
     | otherwise  = go (go (7+pad) (n//1000000)) (n%1000000) 

    (I# a) // (I# b) = I# (a `quotInt#` b) 
    (I# a) % (I# b) = I# (a `remInt#` b) 
    !lenOnes = VU.fromList [0,3,3,5,4,4,3,5,5,4] -- "", "one","two", ... 
    !lenTens = VU.fromList [0,3,6,6,5,5,5,7,6,6] 
    !lenTeens = VU.fromList [3,6,6,8,8,7,7,9,8,8] -- first element is "ten" 3 
+3

你應該向你的問題添加你得到的錯誤,你用來編譯和/或運行你使用的代碼和操作系統的命令行。 – nponeccop

+1

你也應該添加你的編譯器的版本。 –

+1

錯誤是無法找到模塊'Data.Vector.Generic' 使用-v查看搜索文件的列表。我的操作系統是Windows我的編譯器是ghci 2012.2.0.0 – user1664205

回答

2

看來你

  1. 小姐擴展
  2. 有破壓痕

這裏有一個固定的版本:

{-# LANGUAGE MagicHash, BangPatterns #-} 

import qualified Data.Vector.Unboxed as VU 
import Data.Vector.Unboxed ((!)) 
import qualified Data.Vector.Generic as VG --this once 
import GHC.Base (Int(..), quotInt#, remInt#) 

ones, tens, teens :: [String] 
ones = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"] 
tens = ["", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"] 
teens = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eig hteen", "nineteen"] 

wordify :: Int -> String 
wordify n 
| n < 10   = ones !! n 
| n < 20   = teens !! (n-10) 
| n < 100  = splitterTen 
| n < 1000  = splitter 100 "hundred" 
| n < 1000000 = splitter 1000 "thousand" 
| otherwise  = splitter 1000000 "million" 
    where 
    splitterTen = let (t, x) = n `quotRem` 10 
    in (tens !! t) ++ wordify x 
    splitter d suffix = let (t, x) = n `quotRem` d 
    in (wordify t) ++ suffix ++ wordify x 

wordLength :: Int -> Int 
wordLength i = go 0 i 
    where 
    go !pad !n 
    | n < 10   = lenOnes `VG.unsafeIndex` n + pad 
    | n < 20   = lenTeens `VG.unsafeIndex` (n-10) + pad 
    | n < 100  = go (lenTens `VG.unsafeIndex` (n//10) + pad) (n%10) 
    | n < 1000  = go (go (7+pad) (n//100)) (n%100) 
    | n < 1000000 = go (go (8+pad) (n//1000)) (n%1000) 
    | otherwise  = go (go (7+pad) (n//1000000)) (n%1000000) 

    (I# a) // (I# b) = I# (a `quotInt#` b) 
    (I# a) % (I# b) = I# (a `remInt#` b) 
    !lenOnes = VU.fromList [0,3,3,5,4,4,3,5,5,4] -- "", "one","two", ... 
    !lenTens = VU.fromList [0,3,6,6,5,5,5,7,6,6] 
    !lenTeens = VU.fromList [3,6,6,8,8,7,7,9,8,8] -- first element is "ten" 3 

注意固定縮進和LANGUAGE編譯指示開始時表示您使用了兩個非標準語言擴展 。

您可以運行ghc-pkg vector,如果它不這樣說vector-0.9.1那麼你需要運行cabal update隨後cabal install vector

+0

抱歉,什麼是cabal? – user1664205

+0

請參閱http://hackage.haskell.org/trac/hackage/wiki/CabalInstall。這是Haskell的庫安裝程序。它帶有Haskell平臺。如果'ghc-pkg vector'未能顯示庫的版本 - 這意味着你沒有安裝矢量庫並且必須安裝它。運行'cabal install {庫名}}是安裝庫的首選方式。 – nponeccop

+0

我在哪寫這個命令? – user1664205