在这里,Map是一个由键和值对组成的Map数据,如
Map = # {"a" => "Apple","b" =>"bat","c" =>"cat ",
"d" => "dog","e" => "eagle","f" => "fan ","g" => "goat",
"h" =>"hat","i" =>"ink","j" =>"jar","k" =>"king","l" =>"lion ",
"m" =>"madam","n" =>"nike","o" => "orange","p" =>"pot",
"q" =>"queue ","r" =>"rat","s" =>"snake","t" =>"tea ",
"u" =>"umbrella","v" =>"van ","w" =>"wolf ","x" =>"xperia ",
"y" =>"yawk","z" =>"zoo "}
我试图打印以下内容:
示例:
- 输入:苹果耐克山羊狮子老鹰
- 预期输出:Angular 。
尝试的代码:
-module(main).
-export([start/1,func/2]).
start(Str) ->
Map = # {"a" => "Apple","b" =>"bat","c" =>"cat ","d" => "dog","e" => "eagle","f" => "fan ","g" => "goat","h" =>"hat","i" =>"ink","j" =>"jar","k" =>"king","l" =>"lion ","m" =>"madam","n" =>"nike","o" => "orange","p" =>"pot","q" =>"queue ","r" =>"rat","s" =>"snake","t" =>"tea ","u" =>"umbrella","v" =>"van ","w" =>"wolf ","x" =>"xperia ","y" =>"yawk","z" =>"zoo "},
Chunks = string:tokens(Str, [$\s]),
io:format("~n~p",[Chunks]),
L = func(Chunks,Map),
io:format("~p",[L]).
func([],#{}) ->
io:format("~nCompleted~n");
func([First | Rest], Map) ->
Fun = fun(K, V, Acc) ->
if V == First -> [K | Acc];
true -> Acc
end
end,
maps:fold(Fun, [], Map),
func(Rest, Map).
有人能给我点建议吗?
2条答案
按热度按时间k2fxgqgv1#
You say your input is:
but that isn't an erlang term, so your input is nonsensical.
If your input is actually the string
"Apple nike goat lion eagle"
, then I'm not sure why you need your map because you can just extract the first letter of each word:In the shell:
You can read about how to work with strings below. The list of words returned by string:split/3 is actually a list where each element is a list of integers, e.g.
The function
get_first_letters/2
might be easier to understand if you write it like this:It also makes no sense to create a map where the words are the values and the first letters are the keys. Instead, you would create a map where the words are the keys and the values are the first letters, then you could call maps:get/2 with a word to look up the first letter.
If you actually have a map where a word/value is associated with more than one key, then things are a little trickier. The following example retrieves all the keys associated with a word:
In the shell:
In erlang, a string is just a shortcut for creating a list of integers, where the integers in the list are the ascii codes of the characters in the string.
You may not like that, but that's the way it is: a double quoted string tells erlang to create a list of integers. The shell is misleading because sometimes the shell prints out the string "abc" as
"abc"
instead of[97, 98, 99]
. To prevent the shell from misleading you, you can executeshell:strings(false)
in the shell, and then the shell will always output lists of integers for strings:That will continue for the rest of your shell session (or until you execute
shell:strings(true)
. You can still explicitly tell the shell to print a string if you want:When you use
[Head|Tail]
to match a list of integers, e.g. a double quoted string,Head
will match an integer:The problem is that the keys in your map are strings--not integers. The easiest way to handle that is to turn the integer into a string by inserting it into a list:
Here is an example:
In the shell:
If you would like the results in the same order as the letters in your input string, then return
lists:reverse(Acc)
instead ofAcc
.If you want to display each word--not a list of words, like this:
you can do this:
If you don't want to display the quotes, for instance:
you can use the
~s
control sequence:oprakyz72#
我建议你这样做。当然,为了提高效率,你应该把反向字典存储在某个地方,比如服务器状态。