gpt4 book ai didi

Haskell 条件错误

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

我的 Haskell 代码有问题,我有以下问题:

takeMeHere cs m =
|(find (==E) cs == Nothing && (checkNextStep pozxCrt+1 pozyCrt m) == True) = (Just E,[(pozxCrt+1,pozyCrt)] ++ m)
|(find (==S) cs == Nothing && (checkNextStep pozxCrt pozyCrt-1 m) == True) = (Just S,[(pozxCrt,pozyCrt-1)] ++ m)
|(find (==W) cs == Nothing && (checkNextStep pozxCrt-1 pozyCrt m) == True) = (Just W,[(pozxCrt-1,pozyCrt)] ++ m)
|(find (==N) cs == Nothing && (checkNextStep pozxCrt pozyCrt+1 m) == True) = (Just N,[(pozxCrt,pozyCrt+1)] ++ m)
|otherwise = (Nothing,m)
where
pozxCrt=fst(head m)
pozyCrt=snd(head m)

checkNextStep x y m = if(find (== (x,y)) m == Nothing) then True
else False

我在输入“|”时遇到解析错误。如果我用 if then else if then 之类的东西编写代码......它就有效。但我想用 |以获得更紧凑的编码。这里似乎有什么问题?

最佳答案

要修复解析错误,请从第一行删除=。 = 符号放在守卫后面。

接下来,您应该缩进“where”

takeMeHere cs m
|(find (==E) cs == Nothing && (checkNextStep pozxCrt+1 pozyCrt m) == True) = (Just E,[(pozxCrt+1,pozyCrt)] ++ m)
|(find (==S) cs == Nothing && (checkNextStep pozxCrt pozyCrt-1 m) == True) = (Just S,[(pozxCrt,pozyCrt-1)] ++ m)
|(find (==W) cs == Nothing && (checkNextStep pozxCrt-1 pozyCrt m) == True) = (Just W,[(pozxCrt-1,pozyCrt)] ++ m)
|(find (==N) cs == Nothing && (checkNextStep pozxCrt pozyCrt+1 m) == True) = (Just N,[(pozxCrt,pozyCrt+1)] ++ m)
|otherwise = (Nothing,m)
where
pozxCrt=fst(head m)
pozyCrt=snd(head m)

这至少会解析,但不会编译。 (checkNextStep pozxCrt pozyCrt+1 m) 应为 (checkNextStep pozxCrt (pozyCrt+1) m)

让我补充一下,您可以修复很多冗长的代码:

  • find (==E) cs == Nothing 可以更改为 E `notElem` x
  • 您不需要与 True 进行比较:将 x == True 更改为 x
  • if x then True else False 可以更改为 x
  • [x]++y 可以更改为 x:y
  • 您可以使用如下模式匹配:(pozxCrt, pozyCrt) = head m(pozxCrt, pozyCrt):_ = m

结果是:

takeMeHere cs m                                                                 
| E `notElem` cs && checkNextStep (pozxCrt+1) pozyCrt m = (Just E,(pozxCrt+1,pozyCrt):m)
| S `notElem` cs && checkNextStep pozxCrt (pozyCrt-1) m = (Just S,(pozxCrt,pozyCrt-1):m)
| W `notElem` cs && checkNextStep (pozxCrt-1) pozyCrt m = (Just W,(pozxCrt-1,pozyCrt):m)
| N `notElem` cs && checkNextStep pozxCrt (pozyCrt+1) m = (Just N,(pozxCrt,pozyCrt+1):m)
| otherwise = (Nothing,m)
where
(pozxCrt, pozyCrt) = head m

checkNextStep x y m = (x,y) `notElem` m

守卫中有很多重复的地方。大量重复是创造新功能的标志。

move E (x, y) = (x+1, y) 
move S (x, y) = (x, y-1)
move N (x, y) = (x, y+1)
move W (x, y) = (x-1, y)

takeMeHere cs m
| canGo E = go E
| canGo S = go S
| canGo W = go W
| canGo N = go N
| otherwise = (Nothing,m)
where
pos = head m
canGo dir = dir `notElem` cs && checkNextStep (move dir pos) m
go dir = (Just dir, move dir pos:m)

checkNextStep (x, y) m = (x,y) `notElem` m

下一步:使用find canGo [E,S,W,N]摆脱守卫:

 takeMeHere cs m =                                                               
case find canGo [E,S,W,N] of
Just dir -> (Just dir, move dir pos:m)
Nothing -> (Nothing, m)
where ...

关于Haskell 条件错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10161509/

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