haskelltuplespattern-matchingparse-errorcustom-data-type

Why am I getting problems with pattern matching?


I am getting a parse error in my code and I don't know why. I can't see any problems with the code as it's the same syntax as pattern matching I have used in the past.

My code:

transaction_to_string (sob : unit : price : stock : day) :: Transaction
    | sob == 'S'   = "Sold " ++ (show unit) ++ " units of " ++ (show stock) ++ " for " ++ (show price) ++ " pounds each on day " ++ (show day)
    | sob == 'B'   = "Bought " ++ (show unit) ++ " units of " ++ (show stock) ++ " for " ++ (show price) ++ " pounds each on day " ++ (show day)

Where Transaction is a custom data type - Transaction = (Char, Int, Int, String, Int)

Error:

Parse error in pattern: transaction_to_string
   |
23 | transaction_to_string (sob : unit : price : stock : day) :: Transaction
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Solution

  • There are several syntax errors in your code example. try this instead:

    type Transaction = (Char, Int, Int, String, Int)
    transaction_to_string :: Transaction -> String
    transaction_to_string (sob, unit, price, stock, day)
        | sob == 'S'   = "Sold" ++ (show unit) ++ " units of " ++ (show stock) ++ " for " ++ (show price) ++ " pounds each on day " ++ (show day)
        | sob == 'B'   = "Bought " ++ (show unit) ++ " units of " ++ (show stock) ++ " for " ++ (show price) ++ " pounds each on day " ++ (show day)