Home > Software engineering >  Why am I getting problems with pattern matching?
Why am I getting problems with pattern matching?

Time:11-23

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
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

CodePudding user response:

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)
  • Related