I'm working through a practice exercise on exercism and can't figure out why I'm getting the following error:
(CompileError) anagram.exs:19: cannot invoke remote function String.codepoints/1 inside match
(stdlib) lists.erl:1353: :lists.mapfoldl/3
(stdlib) lists.erl:1353: :lists.mapfoldl/3
I guess I'm not understanding pattern matching as well as I thought because I don't quite understand how I'm trying to call a remote function inside a match. Here are a couple examples of the test suite for context:
defmodule AnagramTest do
use ExUnit.Case
test "no matches" do
matches = Anagram.match "diaper", ["hello", "world", "zombies", "pants"]
assert matches == []
end
test "detect simple anagram" do
matches = Anagram.match "ant", ["tan", "stand", "at"]
assert matches == ["tan"]
end
Here's my code:
defmodule Anagram do
@doc """
Returns all candidates that are anagrams of, but not equal to, 'base'.
"""
@spec match(String.t, [String.t]) :: [String.t]
def match(base, candidates) do
base
|> String.codepoints
|> Enum.sort
|> scan_for_matches(candidates)
end
defp scan_for_matches(base, candidates) do
Enum.scan candidates, [], &(if analyze(&1, base), do: &2 ++ &1)
end
defp analyze(candidate, base) do
candidate
|> String.codepoints
|> Enum.sort
|> match?(base)
end
defp match?(candidate, base) do
candidate == base
end
end
Am I not just passing variables through to the analyze/2
function so that it ultimately returns a boolean
? I appreciate any insight.
match?/2
function, I havent tried to find any other mistakes in the code but it gets compiled if I change that function to something else. Must be something reserved. – NoDisplayName