2012-11-03 8 views
7

私はhaskellを学んでいて、Haskellのコードを使い、モジュールを使うための小さなテストプログラムを書くことに決めました。現在、Cypto.PasswordStoreを使用してパスワードハッシュを作成するために、最初の引数を使用しようとしています。私のプログラムをテストするために、私は最初の引数からハッシュを作成して、画面にハッシュを印刷しようとしています。どのように私はStrLnにData.ByteString.Internal.ByteStringを配置しますか?

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) 

私は、参照として、以下のリンクを使用してきたが、私は今、ちょうど裁判-erroring無駄にしています。 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

答えて

4

あなたは延ByteStringをインポートしていないので、putStrLnの文字列バージョンを使用しようとしています。 String->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 
+0

感謝を!これは私のために働く。このコードでは廃止予定の関数について警告していますが、答えは必要なものです。 _Warning: 'B.putStrLn 'を使用しています。 (Data.ByteStringからインポートしました): 廃止予定:「Data.ByteString.Char8.putStrLnを代わりに使用してください。 – NerdGGuy

+1

@NerdGGuy' Data.ByteString'を完全に削除して、 Data.ByteString.Char8'の代わりに(編集を参照) – AndrewC

+0

Data.ByteString.Char8を使用すると意味があります – NerdGGuy

4

を試してみてください。まず、makePasswordが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 

または、からIO関数をインポートする必要があり、あなたが直接、二つの機能を接続するためにバインドを使用することができます。

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

修飾されたData.ByteStringをBとしてインポートすることを意味しますか? もう1つのエラー: できませんでした実際のタイプが 'String 'の と予想されるタイプとの一致 予想タイプ:[B.ByteString] 実際のタイプ:[String] ' head'の最初の引数、つまりargsで 第1引数の 'makePassw 'ord'、つまり '(head args) ' – NerdGGuy

+1

うん、固定。プレコーヒーを投稿するよう教えて –

関連する問題