Skip to content

Instantly share code, notes, and snippets.

@pirj
Created April 30, 2010 14:04
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 pirj/385245 to your computer and use it in GitHub Desktop.
Save pirj/385245 to your computer and use it in GitHub Desktop.
6
whale: big black water animal
penguin: black white ice beak
piano: keyboard black white wire
jackboot: leather heel black
train: rail wheel black
rose: red green thorn
4
whale penguin piano jackboot train
penguin piano
jackboot rose
piano train
#!/usr/bin/env escript
-module(rec).
-export([main/1]).
recombine(_, []) -> nil;
recombine([], Common) -> Common;
recombine([Head|Tail], Common) -> recombine(Tail, [X || X <- Head, Y <- Common, X == Y]).
recombine([]) -> nil;
recombine([Head|Tail]) -> recombine(Tail, Head).
remove_newline(S) -> lists:sublist(S, 1, length(S) - 1).
read_n_lines(_, Lines, 0) -> Lines;
read_n_lines(File, Lines, N) ->
read_n_lines(File, [remove_newline(io:get_line(File, nil)) | Lines], N - 1).
print_solutions(Defs, Comb) ->
M = [Props || Term1 <- Comb, {Term, Props} <- Defs, Term1 == Term],
R = recombine(M),
io:fwrite("Intersection: ~p~n", [R]).
main([FileName]) ->
{ok, File} = file:open(FileName, [read]),
TermDefs = read_n_lines(File, [], list_to_integer(remove_newline(io:get_line(File, nil)))),
Defs0 = [string:tokens(Def, ":") || Def <- TermDefs],
Defs = [{list_to_atom(H1), [list_to_atom(S) || S <- string:tokens(H2, " ")]} || [H1|[H2|_]] <- Defs0],
TermCombs = read_n_lines(File, [], list_to_integer(remove_newline(io:get_line(File, nil)))),
Combs = [[list_to_atom(S) || S <- string:tokens(Comb, " ")] || Comb <- TermCombs],
[print_solutions(Defs, Comb) || Comb <- Combs].
Intersection: [black]
Intersection: nil
Intersection: [black,white]
Intersection: [black]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment