gpt4 book ai didi

haskell - 获取数据并将其附加到列表中直至完成

转载 作者:行者123 更新时间:2023-12-02 14:56:13 26 4
gpt4 key购买 nike

我正在使用 Commit 类型的 hit 包。我有下一个函数,但我希望它返回一个包含来自 loopTillEmpty 函数的所有 ref 的列表,而不仅仅是打印它们。这可能吗?

getRefs revision git = do
ref <- maybe (error "revision cannot be found") id <$> resolveRevision git revision
loopTillEmpty ref
where loopTillEmpty ref = do
commit <- getCommit git ref
putStrLn $ show ref
case commitParents commit of
[] -> return ()
(p:_) -> loopTillEmpty p

示例输出为

5cecfbca98fbc4b02b06ea315a857c97605ef135
e6e4a017be111eba85baad539c502bae2dacb14e
()

这向我们展示了引用,当空时它返回()。数据类型如下所示:

Commit {commitParents = []} -- when empty
Commit {commitParents = [e6e4a017be111eba85baade]} -- when not empty

谢谢。

最佳答案

现在,当您访问它们时,您将在输出中打印引用,然后循环。您需要的是积累这些引用而不是打印它们,这对于列表来说是完美的任务。重要的部分:

getRefs :: Revision -> Git -> IO [Ref]
getRefs revision git = do
ref <- maybe (error "revision cannot be found") id <$> resolveRevision git revision
loopTillEmpty ref
where loopTillEmpty ref = do
commit <- getCommit git ref
nextRefs <- case commitParents commit of
[] -> return []
(p:_) -> loopTillEmpty p
return $ ref : nextRefs

因此,当当前引用之后没有更多引用时,您的基本情况是 return [] ,而在另一种情况下是 loopTillEmpty p 。最后一行将当前 refnextRefs 组合到结果列表中。然后,您可以在 getRefs 的结果上map show 来获取提交字符串列表。

关于haskell - 获取数据并将其附加到列表中直至完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31382532/

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