I have a problem with a Haskell code, I have the following:
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
I get a parse error on input "|" . If I write the code with something like if then else if then…it works. But I want to use the | for a more compact coding.What seems to be the problem here ?
To fix parsing error, remove = from first line. The = sign is put after the guards.
Next, you should indent “where”
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 == Nothingcan be changed toE `notElem` xx == Truetoxif x then True else Falsecan be changed tox[x]++ycan be changed tox:y(pozxCrt, pozyCrt) = head mor(pozxCrt, pozyCrt):_ = mThe result is:
You have a lot of repetition in the guards. A lot of repetition is a sign to create new functions.
Next step: use
find canGo [E,S,W,N]to get rid of the guards: