2017-09-06 104 views
3

的 「圖案解析錯誤」 這是我的代碼:哈斯克爾:哪裏是

connected :: [(Integer,Integer)] -> Bool 
connected [] = True 
connected [(_,_)] = True 
connected (a,b):(c,d):xs 
       | a > c  = False 
       |otherwise = connected (c,d):xs 

當我加載它GHCI它顯示

error: parse error in pattern: connected

哪兒了我犯了一個錯誤?

+0

小調注意:'foo | x = False |否則= something'是(IMO)更常被寫爲'foo = not x && something'。在你的情況下,你可以使用'connected(...)= a <= c && connected(...)'。 – chi

回答

7

您需要在兩個地方周圍添加你的缺點表達式括號:

connected :: [(Integer,Integer)] -> Bool 
connected [] = True 
connected [(_,_)] = True 
connected ((a,b):(c,d):xs)       -- (2) 
       | a > c  = False 
       | otherwise = connected ((c,d):xs) -- (1) 
  1. 功能應用結合更加緊密地比中綴運算符,所以connected (c,d) : xs被解析爲(connected (c,d)) : xs

  2. 類似的事情發生在模式表達式中。雖然你得到的無用的錯誤信息是相當不幸的。

固執己見側面說明:我建議總是寫綴運營商與周圍空間(例如,a : b代替a:b),因爲我覺得忽略空白巧妙地暗示了運營商結合更加緊密比它確實做到了。