简体   繁体   English

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

[英]How do I putStrLn a Data.ByteString.Internal.ByteString?

I'm learning haskell and decided to try writing some small test programs to get use to Haskell code and using modules. 我正在学习haskell,并决定尝试编写一些小的测试程序来使用Haskell代码和使用模块。 Currently I'm trying to use the first argument to create a password hash using the Cypto.PasswordStore. 目前我正在尝试使用第一个参数使用Cypto.PasswordStore创建密码哈希。 To test out my program I'm trying to create a hash from the first argument and then print the hash to screen. 为了测试我的程序,我试图从第一个参数创建一个哈希,然后将哈希打印到屏幕。

import Crypto.PasswordStore
import System.Environment

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

I'm getting the following error: 我收到以下错误:

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)

I've been using the following links as references but I am now just trial-erroring to no avail. 我一直在使用以下链接作为参考,但我现在只是试错了无济于事。 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 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

You haven't imported ByteString, so it's trying to use the String version of putStrLn. 您还没有导入ByteString,因此它正在尝试使用putStrLn的String版本。 I've provided toBS for the String->ByteString conversion. 我已经为toBS提供了toBS String->ByteString转换。

Try 尝试

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

You have to do two things differently. 你必须以不同的方式做两件事。 First, makePassword is in IO, so you need to bind the result to a name and then pass the name to the IO function. 首先, makePassword位于IO中,因此您需要将结果绑定到名称,然后将名称传递给IO函数。 Secondly, you need to import IO functions from Data.ByteString 其次,您需要从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

Or, if you won't be using the password result anywhere else, you can use bind to connect the two functions directly: 或者,如果您不在其他任何地方使用密码结果,您可以使用bind直接连接这两个函数:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM