[erlang-questions] Noob question about records

Rob Elsner <>
Sun Apr 26 03:48:27 CEST 2009


So I need to stop using gmail because I hit the wrong button all the
time and it sends an early email.

Sigh!

add(Money1, Money2) ->
       #money{currency=Cur1, amount=Amt1} = Money1,
       #money{currency=Cur2, amount=Amt2} = Money2,
       #money{currency=kre, amount=Amt1+Amt2}.

My bad for the previous email.

Rob

>> -module(money).
>> -export([add/2]).
>>
>> -record(money, { currency = usd, amount = 0 }).
>>
>> add(Money1, Money2) ->
>>        #money{currency=Cur1, amount=Amt1} = Money1.
>>        #money{currency=Cur2, amount=Amt2} = Money2.
>>        %% TODO: Check to see that the currencies are the same and throw
>> exception if not
>>        Total = Amt1 + Amt2.
>>        #money{currency=Cur1,amount=
>> Total}.
>>
>> The error I get is:
>> ./money.erl:9: syntax error before: '#'
>> ./money.erl:11: syntax error before: Total
>> ./money.erl:12: syntax error before: '#'
>>
>> The statement " #money{currency=Cur1, amount=Amt1} = Money1." seems to
>> work fine from the command line.
>>
>> Also, I tried a number of variants: using a guard for example in the
>> function header, but couldn't figure out the right syntax for two guards :
>> when record(Money1, money) ...?... (Money2, money).
>>
>> Also I tried using C1 = Money1#money.currency.  - but the next line caused a
>> syntax error.
>>
>> Finally, apologies if this gets double posted. I sent one about 6 hours ago
>> but i don't think my subscription was active then.
>>
>> _______________________________________________
>> erlang-questions mailing list
>> 
>> http://www.erlang.org/mailman/listinfo/erlang-questions
>>
>



More information about the erlang-questions mailing list