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

Stefan Schmiedl <>
Wed Sep 30 19:02:00 CEST 2015


Roelof Wobben (30.09. 17:42):

> Hello,
> 
> I try to make a little parser which parse calculations.
> 
> So so far I have this code :
> 
> -module(test_calculate).
> 
> -export([validate/1]).
> 
> scan(String) ->
>      validate(string:tokens(String, " ")).
> 
> validate(String) ->
>      validate(String, []).
> 
> validate([Head | Tail], Validated_list) when Head >= $0 , Head =< $9 ->
>      validate(Tail, [Head | Validated_list]);
> 
> validate([Head | Tail], Validated_list) when Head =:= $+ ->
>      validate(Tail, [Head | Validated_list]);
> 
> validate([], Validated_list) ->
>      Validated_list.
> 
> test() ->
>      ["1","+","1"]    = scan("1 + 1"),
>      ["10", "+", "1"] = scan("10 + 1"),
>      ["1", "+", "10"] = scan("1 + 10"),
>      io:format("All tests are green ~n").
> 
> but as soon as I try :
> 
> test_calculate:validate(["1","+", "1"])

line 11 points probably to the first "validate" implementation.
So "validate" is missing a clause matching the input.

What is the input? It's a non-empty list, so the first two clauses
are still possible. Both are conditions for head.

What is the head of the list? The string "1".

First clause compares this string, i.e. list of integers, against a
single int, fail.

Second clause compares this list of integers against a single int, fail.

So you still need to parse the tokenized strings into "real" values.

s.


> 
> I see the above error
> 
> Someone a tip where things are going the wrong way ?
> 
> Roelof
> 
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions


More information about the erlang-questions mailing list