How to name Haskell variables which in physics are uppercase

﹥>﹥吖頭↗ 提交于 2019-12-01 00:16:34

问题


Variable names in haskell need to be in small case, but

How to declare variables in .hs file, if we want to store g = 9.8 and G = 6.67300 × 10-11 (in following scenario) ?

Conventionally Physicists mention :

(1) Acceleration due to gravity on earth

g = 9.8 m/sec^2

(2) Universal Gravitational constant

G = 6.67300 × 10-11 m3 kg-1 s-2


回答1:


You will just have to come up with another name. The distinction between names starting with upper- and lowercase letters is part of the syntax.

While this may be unfortunate in your case, it's a design trade-off. In order to simplify differentiating between different things (e.g. between variables and constructors), identifiers starting with lowercase letters and ones starting with uppercase letters are fundamentally different.




回答2:


You could give them meaningful names. Just because mathematicians and physicists historically liked to use inscrutable symbols with no way of interpreting them other than rote memorisation doesn't mean you have to. We're typing now, not writing long-hand, so defining equations in terms of earthGravity and gravitationalConstant isn't so much harder to write than in terms of g and G, and it's a hell of a lot easier to read!

Or, you could designate that all identifiers prefixed with something like c represent standard well known constants, and use cg and cG.




回答3:


I would simply use g and _G. I think the underscore isn't too distracting.



来源:https://stackoverflow.com/questions/11219304/how-to-name-haskell-variables-which-in-physics-are-uppercase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!