首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何putStrLn a Data.ByteString.Internal.ByteString?

如何putStrLn a Data.ByteString.Internal.ByteString?
EN

Stack Overflow用户
提问于 2012-11-03 09:44:42
回答 2查看 2.7K关注 0票数 9

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

代码语言:javascript
复制
import Crypto.PasswordStore
import System.Environment

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

我得到以下错误:

代码语言:javascript
复制
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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-11-03 09:49:15

您尚未导入ByteString,因此它尝试使用putStrLn的字符串版本。我已经为String->ByteString转换提供了toBS

试一试

代码语言:javascript
复制
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
票数 5
EN

Stack Overflow用户

发布于 2012-11-03 09:51:33

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

代码语言:javascript
复制
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直接连接这两个函数:

代码语言:javascript
复制
main = do
    args <- getArgs
    B.putStrLn =<< makePassword (B.pack $ head args) 12
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13205192

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档