【问题标题】:How do I putStrLn a Data.ByteString.Internal.ByteString?如何将StrLn 放入Data.ByteString.Internal.ByteString?
【发布时间】:2012-10-23 16:48:07
【问题描述】:

我正在学习 Haskell,并决定尝试编写一些小型测试程序来习惯 Haskell 代码和使用模块。目前我正在尝试使用第一个参数来使用 Cypto.PasswordStore 创建密码哈希。为了测试我的程序,我尝试从第一个参数创建一个哈希,然后将哈希打印到屏幕上。

import Crypto.PasswordStore
import System.Environment

main = do
    args <- getArgs
    putStrLn (makePassword (head args) 12)

我收到以下错误:

testmakePassword.hs:8:19:
    Couldn't match expected type `String'
            with actual type `IO Data.ByteString.Internal.ByteString'
    In the return type of a call of `makePassword'
    In the first argument of `putStrLn', namely
      `(makePassword (head args) 12)'
    In a stmt of a 'do' block: putStrLn (makePassword (head args) 12)

我一直使用以下链接作为参考,但我现在只是试错无济于事。 http://hackage.haskell.org/packages/archive/bytestring/0.9.0.4/doc/html/Data-ByteString-Internal.html http://hackage.haskell.org/packages/archive/pwstore-purehaskell/2.1/doc/html/Crypto-PasswordStore.html

【问题讨论】:

    标签: string haskell io output


    【解决方案1】:

    您还没有导入 ByteString,所以它正在尝试使用 putStrLn 的 String 版本。 我为String-&gt;ByteString 转换提供了toBS。

    试试

    import Crypto.PasswordStore
    import System.Environment
    import qualified Data.ByteString.Char8 as B
    
    toBS = B.pack
    
    main = do
        args <- getArgs
        makePassword (toBS (head args)) 12 >>= B.putStrLn
    

    【讨论】:

    • 谢谢!这对我有用。该代码确实警告过不推荐使用的功能,但答案是我需要的。 警告:在使用 `B.putStrLn'(从 Data.ByteString 导入):不推荐使用:“使用 Data.ByteString.Char8.putStrLn 代替
    • @NerdGGuy 让我们完全摆脱Data.ByteString,然后改用Data.ByteString.Char8(见编辑)。
    • 太棒了。使用 Data.ByteString.Char8 对我来说很有意义。
    【解决方案2】:

    你必须以不同的方式做两件事。首先makePassword在IO中,所以需要给结果绑定一个名字,然后把名字传递给IO函数。其次,需要从Data.ByteString导入IO函数

    import Crypto.PasswordStore
    import System.Environment
    import qualified Data.ByteString as B
    
    main = do
        args <- getArgs
        pwd <- makePassword (B.pack $ head args) 12
        B.putStrLn pwd
    

    或者,如果您不会在其他任何地方使用密码结果,您可以使用 bind 直接连接这两个函数:

    main = do
        args <- getArgs
        B.putStrLn =<< makePassword (B.pack $ head args) 12
    

    【讨论】:

    • 您的意思是将合格的Data.ByteString 导入为B 吗?还有一个错误:无法匹配预期类型B.ByteString' with actual type String' 预期类型:[B.ByteString] 实际类型:[String] 在head', namely args' 的第一个参数中在makePassword', namely (head 的第一个参数中args)'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-24
    • 2016-01-24
    • 2014-06-19
    • 2013-04-25
    • 2020-04-30
    • 2014-11-30
    • 2011-06-02
    相关资源
    最近更新 更多