gpt4 book ai didi

haskell - Haskell 中嵌套 `do` block

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

我正在尝试在 Haskell 中编写一个函数,它检查一些内容,然后根据一些最小的用户输入进行递归。为了做到这一点,我想我必须使用 do block 。

cip :: [Argument] -> [Argument] -> Bool -> Bool -> IO()
cip (a:args) pargs burden gameover = do
let nasko = a:pargs
putStrLn (getPremise a)
let newgraph = Carneades.mkArgGraph nasko
let newcaes = (CAES (newgraph,audience2,assStandarts))
let answer = (acceptable (mkProp (getPremise a)) newcaes )
print answer
if(answer==True)
then (cip args nasko burden gameover)
else do
print "One of the arguments is not proved. Here are the premises that need proving"
print (propsForFixing newcaes a)
print "Let's see what you have for the first Propositon"
--add an if to check if no applicable arguments.
print (argumentScanHelp (head (propsForFixing newcaes a)) args)
print "\n Would you like me to apply the firt one? Y/N"
choice <- getLine
if(choice=="Y") then do print "applying the argument"
let applicabee = head (argumentScanHelp (head (propsForFixing newcaes a)) args)
print "Argument targeted"
let newargs = delete applicabee args
let newpargs = applicabee:nasko
print "Argument applied sucsessfuly. Recusing again"
(cip newargs newpargs burden gameover)
return()

光是看着它就让我眼睛受伤,但这就是你的do block 。第三个 do block 之前的所有内容都没有问题。但在这一行:

        if(choice=="Y") then do print "applying the argument"
let applicabee = head (argumentScanHelp (head (propsForFixing newcaes a)) args)

编译器开始哭泣:

Main.hs:209:73: parse error on input `let'

尝试了各种不同的缩进,但我似乎无法让它工作。我不想使用单独的函数,因为这意味着我必须不断传递大量参数。

谁能帮我把它弄对吗?另外,如果能解释一下嵌套 do block 的具体规范,我们将不胜感激。

最佳答案

我认为错误的原因是 if 表达式的误用。您可以像使用大多数命令式语言中存在的 if 语句一样使用它。简而言之,必须始终有一个 else

但是,在 do block 中“没有 else”是有意义的,类似于没有 else 的 if 语句。幸运的是,Control.Monad 模块将为您提供一个功能:

import Control.Monad (when)

(...)

when (choice=="Y") $ do print "applying the argument"
let applicabee = ...

您似乎已经以正确的方式使用嵌套 do block ,这很好,这基本上是您必须正确缩进。

PS。还要确保最后一个 return () 与代码的其余部分一样缩进! DS。

关于haskell - Haskell 中嵌套 `do` block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13694346/

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