Erlang/OTP Forums

Author Message

<  Erlang  ~  (noob) defining a constant value.

anupamkapoor
Posted: Wed Sep 05, 2007 3:34 pm Reply with quote
Joined: 05 Sep 2007 Posts: 7
hi all,

i have just started looking at erlang and kind of experimenting with toy programs.

i am having a slight problem while defining constant values e.g. i would like to say in (some math_constants) file things like:

PI = 3.14159265358979
E = 2.7182818284590
PHI = 1.6180
g = 0.577215664901

etc. how do i say this in erlang ?

for example, here is a trivial area calculation example:

,----
| -module (area).
| -export ([shape_area/1]).
|
| %% compute the area of simple shapes
| PI = 3.14159265.
|
| shape_area({circle, R}) ->
| PI * R * R.
`----

this gives me the following error:

8> c("/home/anupam/source-code/erlang/area", [{outdir, "/home/anupam/source-code/erlang/"}]).
/home/anupam/source-code/erlang/area.erl:5: syntax error before: PI
/home/anupam/source-code/erlang/area.erl:8: variable 'PI' is unbound
error

any help is greatly appreciated !

thanks
anupam

PS: i have defined a macro of sorts for this, but it just feels *too* perverse.
View user's profile Send private message
michal
Posted: Wed Sep 05, 2007 5:17 pm Reply with quote
User Joined: 20 Jul 2006 Posts: 44 Location: London
Hi,
Macro is the way to go. You probably want to define it in some header file like this:
Code:

-define(PI, 3.14159265358979).

and then include the header file in every module which has code that references to it.

http://www.erlang.org/doc/reference_manual/macros.html

Michal

_________________
http://www.erlang-consulting.com
View user's profile Send private message
anupamkapoor
Posted: Wed Sep 05, 2007 5:26 pm Reply with quote
Joined: 05 Sep 2007 Posts: 7
michal wrote:

Macro is the way to go.....

ah ok ! i was almost certain that i was missing something with erlang being single-assignment and all.

is there some way to avoid macros at all for something so trivial ?

thanks
anupam
View user's profile Send private message
michal
Posted: Wed Sep 05, 2007 5:53 pm Reply with quote
User Joined: 20 Jul 2006 Posts: 44 Location: London
You could define a function in some module that will take no arguments and just return a constant. I.e. when you call math:pi(). it returns a float.

But why would you like to avoid macros? Maybe term 'macro' sounds like dedicated for some more complicated definitions, but it is widely used for both macros and constants.

Michal

_________________
http://www.erlang-consulting.com
View user's profile Send private message
anupamkapoor
Posted: Wed Sep 05, 2007 6:04 pm Reply with quote
Joined: 05 Sep 2007 Posts: 7
michal wrote:

But why would you like to avoid macros? Maybe term 'macro' sounds like dedicated for some more complicated definitions, but it is widely used for both macros and constants.

Michal

well, i am not very conversant with erlang macros yet. are they akin to c pre-processor macros or more like lisp ?

thanks
anupam
View user's profile Send private message
KatolaZ
Posted: Thu Sep 06, 2007 10:28 am Reply with quote
Joined: 04 Jul 2007 Posts: 5
Macros in Erlang does not have anything to do with macros in Lisp. They don't add additional constructs to the language, as Lisp macros do, but define "shortcuts", as C preprocessor directives. Erlang macros also preserve a C-like syntax (except for the fact that -define(). is used, instead of #define... Smile ). For any further explanation see the Erlang Reference Manual, section 7.

HND

Enzo
View user's profile Send private message
wuji
Posted: Thu Aug 30, 2012 7:05 am Reply with quote
User Joined: 10 Aug 2012 Posts: 654
sk of eradicating it becomes no less daunting than expunging expunging cheap Ralph Lauren expunging false or hurtful information posted about you by others.Your
becomes part of what author Matt Ivester calls "your permanent permanent cheap polo shirts permanent record." His book "lol…OMG!" tells college students and
how to undo (or to avoid making) mistakes they may may cheap polo shirts may later regret—embarrassments, say, that do permanent damage their employment
trying to do damage control, Ivester and others say, should should [h1]cheap replica *beep*[/h1] should consider the following steps: 'Superstitious Fund'
Money in Month TwoFund That Trades on Superstitions Down 9.5 9.5 [h4]replica designer bags for sale[/h4] 9.5 Percent After Two MonthsBy SUSANNA KIMJuly 31, 2012— If
invested in stocks purely based on superstitions like avoiding Friday Friday cheap jordan shoes Friday the 13th or lucky numbers, would you end up
View user's profile Send private message
dongdongwu
Posted: Thu Sep 20, 2012 2:09 am Reply with quote
User Joined: 19 Sep 2012 Posts: 236
His good friend Diane said: "Christian Louboutin Men Shoes was a magician, he make shoes is immediately put his female people with legs and advantage. He understands women wanted to do and can make them into beautiful Cinderella." Madonna often in its concert wearing Christian louboutin high heels , and some famous superstar like Angelina jolie, mariah Carey, beyonce Knowles, the famous Japanese singer YaYouMei Hamasaki helps Christian Louboutin Men Shoes set up its powerful position. The youngest customers will count Tom cruise's daughter sully cruz. Louboutin made for only a pair of handmade Christian Louboutin high heel Shoes! Want to be more fashion? Put on Christian Louboutin Outlet !
Candy colors of the chalaza high-heeled shoes with lolita type allure, set full finely gem blue "neon shoes" is to need to use "sexy" to describe. Each pair are worth careful appreciation of lithe and graceful fairy ludaoli, what kind of most let you move?Christian Louboutin Men Shoes that one brush red is always cannot resist the temptation, Christian Louboutin men outlet continue to use the days of high 8cm above slender heel proclaim the sexy and luxuriant. The bowknot on black pointed high-heeled shoes with sharp rivet concomitant, wild python met enchanting color printing grain, It is that pairs of high-heeled shoes lets Carrie more feminine flavour. Like Christian Louboutin for men her word.
View user's profile Send private message

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum