Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of HECTOPK

HECTOPK's solution

to Anagram in the Erlang Track

Published at Feb 27 2021 · 0 comments
Test suite

Given a word and a list of possible anagrams, select the correct sublist.

Given "listen" and a list of candidates like "enlists" "google" "inlets" "banana" the program should return a list containing "inlets".

Running tests

In order to run the tests, issue the following command from the exercise directory:

For running the tests provided, rebar3 is used as it is the official build and dependency management tool for erlang now. Please refer to the tracks installation instructions on how to do that.

In order to run the tests, you can issue the following command from the exercise directory.

$ rebar3 eunit


For detailed information about the Erlang track, please refer to the help page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.


Inspired by the Extreme Startup game https://github.com/rchatley/extreme_startup

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


%% Generated with 'testgen v0.2.0'
%% Revision 1 of the exercises generator was used
%% https://github.com/exercism/problem-specifications/raw/42dd0cea20498fd544b152c4e2c0a419bb7e266a/exercises/anagram/canonical-data.json
%% This file is automatically generated from the exercises canonical data.



'1_no_matches_test_'() ->
    {"no matches",
						    ["hello", "world",
						     "zombies", "pants"])))}.

'2_detects_two_anagrams_test_'() ->
    {"detects two anagrams",
     ?_assertMatch(["maters", "stream"],
						    ["stream", "pigeon",

'3_does_not_detect_anagram_subsets_test_'() ->
    {"does not detect anagram subsets",
						    ["dog", "goody"])))}.

'4_detects_anagram_test_'() ->
    {"detects anagram",
						    ["enlists", "google",
						     "inlets", "banana"])))}.

'5_detects_three_anagrams_test_'() ->
    {"detects three anagrams",
     ?_assertMatch(["gallery", "largely", "regally"],
						    ["gallery", "ballerina",
						     "regally", "clergy",
						     "largely", "leading"])))}.

'6_detects_multiple_anagrams_with_different_case_test_'() ->
    {"detects multiple anagrams with different "
     ?_assertMatch(["Eons", "ONES"],
						    ["Eons", "ONES"])))}.

'7_does_not_detect_non_anagrams_with_identical_checksum_test_'() ->
    {"does not detect non-anagrams with identical "
		   lists:sort(anagram:find_anagrams("mass", ["last"])))}.

'8_detects_anagrams_case_insensitively_test_'() ->
    {"detects anagrams case-insensitively",

'9_detects_anagrams_using_case_insensitive_subject_test_'() ->
    {"detects anagrams using case-insensitive "

'10_detects_anagrams_using_case_insensitive_possible_matches_test_'() ->
    {"detects anagrams using case-insensitive "
     "possible matches",

'11_does_not_detect_an_anagram_if_the_original_word_is_repeated_test_'() ->
    {"does not detect an anagram if the original "
     "word is repeated",
		   lists:sort(anagram:find_anagrams("go", ["go Go GO"])))}.

'12_anagrams_must_use_all_letters_exactly_once_test_'() ->
    {"anagrams must use all letters exactly "

'13_words_are_not_anagrams_of_themselves_case_insensitive_test_'() ->
    {"words are not anagrams of themselves "
						    ["BANANA", "Banana",

'14_words_other_than_themselves_can_be_anagrams_test_'() ->
    {"words other than themselves can be anagrams",
						    ["Listen", "Silent",


find_anagrams(Subject, Candidates) ->
	Sub = string:casefold(Subject),
	find_anagrams(make_map(Sub), Sub, Candidates, []).

find_anagrams(_, _, [], Result) -> Result;

find_anagrams(M, S, [F | R], Result) ->
	case check_word(M, S, string:casefold(F)) of
		true -> find_anagrams(M, S, R, [F | Result]);
  		false -> find_anagrams(M, S, R, Result)

check_word(M, S, L) ->
	case string:equal(L, S, true) of
		true -> false;
		false -> check_word(M,L)

check_word(M, []) ->
	case sumValues(M) of
		0 -> true;
		_Else -> false

check_word(M, [F | R]) ->
	V = maps:get(F, M, 0),
	case V /= 0 of
		true -> check_word(M#{F => V-1}, R);
		false -> false

make_map(List) -> make_map(List, #{}).

make_map([], M) -> M;

make_map([F | R], M) ->	make_map(R, M#{F => maps:get(F,M,0)+1}).

sumValues(M) -> sum(maps:values(M)).

sum(List) -> sum(List, 0).

sum([], Sum) -> Sum;

sum([F | R], Sum) -> sum(R, Sum + F).

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other peopleโ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?