要修复解析错误,请从第一行删除=。 =号被放在守卫之后。
接下来,你应该缩进“其中”
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
- 你并不需要真正的比较:改变
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 ...
你不需要'== TRUE'不断:如果事情是真实的,然后检查,这是真正的没有按”让它更真实。因为'if'中的条件已经是'Bool',所以你也不需要'if ... then True else False'。 – huon 2012-04-15 11:29:15