你能把 Haskell 列表变成一系列 do 指令吗?

发布于 2024-08-25 04:33:46 字数 105 浏览 7 评论 0原文

您可以创建一个函数列表,然后按顺序执行它们,也许将它们传递给 do 表示法?

我目前正在通过映射数据列表来执行此操作,并且想知道是否可以以某种方式调用将结果作为一系列顺序调用传递?

Can you create a list of functions and then execute them sequentially, perhaps passing them into do notation?

I'm currently doing this by mapping over a list of data and am wondering if I can call somehow pass the result as a series of sequential calls?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

戒ㄋ 2024-09-01 04:33:47

到目前为止,答案很好,但如果您还希望每个函数不作用于原始数据,而是作用于前一个函数的结果,请查看折叠函数,例如foldl、foldl1和foldr:

fns = [(1-), (+2), (abs), (+1)]
helperFunction a f = f a
test1 n = foldl helperFunction n fns

并且您可能需要monadic版本、foldM 和 FoldM_ :

import Control.Monad
import Data.Char

helperFunction a f = f a
prnt = \s-> do putStrLn s; return s
actions = [return, prnt, return.reverse, prnt, return.(map toUpper), prnt, return.reverse, prnt] 
test2 str = foldM_ helperFunction str actions 

good answers so far, but if you also want each function to act not on the original data but on the result of the previous function, look at the foldding functions, such as foldl, foldl1, and foldr:

fns = [(1-), (+2), (abs), (+1)]
helperFunction a f = f a
test1 n = foldl helperFunction n fns

and you may need the monadic version, foldM and foldM_ :

import Control.Monad
import Data.Char

helperFunction a f = f a
prnt = \s-> do putStrLn s; return s
actions = [return, prnt, return.reverse, prnt, return.(map toUpper), prnt, return.reverse, prnt] 
test2 str = foldM_ helperFunction str actions 
GRAY°灰色天空 2024-09-01 04:33:46

像这样的东西吗?

sequence [putStrLn "Hello", putStrLn "World"]

Something like this?

sequence [putStrLn "Hello", putStrLn "World"]
夏雨凉 2024-09-01 04:33:46

如果这些是函数,即纯函数,那么您可以使用 ($) 或“apply”:

execute functions argument = map ($argument) functions
-- execute [id,(1+),(1-)] 12 => [12,13,-11]

当然不能保证这种情况会按顺序发生,但您将获得返回值的列表。

如果这些是动作,即不纯的,那么你想要的就是 sequence_

sequence_ [putStr "Hello", putStr " world", putStrLn "!"]

sequence_ 很容易自己编写:

sequence_ [] = return ()
sequence_ (action:actions) = action >> sequence_ actions

还有一个 sequence (不带下划线)运行一系列操作并返回其结果:

main = do
  ss <- sequence [readFile "foo.txt", readFile "bar.txt", readFile "baz.txt"]
  -- or ss <- mapM readFile ["foo.txt", "bar.txt", "baz.txt"]

If these are functions, ie pure, then you can use ($) or "apply":

execute functions argument = map ($argument) functions
-- execute [id,(1+),(1-)] 12 => [12,13,-11]

There's no guarantee that this happens sequentially of course, but you'll get a list of the return values.

If these are actions, ie impure, then what you want is called sequence_:

sequence_ [putStr "Hello", putStr " world", putStrLn "!"]

sequence_ is pretty easy to write yourself:

sequence_ [] = return ()
sequence_ (action:actions) = action >> sequence_ actions

There is also a sequence (without the underscore) that runs a bunch of actions and returns their results:

main = do
  ss <- sequence [readFile "foo.txt", readFile "bar.txt", readFile "baz.txt"]
  -- or ss <- mapM readFile ["foo.txt", "bar.txt", "baz.txt"]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文