You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

模式匹配代码出现解析错误,请求排查原因

问题解析与修复

你的代码存在两个核心语法错误,导致解析失败:

1. 类型签名的位置与格式错误

Haskell 中函数的类型签名必须放在函数定义之前,且要明确输入输出类型。你将 :: Transaction 写在模式匹配之后,不仅位置错误,也未声明函数的返回值类型。正确的类型签名应为 transaction_to_string :: Transaction -> String,表示该函数接收一个 Transaction 类型参数,返回字符串。

2. 元组模式匹配的语法错误

你定义的 Transaction 是元组类型 (Char, Int, Int, String, Int),但误用了列表的 cons 运算符 : 来解构参数。元组的模式匹配需要用逗号分隔元素,并整体包裹在括号中。

修复后的代码

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

额外优化:stock 本身就是 String 类型,无需使用 show 函数,直接拼接即可。

内容的提问来源于stack exchange,提问作者Big Man

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 10:35:34