次のコードがあります。これは、Excelの列タイプに対応する数値を変換する必要があります。たとえば、28に27とABにAA:折りたたみ内のタプルに一致しません
import Data.Char (ord)
import Data.List (foldl1')
columnToNumber :: String -> Int
columnToNumber s = foldl1' (\acc (i, v) -> acc + 26^i * v) (values s)
where values s = zip (reverse [0..(length s)]) ((\c -> ord c - 64) <$> s)
アイデアは、対応する番号
["A", "A"] -> [1, 1]
と右からそうベースでそれをジップに変換文字列「AA」を取ることです左に26^0,26^1,26^2などがあります。
zip [1, 0] [1, 1] -> [(1, 1), (0, 1)]
倍の結果は残念ながら、私は次のエラーを
26^1 * 1 + 26^0 * 1 = 27
を得ていることだろうと私はなぜわからないその方法:
ExcelSheetColumn.hs:7:34:
Couldn't match expected type ‘Int’
with actual type ‘(Integer, Int)’
In the pattern: (i, v)
In the first argument of ‘foldl1'’, namely
‘(\ acc (i, v) -> acc + 26^i * v)’
In the expression:
foldl1' (\ acc (i, v) -> acc + 26^i * v) (values s)
ExcelSheetColumn.hs:7:63:
Couldn't match type ‘(Int, Int)’ with ‘Int’
Expected type: [Int]
Actual type: [(Int, Int)]
In the second argument of ‘foldl1'’, namely ‘(values s)’
In the expression:
foldl1' (\ acc (i, v) -> acc + 26^i * v) (values s)
誰かが助けてもらえ私は出る?
まあ最初私はあなたが '長さSを使用したいと思います。 –