Parse errors when using partial functions in where statments (Haskell) [closed]

我与影子孤独终老i 提交于 2020-02-06 19:04:58

问题


I'm pretty new to Haskell and I've run into this problem a few times and have had no luck finding answers online.

playDomsHandler (dp1,h1,s1) (dp2,h2,s2) b _
    -- players 1 and 2 both knocking, game ends
    | knockingP b h1 && knockingP b h2 = (b, (s1, s2))
    -- just player 1 knocking, player 2 makes a move
    | knockingP b h1 = playDomsHandler (dp1,h1,s1) (dp2,h2,nScore2) nBoard2 2
    -- just player 2 knocking, player 1 makes a move
    | knockingP b h2 = playDomsHandler (dp1,h1,nScore1) (dp2,h2,s2) nBoard1 1
    where 
      (nBoard1, nScore1) = nextPlay (dp1,h1,s1) b
      (nBoard2, nScore2) = nextPlay (dp2,h2,s2) b

Gives a parse error. (The first bracket in the (nBoard2, nScore2) line)

I've used where statements plenty before but I always seem to run into this problem when using partial functions

Any help much appreciated.


回答1:


I bet you are using tabs instead of white space for indentation.

The statements in the where construction must be aligned. Haskell interprets every tabs like 8 white space, so if you are messing with white space and tabs maybe your functions are not aligned.

If your editor is configured with tabs showing to 4 white space, you are not seeing this problem, but Haskell does.

I will use - for white space and \t por tabs (equal to 4 white space).

For example:

where 
----(nBoard1, nScore1) = nextPlay (dp1,h1,s1) b
\t  (nBoard2, nScore2) = nextPlay (dp2,h2,s2) b

These are not aligned, so Haskell will give parse error on (nBoard2, nScore2)

I recomend to use always white space instead of tabs. Most editors can be configured to replace tabs with 4 or 8 white space characters, so you will not get this problem.



来源:https://stackoverflow.com/questions/47354723/parse-errors-when-using-partial-functions-in-where-statments-haskell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!