一千萬個為什麽

搜索

使用cmdArgs提取Int時輸入不匹配

我嘗試使用CmdArgs從命令行參數中提取整數: ./prg --byte = 512

The below code does not compile and the compiler says Couldn't match expected type `Int' with actual type `Strlen -> Int'. Now, I thought that I had deemed byte to be an integer? Isn't it?

{-# LANGUAGE DeriveDataTypeable #-}
import System.Console.CmdArgs

data Strlen = Strlen {byte :: Int} deriving (Data, Typeable)

strlen = cmdArgsMode $ Strlen {byte = def} 

main = do
        n <- cmdArgsRun strlen 
        let datastring = take byte $ randomRs ('a','Z') (mkStdGen 3)

最佳答案

要從 n 中選擇 byte 字段,您需要說 byte n 。所以

let datastring = take (byte n) $ randomRs ('a','Z') (mkStdGen 3)

轉載註明原文: 使用cmdArgs提取Int時輸入不匹配