[erlang-questions] stuck at a exercise

Bengt Kleberg bengt.kleberg@REDACTED
Mon Jan 26 15:39:05 CET 2015


The same function can have different clauses. These have the same name 
and arity, and are separated with ";"

Only after the last clause do you have ".".


bengt

On 01/26/2015 03:35 PM, Roelof Wobben wrote:
> Leonard Boyce schreef op 26-1-2015 om 15:24:
>> Hi Roelof,
>>
>> Welcome to Erlang.
>>
>> On Mon, Jan 26, 2015 at 9:09 AM, Roelof Wobben <r.wobben@REDACTED> wrote:
>>> Hello,
>>>
>>> At a book im following with self-study I have to do this exercise :
>>>
>>> Write a module boolean.erlthat takes logical expressions and Boolean 
>>> values
>>> (represented as the atoms  trueand  false) and returns their Boolean 
>>> result.
>>> The functions
>>> you write should include b_not/1, b_and/2, b_or/2, and b_nand/2. You 
>>> should
>>> not use
>>> the logical constructs  and,  or, and  not, but instead use pattern 
>>> matching
>>> to achieve your
>>> goal.
>>>
>>> So I tried the first clause and came with this :
>>>
>>> -module(boolean).
>>>
>>> -export([boolean/1]).
>>>
>>> b_not({true}) ->
>>>    false.
>>>
>>> But as soon as I compile it I see these error messages :
>>>
>>> boolean.erl:3: function boolean/1 undefined
>>> boolean.erl:5: Warning: function b_not/1 is unused
>>>
>>> How to solve these ?
>> I'm sure to get some hate for this, but I often find it easiest to
>> explain in imperative programming terms to people new to the language;
>>
>> module == class
>> public methods go in -exports([])
>> private methods are not exported and are only callable from within the
>> module itself
>>
>> In your case you'd want to export the function b_not/1
>>
>> Which would be callable as boolean:b_not(X)
>>
>> You do not export the module name, only functions you want to be 
>> callable.
>>
>> Leonard
>>
>>> Roelof
>>>
>>>
>>>
>>> _______________________________________________
>>> erlang-questions mailing list
>>> erlang-questions@REDACTED
>>> http://erlang.org/mailman/listinfo/erlang-questions
>
>
> Thanks
>
> Second problem .
>
> I added a second clause to it like this:
>
> -module(boolean).
>
> -export([b_not/1]).
>
> b_not(true) ->
>   false.
>
> b_not(false) ->
>   true.
>
> but now I see this message ;
>
> boolean.erl:8: function b_not/1 already defined
> error
>
> Which I do not understand because here the same approach is used :
>
> -module(shapes).
>
> -import(math, [sqrt/1]).
>
> -export([area/1]).
>
> area({square, Side}) ->
>   Side * Side ;
>
> area({circle, Radius}) ->
>   math:pi() * Radius * Radius;
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions




More information about the erlang-questions mailing list