gpt4 book ai didi

haskell - 将一元函数传递到需要普通函数的地方

转载 作者:行者123 更新时间:2023-12-02 15:18:59 25 4
gpt4 key购买 nike

如果我有一个函数

pretty :: FilePath -> IO String

如何将其传递给

interact :: (String -> String) -> IO ()

最佳答案

首先,interact::(String -> String) -> IO (),请注意括号。这是完全不同的类型。

写作

main = interact f

is the same as writing

main = do s <- getContents
putStr (f s)

现在,getContents::IO String,即与pretty filename相同的类型,带有filename::FilePath。这样一个人就可以代替另一个人去了:

myinteraction filename f = 
do
s <- pretty filename
putStr (f s)

但你不能只是将其强行放在不适合的地方。

关于haskell - 将一元函数传递到需要普通函数的地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22116675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com