Haskell:使用IO和[]进行monad堆栈绑定

时间:2016-08-14 19:37:34

标签: haskell monad-transformers io-monad

我有:

stuff :: IO [String]
doThings :: String -> IO [()]

我想要

stuff >>= doThings

但我的类型已关闭。我想基本上做一个提升的绑定,但我尝试的一切都是巧妙的错误。

2 个答案:

答案 0 :(得分:1)

使用原始类型,您可以:

import numpy as np
from sklearn.feature_extraction.text import CountVectorizer

def wordcount(data):
    cv = CountVectorizer(min_df=0, strip_accents="ascii", decode_error ="ignore")

    counts = cv.fit_transform([data]).toarray().ravel()
    words = np.array(cv.get_feature_names())

    counts = map(int, counts)

    words = [i.tolist() for i in words]
    words = [x.encode('UTF8') for x in words]

    unsorted_result = zip(words, counts)
    print sorted(unsorted_result,key=lambda x: x[1], reverse=True)

如果您将doThings更改为类型stuff >>= mapM_ doThings

,这也有效

答案 1 :(得分:0)

如果你有很多这样的功能,这是ListT的教科书用例:

Sub RunCmd
dim shell
dim shellWithLog
dim command
dim ARGS
set shell = createobject("Shell.Application")
set shellWithLog = createobject("wscript.shell")

command = "gui.cmd"


if (checkbox1.checked) then
ARGS = ARGS + " Do_This"
end if

if (checkbox2.checked) then
ARGS = ARGS + " Do_That"
end if
if (logFile.checked) then
    shellWithLog.run (shell.shellExecute command, ARGS), " &>"  + "publishLog.log", 1
else
    shell.shellExecute command, , "runas", 1
end if
End Sub