我是Haskell的新手,我在嘗試獲取此腳本時遇到問題。該腳本從命令行讀入參數,並在單獨的文本文件中找到它們。使用編譯器加擾txt文件
cat.txt | ./scramble A123456A123456 (in compiler)
輸出應該是這樣的:
The cat was very sad ...etc.
++++++++++++++++++++
A123456A123456A123456 ...etc.
所以它取代無論是在與「A123456」無限的文本文件。
module Main where
import System
import Data.Char (chr, ord, isLower)
import Data.Bits
let2int :: Char -> Int
let2int c = ord c - ord 'a'
int2let :: Int -> Char
int2let n = chr (ord 'a' + n)
shift :: Int -> Char -> Char
shift n c | isLower c = int2let ((let2int c + n) `mod` 26)
| otherwise = c
main = do
arg1 <- getArgs
txt <- getContents
putStr (scramble txt arg1)
scramble :: Int -> String -> String
scramble shift msg =
let ords = map ord msg
shifted = map (+ shift) ords
in map chr shifted
然而,當我嘗試編譯此,GHCI返回錯誤:
redact.hs:19:26:
Couldn't match expected type 'Int' with actual type '[Char]'
Expected type: Int
Actual type: [String]
In the second argument of 'scramble', namely 'txt'
In the first of 'putStr', namely '<redact txt arg1>'
Failed, module loaded: none.
因此,代碼:
putStr (scramble txt arg1)
引起的問題。 我也曾嘗試:
arg1:_ <- getArgs
,但它並不能幫助
預先感謝您的幫助,如果你能無論如何改進代碼,將是巨大的。
@ user1284290複製+粘貼您的源代碼和錯誤信息到你的問題,而不是重新輸入他們錯了。 – dave4420 2012-03-30 00:00:55
「使用編譯器」是什麼意思?另外,這功課呢? – ivanm 2012-03-30 00:13:35
似乎是一項任務:http://stackoverflow.com/questions/9866421/text-scrambler-in-haskell-using-key-from-ghci – ivanm 2012-03-30 00:42:59