erlang - exception error: no match of right hand side value [] -


i trying see if letter exists in list, doing following

sortedlist(text)-> freq(lists:sort(text)).  freq(list) -> freq (list, [], []).  freq(list, freq, checkedletters) when length(list) > 0 ->      [currhead|t]= list,     checked = lists:member(currhead,checkedletters),     case checked of             false -> checkedletters++[currhead],                     freq(t,freq++[{currhead,count(currhead, list)}],checkedletters);             true -> freq(t,freq,checkedletters)     end; freq([],freq,checkedletters)-> freq. 
  • list contains user entered letters

  • checkedletters empty list, keep track of examined letters

but receiving following erlang exception @ case-statement line (the lines **).

error: no match of right hand side value []

what problem here? been blindy staring @ lines.

the end condition missing in recursive function. when list become empty (each time recall function tail getting smaller), match [currhead|t]= list, fails error.

you have add clause manage end of recursion:

freq([], freq, checkedletters) when length(list) > 0 ->      {freq, checkedletters}; freq(list, freq, checkedletters) when length(list) > 0 ->      [currhead|t]= list,     checked = lists:member(currhead,checkedletters),     case checked of             true -> checkedletters++[currhead],                     freq(t,freq++[{currhead,count(currhead, list)}],checkedletters);             false -> freq(t,freq,checkedletters)     end; 

i think have review inside operation, doubt doing expect. in particular line checkedletters++[currhead], has no effect.

[edit] don't forget variables not mutable in erlang. line checkedletters++[currhead] evaluates new list , forget result (in fact not sure since not bound evaluation variable, , compiler knows it).

i guess want is:

case checked of         true ->              freq(t,freq++[{currhead,count(currhead, list)}],checkedletters++[currhead]);         false ->             freq(t,freq,checkedletters) end; 

next should have @ freq++[{currhead,count(currhead, list)}], think isn't correct.


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -