我如何putStrLn Data.ByteString.Internal.ByteString?

Ner*_*Guy 7 string io haskell output

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

import Crypto.PasswordStore
import System.Environment

main = do
    args <- getArgs
    putStrLn (makePassword (head args) 12)
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

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)
Run Code Online (Sandbox Code Playgroud)

我一直在使用以下链接作为参考,但我现在只是试错了无济于事. 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

And*_*ewC 5

您还没有导入ByteString,因此它正在尝试使用putStrLn的String版本.我已经提供toBSString->ByteString转换.

尝试

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
Run Code Online (Sandbox Code Playgroud)


Joh*_*n L 5

你必须以不同的方式做两件事。首先,makePassword是在IO中,因此需要将结果绑定到一个名称,然后将该名称传递给IO函数。其次,您需要从以下位置导入IO函数Data.ByteString

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
Run Code Online (Sandbox Code Playgroud)

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

main = do
    args <- getArgs
    B.putStrLn =<< makePassword (B.pack $ head args) 12
Run Code Online (Sandbox Code Playgroud)