[erlang-questions] Noob question about records

ljw1001 ljw1001@REDACTED
Sun Apr 26 17:06:35 CEST 2009


Ah, now that's nice code. I'm clearly not thinking in Erlang yet. Is  
there any advantage to learning Haskell or another functional language  
first? I have Joe's book, but I haven't found anything to help me get  
the hang of functional programming specifically for Erlang.

Sent from my iPhone

On Apr 25, 2009, at 10:54 PM, Steve Vinoski <vinoski@REDACTED> wrote:

> On 4/25/09, Larry White <ljw1001@REDACTED> wrote:
>> -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}.
>
> Note that you don't need to check the currencies and throw an
> exception in the manner your comment suggests. Do this instead:
>
> add(#money{currency = C, amount = Amt1}, #money{currency = C, amount  
> = Amt2}) ->
>    #money{currency = C, amount = Amt1 + Amt2}.
>
> Let pattern matching do the work for you.
>
> --steve



More information about the erlang-questions mailing list