如何在map函数中使用lambda和Either?

huangapple go评论87阅读模式
英文:

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 -&gt; x + 1) [1, 2]

... and I would expect it to return

[2, 3]

However, I get the following error:

&lt;interactive&gt;: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 -&gt; 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) -&gt; x + 1) [Left 1, Left 2]

huangapple
  • 本文由 发表于 2023年4月13日 14:39:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76002362.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定