我尝试通过比较map的值和给定的字符串输入来打印键(输入在下面尝试执行的示例输出中给出)。
看下面的代码我试过的代码:
-module(main).
-compile(export_all).
get_code_for_words(Sentence) ->
Words = string:split(Sentence, "\s", all),
LettersWords = # { "a" => ".- ","b" =>"-... ","c" =>"-.-. ","d" => "-.. ","e" => ". ","f" => "..-. ","g" => "--. ","h" =>".... ","i" =>".. ","j" =>".--- ","k" =>"-.- ","l" =>".-.. ","m" =>"-- ","n" =>"-. ","o" => "--- ","p" =>".--. ","q" =>"--.- ","r" =>".-. ","s" =>"... ","t" =>"- ","u" =>"..- ","v" =>"...- ","w" =>".-- ","x" =>"-..- ","y" =>"-.--","z" =>"--.. "},
Result = get_codes(Words, LettersWords, _AllCodes=[]),
io:format("~p~n", [Result]).
get_codes([Word|Words], Map, AllCodes) ->
NewAllCodes = maps:fold(fun(K,V,Acc) ->
%io:format("~p~n",[Acc]),
case V =:= Word of
true -> [K|Acc];
_ -> Acc
end
end,
AllCodes,
Map),
get_codes(Words, Map, NewAllCodes);
get_codes([], _Map, AllCodes) ->
lists:reverse(AllCodes).
当我执行代码时,输出如下:
**
9> c(main).
main.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,main}
10> main:get_code_for_words(".... ..").
[]
ok
**
请指点我好吗?
1条答案
按热度按时间xsuvu9jc1#
Let us first reformat your code (with the help of
rebar3_format
):Now, let's isolate the map so I don't have to copy it around all the time and, since this doesn't look like an escript, let's just remove the call to
io:format/2
for the result…Now, let's talk about
get_codes/3
… Since that function is using the map above, I'll assume that you when you sayWord
, you actually meanCharacter
(since otherwise you'll have to go char-by-char, but this function goes word-by-word)…Let's first use pattern-matching instead of a boolean
case
statement…Now, I'll assume that you expect this to happen…
But, instead, you get…
And, to understand why that happens, let's see what's the result of
string:split/3
in your case…As you can see, those strings have no trailing spaces, like the keys on your map. So… replacing your map with the following one should fix your issue…