英文:
How to use a lamdba with Either in a map function?
问题
以下是翻译好的部分:
我尝试做的是以下操作:
map (\x -> x + 1) [1, 2]
... 我期望它返回
[2, 3]
然而,我得到了以下错误:
<interactive>:25:14: 错误:输入上的解析错误 ‘)’
我在这里做错了什么?
英文:
What I try to do is the following:
map ((\Left x -> x + 1) [1, 2]
... and I would expect it to return
[2, 3]
However, I get the following error:
<interactive>:25:14: error: parse error on input ‘)’
What am I doing wrong here?
答案1
得分: 4
你的括号排列不正确,并且在与其他代码无关的情况下引入了 Either。
请写成:
map (\x -> x + 1) [1, 2]
不清楚为什么你希望使用 Either,因为你正在对一个没有任何 Either 值的列表进行映射。也许你想要的是:
map (\(Left x) -> x + 1) [Left 1, Left 2]
英文:
You have incorrectly organized your parentheses, and introduced Either when it's not related to any of the other code.
Write
map (\ x -> x + 1) [1, 2]
It's not clear why you want Either to be involved, since you're mapping over a list without any Either values in it. Maybe what you want is
map (\ (Left x) -> x + 1) [Left 1, Left 2]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论