This commit is contained in:
2025-09-29 13:34:08 -07:00
parent 91f0064a5b
commit 5135a55081
6 changed files with 268 additions and 13 deletions
+47 -9
View File
@@ -1,10 +1,17 @@
% @doc a word is an ordset of ltrs
%
% This mathematically is a cluster of letters; so in "A + B + AB", this is one
% of the summands.
%
% multiplication is implied in a word
%
% empty word is 1
%
% anything times itself equals itself, so duplication is ignored
%
% multiplication = inclusive union
%
% operations assume all inputs are valid
-module(wfc_word).
-export_type([
@@ -15,7 +22,7 @@
%% constructors
one/0,
validate/1,
from_ltrs/1, to_ltrs/1,
from_list/1, to_list/1,
%% ops
mul/2, mul/1
]).
@@ -32,26 +39,54 @@ one() ->
{w, []}.
validate(_) -> error(nyi).
to_ltrs(_) -> error(nyi).
-spec validate(Word) -> Result
when Word :: word(),
Result :: ok
| {error, Reason :: string()}.
% @doc
% check each letter in the word for validity
%
% also check that word shape is valid
% @end
-spec from_ltrs(Ltrs) -> Result
validate(W = {w, Letters}) ->
case from_list(Letters) of
{ok, Result} when Result =:= W ->
ok;
Error ->
Error
end;
validate(X) ->
{error, wfc_utils:str("wfc_word:validate: malformed word: ~tp", [X])}.
-spec to_list(word()) -> Result
when Result :: {ok, [wfc_ltr:ltr()]}
| {error, string()}.
to_list({w, Ltrs}) -> {ok, Ltrs};
to_list(Bad) -> {error, wfc_utils:str("wfc_word:to_list: bad letter: ~tp", [Bad])}.
-spec from_list(Ltrs) -> Result
when Ltrs :: list(wfc_ltr:ltr()),
Result :: {ok, word()}
| {error, Reason :: string()}.
from_ltrs(Chars) ->
from_ltrs(ordsets:from_list(Chars), []).
from_list(Chars) ->
from_list(ordsets:from_list(Chars), []).
%% validate each letter
from_ltrs([Ltr | Rest], Acc) ->
from_list([Ltr | Rest], Acc) ->
case wfc_ltr:validate(Ltr) of
ok -> from_ltrs(Rest, [Ltr | Acc]);
ok -> from_list(Rest, [Ltr | Acc]);
Error -> Error
end;
% done, all good
from_ltrs([], Acc) ->
from_list([], Acc) ->
{ok, {w, lists:reverse(Acc)}}.
@@ -73,3 +108,6 @@ mul({w, X}, {w, Y}) ->
mul([Word | Rest]) -> mul(Word, mul(Rest));
mul([]) -> one().
add(