要修复解析错误,请从第一行删除 =。= 符号放在警卫之后。
Next, you should indent "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)
This will at least parse, yet it won't compile. The (checkNextStep pozxCrt pozyCrt+1 m)
should be (checkNextStep pozxCrt (pozyCrt+1) m)
.
Let me add that you can fix a lot of verbose code:
find (==E) cs == Nothing
can be changed to E `notElem` x
- You do not need to compare with True: change
x == True
to x
if x then True else False
can be changed to x
[x]++y
can be changed to x:y
- You can use pattern matching like this:
(pozxCrt, pozyCrt) = head m
or (pozxCrt, pozyCrt):_ = m
The result is:
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
You have a lot of repetition in the guards. A lot of repetition is a sign to create new functions.
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
Next step: use find canGo [E,S,W,N]
to get rid of the guards:
takeMeHere cs m =
case find canGo [E,S,W,N] of
Just dir -> (Just dir, move dir pos:m)
Nothing -> (Nothing, m)
where ...