[erlang-questions] ** exception error: no function clause matching test_calculate:validate(["1", "+", "1"], []) (test_calculate.erl, line 11)

Roelof Wobben <>
Thu Oct 1 08:50:40 CEST 2015


Op 1-10-2015 om 08:41 schreef Richard A. O'Keefe:
> On 1/10/2015, at 7:15 pm, Roelof Wobben <> wrote:
>> One thing is not clear to me. What is the meaning of N. I see that N is equal to zero or to one.
>> %% is_number_token(String, N) is true if and only if
>> %% String is a list of ASCII decimal digit characters
>> %% and N + length(String) > 0.
> The comment was supposed to make it tolerably clear.
> I hope you *don't* see that, because it isn't true.
>
> Think of the original token as
>     Seen_Part ++ Unseen_Part
> In is_number_token(String, N),
>     String = Unseen_Part
>     N = length(Seen_Part)
>
> (Recall that the update was not 1 but N+1.  So N had to be
> counting *something*.)
>
> Here's what I might have done in Haskell:
>
> scan :: String -> Maybe [String]
> scan cs = if valid_tokens ts then Just ts else Nothing
>    where valid_tokens ts = all valid_token ts
>          valid_token t = t == "+" || not (null t) && all isDigit t
>
> It's easy to express universal quantification by a loop that
> checks each element of a list.  We _almost_ always want a
> universal quantification over an empty set to be true; this is
> one of the rare cases where we want it to be false and have to
> do something special to guard against it.
>
> The counting pattern is useful for identifiers:
>
> is_identifier_token(String) ->
>      is_identifier_token(String, 0).
>
> is_identifier_token([], N) ->
>      N > 0;
> is_identifier_token([C|Cs], N)
>    when C =< $9, C >= $0, N > 0 ->
>      is_identifier_token(Cs, N+1);
> is_identifier_token([C|Cs], N)
>    when (C bor 32) >= $a, (C bor 32) =< $z ->
>      is_identifier_token(Cs, N+1);
> is_identifier_token([$_|Cs], N)
>    when N > 0 ->
>      is_identifier_token(Cs, N+1);
> is_identifier_token(_, _) ->
>      false.
>
> This allows underscores and digits, but not at the beginning.
> And it doesn't allow empty identifiers.
>
>
>
>
> -----
> Geen virus gevonden in dit bericht.
> Gecontroleerd door AVG - www.avg.com
> Versie: 2015.0.6140 / Virusdatabase: 4419/10735 - datum van uitgifte: 10/01/15
>
>
>


Oke,

next task for me, is finding out how I can convert the strings that 
contain number to real numbers,
So "10" will become 10.

I think again iterate through the list , looking for strings that 
contain numbers and then do the math.

Roelof



More information about the erlang-questions mailing list