ฉันกำลังฝึกปฏิบัติเกี่ยวกับการออกกำลังกาย และไม่เข้าใจว่าทำไมฉันจึงได้รับข้อผิดพลาดต่อไปนี้:
(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
ฉันเดาว่าฉันไม่เข้าใจการจับคู่รูปแบบเช่นเดียวกับที่ฉันคิดเพราะฉันไม่ค่อยเข้าใจว่าฉันพยายามเรียกใช้ฟังก์ชันระยะไกลในการแข่งขันอย่างไร ต่อไปนี้เป็นตัวอย่างชุดทดสอบสำหรับบริบท:
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
นี่คือรหัสของฉัน:
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
ฉันไม่ได้แค่ส่งตัวแปรผ่านไปยังฟังก์ชัน analyze/2
เพื่อให้ส่งกลับ boolean
ในท้ายที่สุดใช่หรือไม่ ฉันขอขอบคุณข้อมูลเชิงลึกใด ๆ
match?/2
ฉันไม่ได้พยายามค้นหาข้อผิดพลาดอื่น ๆ ในโค้ด แต่จะได้รับการคอมไพล์ถ้าฉันเปลี่ยนฟังก์ชันนั้นเป็นอย่างอื่น คงจะเป็นสิ่งที่สงวนไว้ - person NoDisplayName   schedule 21.02.2016