Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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