Haskell下划线与显式变量 [英] Haskell underscore vs. explicit variable
问题描述
我已经学习Haskell几周了,我对使用下划线(_
)作为函数参数有疑问.我想通过一个具体的例子可以更好地提出我的问题.假设我想定义一个函数,该函数根据提供的索引提取列表的元素,是的,我知道(!!)
已经预先定义了.我可以通过以下两种方法定义函数(我敢肯定还有更多方法):
I've been learning Haskell for a few weeks, and I have a question about the use of the underscore (_
) as a function parameter. I think my question will be better asked with a specific example. Let's say I want to define a function that extracts the element of a list based on the provided index—yes, I realize (!!)
is already pre-defined. The two ways that I can define the function (I'm sure there are more) are the following:
版本1
indexedElement :: [a] -> Int -> a
indexedElement xs n | n < 0 = error "Index can not be negative."
indexedElement [] n = error "Index must be smaller than the length of the list."
indexedElement (x:xs) 0 = x
indexedElement (x:xs) n = indexedElement xs (n - 1)
版本2
indexedElement :: [a] -> Int -> a
indexedElement _ n | n < 0 = error "Index can not be negative."
indexedElement [] _ = error "Index must be smaller than the length of the list."
indexedElement (x:_) 0 = x
indexedElement (_:xs) n = indexedElement xs (n - 1)
两个版本显然非常相似.两者之间的唯一区别是使用显式变量或下划线.对我来说,_
意味着实际上可以在其中写入任何内容,而像n
这样的显式变量使该参数必须是整数更加明显.因此,我更喜欢版本1.但是(!!)
的GHC源代码的编写方式与第2版类似.第二版在功能上有优势吗?如果不是这样,核心" Haskell程序员是否会对版本1产生疑问?我了解使用一致的代码编写方式的重要性,因此我尝试遵循未编写的规则"来进行特定语言的编程.这是一个示例,尽管我更喜欢第一个版本,但我认为它不会使代码更难阅读.我不知道这是由于我的纯数学背景还是什么原因引起的,但我想听听您这些经验更丰富的Haskell兽医的想法.
The two versions are obviously very similar. The only difference between the two is the use of an explicit variable or an underscore. To me _
means that literally anything can be written there while an explicit variable like n
makes it more obvious that the argument must be an integer. For that reason, I prefer Version 1; but the GHC source code for (!!)
is written like Version 2. Is there a functional advantage of the second version? If not, would "hardcore" Haskell programmers take issue with Version 1? I understand the importance of having a consistent way of writing code, so I try to follow the "unwritten rules" for programming in a particular language. This is an example where I much prefer the first version though, and I don't think it makes the code any more difficult to read. I don't know if it's due to my background in pure math or what, but I'd like to hear what you more-seasoned-Haskell vets think.
推荐答案
第二个版本有功能上的优势吗?
Is there a functional advantage of the second version?
我认为它们在操作上没有任何区别.但是我发现第二个版本更具可读性. _
表示根本没有使用它.因此,在阅读代码时,我可以忽略它,而只专注于其他参数.而在第一个版本中,我会认为已经定义了n
,但是作者可能忘记使用它了?或者也许不需要该参数.第二个版本只是避免了这种精神上的负担.但这只是我的意见. :)
I don't think they have any operational difference. But I find the second version more readable. _
indicates that it isn't used at all. So while reading the code, I can just ignore it and just concentrate on the other parameters. Whereas in the first version, I will be thinking that n
is defined but maybe the author forgot to use it ? Or maybe the argument isn't required. The second version just avoids this kind of mental overload. But this is just my opinion. :)
实际上,如果启用警告标志(-Wall
)并编译代码,它将为您的第一个版本引发警告:
In fact, if you enable the warning flag (-Wall
) and compile your code, it will throw warning for your first version:
[1 of 1] Compiling Main ( code.hs, code.o )
code.hs:2:16: Warning: Defined but not used: ‘xs’
code.hs:3:19: Warning: Defined but not used: ‘n’
code.hs:4:19: Warning: Defined but not used: ‘xs’
code.hs:5:17: Warning: Defined but not used: ‘x’
code.hs:8:17: Warning: Defined but not used: ‘xs’
这篇关于Haskell下划线与显式变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!