2

我对 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

我得到一个parse error on input "|". 如果我用 if then else if then 之类的东西编写代码......它可以工作。但我想使用 | 以获得更紧凑的编码。这里似乎有什么问题?

4

2 回答 2

7

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

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 ...
于 2012-04-15T11:34:18.713 回答
3

我可以在您的代码中看到至少三个错误。

  • =必须删除第一行的。语法要求=每个|. 基本上,错误是说您|在 an 之后第一个管道符号是意外的,=因为后者在该位置使用时没有防护。
  • 当您将数学表达式作为函数的输入时,您应该在数学表达式周围加上括号,因为像这样的中缀运算符+在函数应用方面具有较低的优先级。checkNextStep pozxCrt+1 pozyCrt m被评估为(checkNextStep pozxCrt) + (1 pozyCrt m)(这显然是一个错误),而不是checkNextStep (pozxCrt+1) pozyCrt m.
  • where应该相对于第一行缩进。

除非您省略的代码中有其他错误,否则它应该可以工作:

takeMeHere cs m    -- no more "=" here
    |(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 -- indentation
    pozxCrt=fst(head m)
    pozyCrt=snd(head m)

顺便说一句,您的代码非常多余,您应该对所有这些比较做一些事情True(请参阅@dbaupp 对您的问题的评论)。我建议你多学习一点 Haskell 的运算符优先级和语法,这将有助于你使你的代码更易于阅读:)

于 2012-04-15T11:23:38.303 回答