haskell - 如何使函数具有不同数量的参数
问题描述
buyItem :: Hero -> Item -> Hero
buyItem (Mage n Wealth Gold) item1 = (Mage n Wealth item1)
buyItem (Warrior a item)
| enough (Warrior a c) >= cost item = (Warrior a c++item)
| otherwise = (Warrior a c)
buyItem (Mage a b c) item
| enough (Mage a b c) >= cost item = (Mage a b item)
| otherwise = (Mage a b c)
RPGdefs.hs:136:1: error:
Equations for ‘buyItem’ have different numbers of arguments
RPGdefs.hs:136:1-58
RPGdefs.hs:(137,1)-(139,31)
|
136 | buyItem (Mage n Wealth Gold) item1 = (Mage n Wealth item1)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...
Failed, no modules loaded.
解决方案
你这里有一个错字:
buyItem (Warrior a item)
应该是这样的:
buyItem (Warrior a) item
当您编写(Warrior a item)
时,这意味着它item
是 的一部分Warrior
,但从您的代码的其余部分来看,它看起来Warrior
应该是第一个参数,item
应该是第二个。这就是其他两种情况(带有 的情况Mage
)的编写方式。
推荐阅读
- mysql - 在 mySQL 表中搜索和删除孤立项
- python - AttributeError:“函数”对象没有属性“w”
- javascript - 如何在JS中对数字和字符串进行排序,同时保持字符串在前面的顺序和数字在后面的排序?
- android - 如何保存 Flutter Custom Scroll View 的滚动位置?
- php - 如何计算某个用户的按钮点击次数,然后显示计数?
- networking - 找到活动网络接口的 IP?
- json - 在 JQ 中选择包含在数组中
- c++ - 当一个库从草图文件夹中的 src 以外的目录包含到 arduino 代码中时出现错误未定义的引用
- elm - Elm 0.19 --优化和移植
- python - spacy 在 Windows 10 和 Python 3.5.3 上找不到模型'en_core_web_sm' :: Anaconda custom (64-bit)