Skip to content

Instantly share code, notes, and snippets.

@msszczep
Created April 13, 2019 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msszczep/ec0661f247841e831661d121cbccc9ac to your computer and use it in GitHub Desktop.
Save msszczep/ec0661f247841e831661d121cbccc9ac to your computer and use it in GitHub Desktop.
Subanagram prototype in Erlang (dated May 3, 2008)
#!/usr/bin/env escript
% This is an Erlang version of the subanagram generator.
% It works far faster and the code is far shorter.
% It takes a word at the command line as an argument.
% e.g.: `./subanagram_prototype.erl representative`
% Note: You need a file of words, one word per line, in the same directory as
% the script.
main([Original_word]) ->
List_of_words = file_parse("ospd3.txt"),
Subanagrams = sa_function(Original_word, List_of_words),
lists:map(fun(Word) -> io:fwrite("~s~n", [Word]) end, Subanagrams).
file_parse(File) ->
{ok, S} = file:open(File, [read,binary,raw]),
Size = filelib:file_size(File),
{ok, B1} = file:pread(S, 0, Size),
string:tokens(binary_to_list(B1), "\n").
sa_function(Original, List_of_words) ->
lists:filter(fun(Subanagram) -> length(Subanagram -- Original) =:= 0 end, List_of_words).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment