这个程序是关于计算单词,但它在标题中给出错误,我无法解决它。它说未找到的编译器并要求我使用-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
答案 0 :(得分: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
pragma表示您使用
两种非标准语言扩展。
您可以运行ghc-pkg vector
,如果它没有说出vector-0.9.1
之类的内容,那么您需要运行cabal update
,然后运行cabal install vector
。