在Haskell中的标识符中的撇号 [英] Apostrophe in identifiers in Haskell

查看:165
本文介绍了在Haskell中的标识符中的撇号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在互联网上发现这个程式码:

  digits 0 = [0] 
digits n = digits 'n []
其中数字'0 ds = ds
数字'n ds = let(q,r)= quotRem n 10
在数字'q(r:ds)

sumOfDigits = sum。有人可以快速解释什么是'符号( digits n = 0)。 digits'n [] )后递归函数调用是为?我看过一些其他的代码示例在Haskell(教程),但我不明白这一个。欢迎快速解释。

解决方案

撇号只是名称的一部分。它是Haskell中采用的命名约定(惯用法)。



Haskell中的约定是,在数学中,变量名称上的撇号表示与某个以前的变量相关或类似的变量。



例如:

  let x = 1 
x'= x * 2
in x'

x' x 相关,我们用撇号表示。






您可以在GHCi中运行,

  Prelude> :{
Prelude | let x = 1
Prelude | x'= x * 2
Prelude | in x'
Prelude | :}
2


I found this code snipped on the internet:

digits 0 = [0]
digits n = digits' n []
  where digits' 0 ds = ds
        digits' n ds = let (q,r) = quotRem n 10
                       in digits' q (r:ds)

sumOfDigits = sum . digits

Can someone quickly explain what the " ' " sign ( digits n = digits' n [] ) after the recursive function call is for? I've seen some other code examples in Haskell (tutorials), but im not understandig this one. A quick explanation is appreciated.

解决方案

The apostrophe is just part of the name. It is a naming convention (idiom) adopted in Haskell.

The convention in Haskell is that, like in math, the apostrophe on a variable name represents a variable that is somehow related, or similar, to a prior variable.

An example:

let x  = 1
    x' = x * 2
in x'

x' is related to x, and we indicate that with the apostrophe.


You can run this in GHCi, by the way,

Prelude> :{ 
Prelude| let x  = 1
Prelude|     x' = x * 2
Prelude| in x'
Prelude| :}
2

这篇关于在Haskell中的标识符中的撇号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆