-2

このプログラムは単語を数えることを目的としていますが、ヘッダーにエラーがあり、修正できませんでした。コンパイラが見つからないと言って使用するように求められました-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
4

1 に答える 1

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

LANGUAGE2 つの非標準言語拡張機能を使用していることを示すために、先頭の固定インデントとプラグマに注意してください。

実行できますがghc-pkg vector、次のようなメッセージが表示されない場合vector-0.9.1は、実行cabal updateしてからcabal install vector.

于 2012-09-12T11:47:46.177 に答える