Skip to content

Commit

Permalink
Cleanup and refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
khueue committed Sep 27, 2011
1 parent 4b808a5 commit 6a8efee
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 29 deletions.
File renamed without changes.
File renamed without changes.
36 changes: 10 additions & 26 deletions nine.pl
@@ -1,6 +1,6 @@
:- begin_tests(nine_puzzle).

test('check all possible') :-
test('all valid words') :-
AllPossible =
[alandsk,dalska,damask,danska,drakma,dranka,kandar,kansla,
kardan,kladsam,klamra,klandra,klarna,kramla,kransa,krasnal,
Expand All @@ -13,18 +13,19 @@

:- end_tests(nine_puzzle).

% Interestingly, reading and dynamically asserting these terms instead
% of consulting them makes the program about twice as fast?
% See earlier revisions for that code.
:- [dict_terms].

nine(Grid, Word) :-
Grid = [_L1,_L2,_L3, _L4,L5,_L6, _L7,_L8,_L9],
read_dictionary,
Grid = [_,_,_, _,Center,_, _,_,_],
word(Word),
atom_chars(Word, Chars),
suitable_length(Chars),
lists:length(Chars, Len),
4 =< Len, Len =< 9,
singletons(Chars, Grid),
lists:member(L5, Chars).

suitable_length(L) :-
lists:length(L, Len),
4 =< Len, Len =< 9.
lists:member(Center, Chars).

singletons([], _).
singletons([C|Cs], Grid) :-
Expand All @@ -34,20 +35,3 @@
extract([X|Xs], X, Xs) :- !.
extract([X|Xs], Y, [X|Xs1]) :-
extract(Xs, Y, Xs1).

:- dynamic word/1.

read_dictionary :-
word(_), % Already loaded.
!.
read_dictionary :-
see('dict_terms.txt'),
read(Word),
read_word(Word),
seen.

read_word(end_of_file) :- !.
read_word(Term) :-
assert(Term),
read(NextTerm),
read_word(NextTerm).
7 changes: 4 additions & 3 deletions words_to_terms.rb
@@ -1,3 +1,4 @@
# $ ruby words_to_terms.rb > dict_terms.txt
terms = File.read('dict.txt').split.map {|w| "word(#{w})."}.join "\n"
puts terms
# $ ruby words_to_terms.rb > dict_terms.pl
words = File.read('dict_words.txt').split
terms = words.map {|w| "word(#{w})."}
puts terms.join "\n"

0 comments on commit 6a8efee

Please sign in to comment.