对于Haskell,我真的很新,我需要返回一个“已修改”输入函数的函数。如何根据输入返回调用另一个函数的函数?
我想你不能复制和修改原始函数(基于某些条件),所以你必须直接实现自己的行为,然后调用原始函数?
这是我的方法:
switchFirstEgg eggCarton = if eggCarton 1 == 0
then switchedCarton where switchedCarton position = if position == 1 then 2 else eggCarton position
else if eggCarton 1 == 1
then switchedCarton where switchedCarton position = if position == 1 then 0 else eggCarton position
else if eggCarton 1 == 2
then switchedCarton where switchedCarton position = if position == 1 then 1 else eggCarton position
else switchedCarton where switchedCarton position = eggCarton position
我从GHCI得到的错误是
哈斯克尔/ eggcartons.hs:42:54:输入解析错误 '其中'
哪个指向第一个字之后的第一个字where
。
(参考:我也想在这里设置多个支架http://pastebin.com/2wTqAqpm,我试图与警卫http://pastebin.com/RVm28Y7n这样做,但这只是使事情变得更糟而不会操作装配理解,至少警卫工作对我来说这里http://pastebin.com/uQeFwLU5?)
我搜索了Haskell中的函数,但我只有几个随机信息,我使用了我所做的where
事情。
我的理念对吗?这只是一个小错误吗?
任何帮助进一步阅读有关返回函数的语法也非常感谢!
我不同意用'ec'更换'eggCarton'和'sc'的'switchedCarton'有助于提高可读性。否则,这是一个很好的答案。 – WolfeFan
@WolfeFan:好吧,在这种情况下,原始版本很难阅读,因为它的宽度很宽。将变量缩写为两个字母当然不总是一个好主意,但是任何经常出现八次的名字都应该保持较短(或者至少在本地被别名为短)。 – leftaroundabout
很好的答案。请注意,如果'pos'为'1'且'ec 1'为'3',最后一个答案将会崩溃,这与以前的替代方法不同,因为这种情况并非详尽无遗。当然,可以在案件中添加'_ - > ec pos'来防止这种情况发生。 – chi