模式中的解析错误:?

时间:2017-11-24 06:20:42

标签: haskell compiler-errors functional-programming

我是Haskell的新手,并尝试构建简单的billing system for a Fastfood stall。我定义了linkItem function(在代码的最后部分),它在整个列表中运行,就 本地函数linkBill,它只对一个元素进行操作。 linkBill寻找 匹配的代码,返回相关的帐单类型,或导致运行时错误 如果itemDb中没有此类代码条目。

import Data.List
import Data.Char

type Name = String
type Code = Int
type Price = Float
type ToatalQtity = Int

type Db = [(Code,Name,Price,ToatalQtity)]

itemDb :: Db
itemDb = [(1234,"Chicken Lollypop",250.00,50),
          (2222,"Chili Mushroom",150.00,50),
          (1111,"Paneer Pakora",100.00,100),
          (4719,"Fish Fingers",121.00,100),
          (1235,("Tandoori Chicken",200.00,50))]

type ItemCode = [Code]
type BillType = [(Name,Price)]

till :: ItemCode
till = [1234,2222,1111,4719,1235]

lookupBy :: (a -> Maybe b) -> [a] -> Maybe b
lookupBy f [] = Nothing
lookupBy f (x:xs) = case f x of
                  Nothing -> lookupBy f xs
                  r       -> r

linkItem :: ItemCode -> BillType
linkItem = map linkBill 
         where linkBill code' = case lookupBy (matchingCode code') itemDb of 
                                Just item -> item 
                                Nothing -> Error "**WRONG CODE, NO ITEMFOUND**"
                                matchingCode (code') (code,name,price,_) 
                                                          | (code' == code) = Just (name,price)
                                                          | otherwise = Nothing

每当我编译它时,它都显示如下错误:

billing.hs:48:34: error: Parse error in pattern: matchingCode
   |
48 |       matchingCode (code') (code,name,price)     
   |       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                                                | (code' == code) = Just (name,price)
   Failed, 0 modules loaded

我认为我在matchingCode函数声明做错了我不知道是什么?我有什么可以摆脱这些错误?

1 个答案:

答案 0 :(得分:2)

缩进where内部函数定义的代码与缩进的级别相同:

linkItem :: ItemCode -> BillType
linkItem = map linkBill where
  linkBill code' = case lookupBy (matchingCode code') itemDb of 
    Just item -> item 
    Nothing   -> error "**WRONG CODE, NO ITEMFOUND**"
  matchingCode code' (code, name, price, _) | code' == code = Just (name, price)
                                            | otherwise     = Nothing

您还需要error而不是Error(假设您确实想要使用error进行错误处理)。