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

Ruan Jonker <>
Wed Sep 30 18:19:00 CEST 2015


Hi,

Your validate function seems to expect a list of chars, but it is in fact
getting a list of lists (result of string:tokens). So what's happening is
that in the first clause you are comparing a list to an integer, and in the
second you are asserting the type =:= as well (which will not match).

Rather have a look @ Sean Cribbs' neotoma (github) than trying to roll your
own.

R
On 30 Sep 2015 5:50 pm, "Roelof Wobben" <> wrote:

> 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"])
>
> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150930/7008d5c0/attachment.html>


More information about the erlang-questions mailing list