Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
...
  • 5 commits
  • 28 files changed
  • 0 commit comments
  • 1 contributor
Commits on Jan 19, 2012
@kukulski prolog forward fib 213b4cb
Commits on Jan 20, 2012
@kukulski sodoku, eightQueens 4826e68
Commits on Jan 21, 2012
@kukulski factored out sodoku not working quite yet a07a554
@kukulski slightly less talky version is up 1186faf
Commits on Jan 23, 2012
@kukulski another day
scala -- got the job done, but the code is bad bad bad. I don't get the
core objects yet

prolog -- bugs in the big-boad version -- typos I think
would want better file reading to work right
(start with an 'all open' and write concrete values into cells as we
read from the file)
db232eb
View
1 prolog/board.txt
@@ -0,0 +1 @@
+1 2 3 4 5 6 7 8 9
View
5 prolog/books.pl
@@ -0,0 +1,5 @@
+book(snow_crash,stevenson,sf,short).
+book(cryptonomicon,stevenson,sf,long).
+book(code,lessig,nonfiction,short).
+book(sevenLanguages,tate,nonfiction,short).
+book(dictionary,webster,reference,long).
View
11 prolog/concat.pl
@@ -0,0 +1,11 @@
+#---
+# Excerpted from "Seven Languages in Seven Weeks",
+# published by The Pragmatic Bookshelf.
+# Copyrights apply to this code. It may not be used to create training material,
+# courses, books, articles, and the like. Contact us if you are in doubt.
+# We make no guarantees that this code is fit for any purpose.
+# Visit http://www.pragmaticprogrammer.com/titles/btlang for more book information.
+#---
+concatenate([], List, List).
+concatenate([Head|Tail1], List, [Head|Tail2]) :-
+ concatenate(Tail1, List, Tail2).
View
9 prolog/concat_step_1.pl
@@ -0,0 +1,9 @@
+#---
+# Excerpted from "Seven Languages in Seven Weeks",
+# published by The Pragmatic Bookshelf.
+# Copyrights apply to this code. It may not be used to create training material,
+# courses, books, articles, and the like. Contact us if you are in doubt.
+# We make no guarantees that this code is fit for any purpose.
+# Visit http://www.pragmaticprogrammer.com/titles/btlang for more book information.
+#---
+concatenate([], List, List).
View
10 prolog/concat_step_2.pl
@@ -0,0 +1,10 @@
+#---
+# Excerpted from "Seven Languages in Seven Weeks",
+# published by The Pragmatic Bookshelf.
+# Copyrights apply to this code. It may not be used to create training material,
+# courses, books, articles, and the like. Contact us if you are in doubt.
+# We make no guarantees that this code is fit for any purpose.
+# Visit http://www.pragmaticprogrammer.com/titles/btlang for more book information.
+#---
+concatenate([], List, List).
+concatenate([Head|[]], List, [Head|List]).
View
12 prolog/concat_step_3.pl
@@ -0,0 +1,12 @@
+#---
+# Excerpted from "Seven Languages in Seven Weeks",
+# published by The Pragmatic Bookshelf.
+# Copyrights apply to this code. It may not be used to create training material,
+# courses, books, articles, and the like. Contact us if you are in doubt.
+# We make no guarantees that this code is fit for any purpose.
+# Visit http://www.pragmaticprogrammer.com/titles/btlang for more book information.
+#---
+concatenate([], List, List).
+concatenate([Head|[]], List, [Head|List]).
+concatenate([Head1|[Head2|[]]], List, [Head1, Head2|List]).
+concatenate([Head1|[Head2|[Head3|[]]]], List, [Head1, Head2, Head3|List]).
View
15 prolog/family.pl
@@ -0,0 +1,15 @@
+#---
+# Excerpted from "Seven Languages in Seven Weeks",
+# published by The Pragmatic Bookshelf.
+# Copyrights apply to this code. It may not be used to create training material,
+# courses, books, articles, and the like. Contact us if you are in doubt.
+# We make no guarantees that this code is fit for any purpose.
+# Visit http://www.pragmaticprogrammer.com/titles/btlang for more book information.
+#---
+father(zeb, john_boy_sr).
+father(john_boy_sr, john_boy_jr).
+
+ancestor(X, Y) :-
+ father(X, Y).
+ancestor(X, Y) :-
+ father(X, Z), ancestor(Z, Y).
View
21 prolog/food.pl
@@ -0,0 +1,21 @@
+#---
+# Excerpted from "Seven Languages in Seven Weeks",
+# published by The Pragmatic Bookshelf.
+# Copyrights apply to this code. It may not be used to create training material,
+# courses, books, articles, and the like. Contact us if you are in doubt.
+# We make no guarantees that this code is fit for any purpose.
+# Visit http://www.pragmaticprogrammer.com/titles/btlang for more book information.
+#---
+food_type(velveeta, cheese).
+food_type(ritz, cracker).
+food_type(spam, meat).
+food_type(sausage, meat).
+food_type(jolt, soda).
+food_type(twinkie, dessert).
+
+flavor(sweet, dessert).
+flavor(savory, meat).
+flavor(savory, cheese).
+flavor(sweet, soda).
+
+food_flavor(X, Y) :- food_type(X, Z), flavor(Y, Z).
View
13 prolog/friends.pl
@@ -0,0 +1,13 @@
+#---
+# Excerpted from "Seven Languages in Seven Weeks",
+# published by The Pragmatic Bookshelf.
+# Copyrights apply to this code. It may not be used to create training material,
+# courses, books, articles, and the like. Contact us if you are in doubt.
+# We make no guarantees that this code is fit for any purpose.
+# Visit http://www.pragmaticprogrammer.com/titles/btlang for more book information.
+#---
+likes(wallace, cheese).
+likes(grommit, cheese).
+likes(wendolene, sheep).
+
+friend(X, Y) :- \+(X = Y), likes(X, Z), likes(Y, Z).
View
20 prolog/hello.pl
@@ -0,0 +1,20 @@
+#---
+# Excerpted from "Seven Languages in Seven Weeks",
+# published by The Pragmatic Bookshelf.
+# Copyrights apply to this code. It may not be used to create training material,
+# courses, books, articles, and the like. Contact us if you are in doubt.
+# We make no guarantees that this code is fit for any purpose.
+# Visit http://www.pragmaticprogrammer.com/titles/btlang for more book information.
+#---
+character(brian).
+character(jinx).
+
+species(brian, dog).
+species(jinx, cat).
+
+class(dog, mammal).
+class(cat, mammal).
+
+animal(X) :- species(X, Y).
+is_a(X, Z) :- species(X, Y), class(Y, Z).
+
View
54 prolog/last.pl
@@ -0,0 +1,54 @@
+myLast(Hd, [Hd|[]]).
+myLast(Other, [_|Tail]) :- myLast(Other,Tail).
+
+
+count(0,[]).
+count(Count, [_|T]) :- count(TailCount, T), Count is TailCount +1.
+
+
+sum(0,[]).
+sum(X,[X]).
+sum(Total, [Head|T]) :- sum(SubTotal, T), Total is SubTotal +Head.
+
+bisum(0,[]).
+%% bisum(X,[X]).
+bisum(Total, [Head|T]) :- bisum(SubTotal, T), Total #=# SubTotal +Head.
+
+
+
+
+
+
+average(Avg,List) :- sum(Sum,List), count(Count,List), Avg is Sum/Count;
+average(0,[]).
+
+
+biaverage(Avg,List) :- bisum(Sum,List), count(Count,List), Avg #=# Sum/Count;
+biaverage(0,[]).
+
+
+
+concatenate([], List, List).
+concatenate([Head|Tail1], List, [Head|Tail2]) :-
+ concatenate(Tail1, List, Tail2).
+
+
+fibr(0,0).
+fibr(1,1).
+fibr(N,Fib) :-
+ N > 1,
+ N1 is N-1, N2 is N-2,
+ fibr(N1,F1), fibr(N2,F2),
+ Fib is F1 + F2.
+
+
+
+fib(N,Fib) :- fibList(N,[Fib|_]).
+fibList(0,[0]).
+fibList(1,[1,0]).
+fibList(N,[Fib|Tail]) :- N > 0, N1 is N-1, fibList(N1,Tail), Tail = [F1 | [F2 | _]], Fib is F1 + F2.
+
+bifib(N,Fib) :- bifibList(N,[Fib|_]).
+bifibList(0,[0]).
+bifibList(1,[1,0]).
+bifibList(N,[Fib|Tail]) :- N #># 0, N1 #=# N-1, bifibList(N1,Tail), Tail = [F1 | [F2 | _]], Fib #=# F1 + F2.
View
18 prolog/list.pl
@@ -0,0 +1,18 @@
+#---
+# Excerpted from "Seven Languages in Seven Weeks",
+# published by The Pragmatic Bookshelf.
+# Copyrights apply to this code. It may not be used to create training material,
+# courses, books, articles, and the like. Contact us if you are in doubt.
+# We make no guarantees that this code is fit for any purpose.
+# Visit http://www.pragmaticprogrammer.com/titles/btlang for more book information.
+#---
+head(Head, [Head|Tail]).
+tail(Tail, [Head|Tail]).
+
+beginning(Top, List) :- head(Top, List).
+
+end(Head, [Head|[]]).
+end(End, [Head|Tail]) :- end(End, Tail).
+
+includes(Head, [Head|Tail]).
+includes(Item, [Head|Tail]) :- includes(Item, Tail).
View
15 prolog/list_math.pl
@@ -0,0 +1,15 @@
+#---
+# Excerpted from "Seven Languages in Seven Weeks",
+# published by The Pragmatic Bookshelf.
+# Copyrights apply to this code. It may not be used to create training material,
+# courses, books, articles, and the like. Contact us if you are in doubt.
+# We make no guarantees that this code is fit for any purpose.
+# Visit http://www.pragmaticprogrammer.com/titles/btlang for more book information.
+#---
+count(0, []).
+count(Count, [Head|Tail]) :- count(TailCount, Tail), Count is TailCount + 1.
+
+sum(0, []).
+sum(Total, [Head|Tail]) :- sum(Sum, Tail), Total is Head + Sum.
+
+average(Average, List) :- sum(Sum, List), count(Count, List), Average is Sum/Count.
View
22 prolog/map.pl
@@ -0,0 +1,22 @@
+#---
+# Excerpted from "Seven Languages in Seven Weeks",
+# published by The Pragmatic Bookshelf.
+# Copyrights apply to this code. It may not be used to create training material,
+# courses, books, articles, and the like. Contact us if you are in doubt.
+# We make no guarantees that this code is fit for any purpose.
+# Visit http://www.pragmaticprogrammer.com/titles/btlang for more book information.
+#---
+different(red, green). different(red, blue).
+different(green, red). different(green, blue).
+different(blue, red). different(blue, green).
+
+coloring(Alabama, Mississippi, Georgia, Tennessee, Florida) :-
+ different(Mississippi, Tennessee),
+ different(Mississippi, Alabama),
+ different(Alabama, Tennessee),
+ different(Alabama, Mississippi),
+ different(Alabama, Georgia),
+ different(Alabama, Florida),
+ different(Georgia, Florida),
+ different(Georgia, Tennessee).
+
View
24 prolog/math.pl
@@ -0,0 +1,24 @@
+#---
+# Excerpted from "Seven Languages in Seven Weeks",
+# published by The Pragmatic Bookshelf.
+# Copyrights apply to this code. It may not be used to create training material,
+# courses, books, articles, and the like. Contact us if you are in doubt.
+# We make no guarantees that this code is fit for any purpose.
+# Visit http://www.pragmaticprogrammer.com/titles/btlang for more book information.
+#---
+factorial(0, 1).
+factorial(Index, Fac) :-
+ Index > 0,
+ OneBack is Index - 1,
+ factorial(OneBack, PreviousFac),
+ Fac is PreviousFac * Index.
+
+fib(1, 1).
+fib(2, 1).
+fib(Index, Value) :-
+ Index > 2,
+ Previous is Index - 1,
+ PreviousPrevious is Index - 2,
+ fib(Previous, X),
+ fib(PreviousPrevious, Y),
+ Value is X + Y.
View
1 prolog/musicians.pl
@@ -0,0 +1 @@
+musician
View
13 prolog/ohmy.pl
@@ -0,0 +1,13 @@
+#---
+# Excerpted from "Seven Languages in Seven Weeks",
+# published by The Pragmatic Bookshelf.
+# Copyrights apply to this code. It may not be used to create training material,
+# courses, books, articles, and the like. Contact us if you are in doubt.
+# We make no guarantees that this code is fit for any purpose.
+# Visit http://www.pragmaticprogrammer.com/titles/btlang for more book information.
+#---
+cat(lion).
+cat(tiger).
+
+dorothy(X, Y, Z) :- X = lion, Y = tiger, Z = bear.
+twin_cats(X, Y) :- cat(X), cat(Y).
View
37 prolog/optimized_queens.pl
@@ -0,0 +1,37 @@
+#---
+# Excerpted from "Seven Languages in Seven Weeks",
+# published by The Pragmatic Bookshelf.
+# Copyrights apply to this code. It may not be used to create training material,
+# courses, books, articles, and the like. Contact us if you are in doubt.
+# We make no guarantees that this code is fit for any purpose.
+# Visit http://www.pragmaticprogrammer.com/titles/btlang for more book information.
+#---
+valid_queen((Row, Col)) :- member(Col, [1,2,3,4,5,6,7,8]).
+valid_board([]).
+valid_board([Head|Tail]) :- valid_queen(Head), valid_board(Tail).
+
+cols([], []).
+cols([(_, Col)|QueensTail], [Col|ColsTail]) :-
+ cols(QueensTail, ColsTail).
+
+diags1([], []).
+diags1([(Row, Col)|QueensTail], [Diagonal|DiagonalsTail]) :-
+ Diagonal is Col - Row,
+ diags1(QueensTail, DiagonalsTail).
+
+
+diags2([], []).
+diags2([(Row, Col)|QueensTail], [Diagonal|DiagonalsTail]) :-
+ Diagonal is Col + Row,
+ diags2(QueensTail, DiagonalsTail).
+
+eight_queens(Board) :-
+ Board = [(1, _), (2, _), (3, _), (4, _), (5, _), (6, _), (7, _), (8, _)],
+ valid_board(Board),
+
+ cols(Board, Cols),
+ diags1(Board, Diags1),
+ diags2(Board, Diags2),
+ fd_all_different(Cols),
+ fd_all_different(Diags1),
+ fd_all_different(Diags2).
View
176 prolog/phodoku.pl
@@ -0,0 +1,176 @@
+sodoku(P,S) :- P = S,
+P = [A1,A2,A3,A4,
+B1,B2,B3,B4,
+C1,C2,C3,C4,
+D1,D2,D3,D4],
+fd_domain(P,1,4),
+Row1=[A1,A2,A3,A4],
+Row2=[B1,B2,B3,B4],
+Row3=[C1,C2,C3,C4],
+Row4=[D1,D2,D3,D4],
+Col1=[A1,B1,C1,D1],
+Col2=[A2,B2,C2,D2],
+Col3=[A3,B3,C3,D3],
+Col4=[A4,B4,C4,D4],
+Sq1=[A1,A2,B1,B2],
+Sq2=[A3,A4,B3,B4],
+Sq3=[C1,C2,D1,D2],
+Sq4=[C3,C4,D3,D4],
+valid([Row1,Row2,Row3,Row4,Col1,Col2,Col3,Col4,Sq1,Sq2,Sq3,Sq4]),
+printboard(P).
+
+
+s3(P,S) :-
+P = S,
+fd_domain(P,1,9),
+length(P,81),
+rows(P,9,Rows),
+columns(P,[0,1,2,3,4,5,6,7,8] ,Cols),
+squares9(P,[0,3,6,
+ 27,30,33,
+ 54,57,63],Squares),
+valid(Rows),
+valid(Squares),
+valid(Cols),
+printboard9(P).
+
+
+
+s2(P,S) :-
+P = S,
+fd_domain(P,1,4),
+length(P,16),
+rows(P,4,Rows),
+columns(P,[0,1,2,3] ,Cols),
+squares(P,[0,2,8,10],Squares),
+valid(Rows),
+valid(Squares),
+valid(Cols),
+printboard(P).
+
+
+%s2f(Name,S) :-
+%readArray(Name,P), s2(P,S).
+
+%s3f(Name,S) :-
+%readArray(Name,P), s2(P,S).
+
+%% doesn't work -- I don't know how to init the unknowns from a file
+%readArray(Name,Arr) :-
+% seeing(Old),
+% see(Name),
+% readArray(Arr),
+% seen,
+% see(Old).
+%
+%readArray([X|Tail]) :-
+% read(X),
+% X \= end-of-file,
+% readArray(Tail).
+
+
+
+
+
+peelOff(X,Len,B):- append(A,B,X), length(A,Len).
+peelOff(X,Len,A,B):- append(A,B,X), length(A,Len).
+
+%rows([],[]).
+%rows([A,B,C,D|Tail],[[A,B,C,D]|NewTail]) :- rows(Tail,NewTail).
+
+rows(P,Count,Out):- chunk(P,Count,Out).
+
+fourOdd([],[]).
+fourOdd([A,_,_,_|Tail],[A|NewTail]) :- fourOdd(Tail,NewTail).
+fourOdd([A,_,_],[A]).
+fourOdd([A,_],[A]).
+fourOdd([A],[A]).
+
+%%%
+%
+% not working. I think the bug is is Xodd - - could be in Columns9
+%
+%%%
+
+xOdd(List,X,[Out|NewTail]):- peelOff(List,Len,[Out|_],Tail), xOdd(Tail,X,NewTail).
+
+chunk([],_,[]).
+chunk(Full,ChunkSize,[Col|NewTail]) :-
+ peelOff(Full,ChunkSize,Col,Tail),
+ chunk(Tail,ChunkSize,NewTail).
+
+item(Full,Index,X) :- peelOff(Full,Index,Tail), Tail = [X|_].
+
+count(X,[X|NewTail]):- X > 0, X1 is X-1, count(X1,NewTail).
+count(0,[]).
+
+
+columns(_,[],_).
+columns(Puzzle,[Counter|CounterTail],[Col|NewTail]):-
+ peelOff(Puzzle,Counter,Shifted),
+ fourOdd(Shifted,Col),
+ columns(Puzzle,CounterTail,NewTail).
+
+squares(_,[],_).
+squares(Puzzle,[Counter|CounterTail],[Square|NewTail]):-
+ peelOff(Puzzle,Counter,Shifted),
+ square(Shifted,Square),
+ squares(Puzzle,CounterTail,NewTail).
+
+
+columns9(_,[],_).
+columns9(Puzzle,[Counter|CounterTail],[Col|NewTail]):-
+peelOff(Puzzle,Counter,Shifted),
+xOdd(Shifted,9,Col),
+columns9(Puzzle,CounterTail,NewTail).
+
+squares9(_,[],_).
+squares9(Puzzle,[Counter|CounterTail],[Square|NewTail]):-
+ peelOff(Puzzle,Counter,Shifted),
+ square9(Shifted,Square),
+squares9(Puzzle,CounterTail,NewTail).
+
+square([A,B,_,_,C,D|_],[A,B,C,D]).
+
+square9(X,MySquare):-
+ chunk(X,3,[R1,_,_,R2,_,_,R3|_]),
+ append(R1,R2,TwoLines),
+ append(TwoLines,R3,MySquare).
+
+valid([]).
+valid([H|T]) :- fd_all_different(H),valid(T).
+
+%printline([]).
+printline(X) :- print(X), print('\n').
+%printline([H|T]) :- prin(H), printline(T).
+
+printboard([]).
+printboard([A,B,C,D|T]) :- printline([A,B,C,D]), printboard(T).
+
+printboardX(Board,X):-
+ chunk(Board,X,SubRows),
+chunk(SubRows,X,Rows),
+ chunk(Rows,X,Slices),
+ printSlices(Slices).
+
+testBreak:-
+ count(81,X),
+ printboardX(X,3).
+
+printSlices([]).
+printSlices([Slice|Tail]):-
+ printSlice(Slice),
+ print('\n'),
+ printSlices(Tail).
+
+printSlice([]).
+printSlice([Line|Tail]):- print(Line), print('\n'),printSlice(Tail).
+
+testRows(X):-
+ count(81,Y),
+ rows(Y,9,X).
+
+testCols(X):-
+ count(81,P),
+ columns9(P,[0,1,2,3,4,5,6,7,8] ,X).
+
View
46 prolog/queens.pl
@@ -0,0 +1,46 @@
+#---
+# Excerpted from "Seven Languages in Seven Weeks",
+# published by The Pragmatic Bookshelf.
+# Copyrights apply to this code. It may not be used to create training material,
+# courses, books, articles, and the like. Contact us if you are in doubt.
+# We make no guarantees that this code is fit for any purpose.
+# Visit http://www.pragmaticprogrammer.com/titles/btlang for more book information.
+#---
+valid_queen((Row, Col)) :-
+ Range = [1,2,3,4,5,6,7,8],
+ member(Row, Range), member(Col, Range).
+
+valid_board([]).
+valid_board([Head|Tail]) :- valid_queen(Head), valid_board(Tail).
+
+rows([], []).
+rows([(Row, _)|QueensTail], [Row|RowsTail]) :-
+ rows(QueensTail, RowsTail).
+
+cols([], []).
+cols([(_, Col)|QueensTail], [Col|ColsTail]) :-
+ cols(QueensTail, ColsTail).
+
+diags1([], []).
+diags1([(Row, Col)|QueensTail], [Diagonal|DiagonalsTail]) :-
+ Diagonal is Col - Row,
+ diags1(QueensTail, DiagonalsTail).
+
+diags2([], []).
+diags2([(Row, Col)|QueensTail], [Diagonal|DiagonalsTail]) :-
+ Diagonal is Col + Row,
+ diags2(QueensTail, DiagonalsTail).
+
+eight_queens(Board) :-
+ length(Board, 8),
+ valid_board(Board),
+
+ rows(Board, Rows),
+ cols(Board, Cols),
+ diags1(Board, Diags1),
+ diags2(Board, Diags2),
+
+ fd_all_different(Rows),
+ fd_all_different(Cols),
+ fd_all_different(Diags1),
+ fd_all_different(Diags2).
View
24 prolog/sort.pl
@@ -0,0 +1,24 @@
+insert_sort(List,Sorted):-isort(List,[],Sorted).
+
+isort([],Acc,Acc).
+isort([H|T],Acc,Sorted):-insert(H,Acc,Nacc),isort(T,Nacc,Sorted).
+
+insert(X,[Y|T],[Y|NT]):- X>Y ,insert(X,T,NT).
+insert(X,[Y|T],[X,Y|T]):-X=<Y.
+insert(X,[],[X]).
+
+
+
+
+rva([X|T],Acc,Result) :- rva(T,[X|Acc],Result).
+rva([],Acc,Acc).
+rva(X,Result) :- rva(X,[],Result).
+
+smallest(X,[X]).
+smallest(Head, [Head|T]) :- smallest(PreviousBest, T), Head < PreviousBest.
+smallest(Head, [Head|T]) :- smallest(PreviousBest, T), Head = PreviousBest.
+smallest(PreviousBest, [Head|T]) :- smallest(PreviousBest, T), Head > PreviousBest.
+
+
+
+
View
37 prolog/sqeens.pl
@@ -0,0 +1,37 @@
+valid_queen((_,C)) :- member(C,[1,2,3,4,5,6,7,8]).
+
+valid_board([]).
+valid_board([H|T]) :- valid_queen(H), valid_board(T).
+
+cols([],[]).
+cols([(_,Col)|QT],[Col|CT]) :- cols(QT,CT).
+
+diags1([],[]).
+diags1([(Row,Col)|QueensTail],[Diagonal|DiagonalsTail]) :-
+ Diagonal is Col - Row,
+ diags1(QueensTail,DiagonalsTail).
+
+
+diags2([],[]).
+diags2([(Row,Col)|QueensTail],[Diagonal|DiagonalsTail]) :-
+ Diagonal is Col + Row,
+ diags2(QueensTail,DiagonalsTail).
+
+allDifferent([]).
+allDifferent([H|T]) :- fd_all_different(H),alldifferent(T).
+
+
+eight_queens(Board) :-
+ Board=[(1,_),(2,_),(3,_),(4,_),(5,_),(6,_),(7,_),(8,_)],
+ valid_board(Board),
+
+ cols(Board,Cols),
+ diags1(Board,Diags1),
+ diags2(Board,Diags2),
+
+ allDifferent([Cols,Diags1,Diags2]).
+%fd_all_different(Cols), fd_all_different(Diags1), fd_all_different(Diags2).
+%.
+
+
+
View
70 prolog/sudoku.pl
@@ -0,0 +1,70 @@
+#---
+# Excerpted from "Seven Languages in Seven Weeks",
+# published by The Pragmatic Bookshelf.
+# Copyrights apply to this code. It may not be used to create training material,
+# courses, books, articles, and the like. Contact us if you are in doubt.
+# We make no guarantees that this code is fit for any purpose.
+# Visit http://www.pragmaticprogrammer.com/titles/btlang for more book information.
+#---
+valid([]).
+valid([Head|Tail]) :- valid(Tail), fd_all_different(Head).
+
+sudoku(
+ S11, S12, S13, S14, S15, S16, S17, S18, S19,
+ S21, S22, S23, S24, S25, S26, S27, S28, S29,
+ S31, S32, S33, S34, S35, S36, S37, S38, S39,
+ S41, S42, S43, S44, S45, S46, S47, S48, S49,
+ S51, S52, S53, S54, S55, S56, S57, S58, S59,
+ S61, S62, S63, S64, S65, S66, S67, S68, S69,
+ S71, S72, S73, S74, S75, S76, S77, S78, S79,
+ S81, S82, S83, S84, S85, S86, S87, S88, S89,
+ S91, S92, S93, S94, S95, S96, S97, S98, S99,
+ Board) :-
+
+ Board = [S11, S12, S13, S14, S15, S16, S17, S18, S19,
+ S21, S22, S23, S24, S25, S26, S27, S28, S29,
+ S31, S32, S33, S34, S35, S36, S37, S38, S39,
+ S41, S42, S43, S44, S45, S46, S47, S48, S49,
+ S51, S52, S53, S54, S55, S56, S57, S58, S59,
+ S61, S62, S63, S64, S65, S66, S67, S68, S69,
+ S71, S72, S73, S74, S75, S76, S77, S78, S79,
+ S81, S82, S83, S84, S85, S86, S87, S88, S89,
+ S91, S92, S93, S94, S95, S96, S97, S98, S99],
+
+ fd_domain(Board, 1, 9),
+
+ Row1 = [S11, S12, S13, S14, S15, S16, S17, S18, S19],
+ Row2 = [S21, S22, S23, S24, S25, S26, S27, S28, S29],
+ Row3 = [S31, S32, S33, S34, S35, S36, S37, S38, S39],
+ Row4 = [S41, S42, S43, S44, S45, S46, S47, S48, S49],
+ Row5 = [S51, S52, S53, S54, S55, S56, S57, S58, S59],
+ Row6 = [S61, S62, S63, S64, S65, S66, S67, S68, S69],
+ Row7 = [S71, S72, S73, S74, S75, S76, S77, S78, S79],
+ Row8 = [S81, S82, S83, S84, S85, S86, S87, S88, S89],
+ Row9 = [S91, S92, S93, S94, S95, S96, S97, S98, S99],
+
+ Col1 = [S11, S21, S31, S41, S51, S61, S71, S81, S91],
+ Col2 = [S12, S22, S32, S42, S52, S62, S72, S82, S92],
+ Col3 = [S13, S23, S33, S43, S53, S63, S73, S83, S93],
+ Col4 = [S14, S24, S34, S44, S54, S64, S74, S84, S94],
+ Col5 = [S15, S25, S35, S45, S55, S65, S75, S85, S95],
+ Col6 = [S16, S26, S36, S46, S56, S66, S76, S86, S96],
+ Col7 = [S17, S27, S37, S47, S57, S67, S77, S87, S97],
+ Col8 = [S18, S28, S38, S48, S58, S68, S78, S88, S98],
+ Col9 = [S19, S29, S39, S49, S59, S69, S79, S89, S99],
+
+ Square1 = [S11, S12, S13, S21, S22, S23, S31, S32, S33],
+ Square2 = [S14, S15, S16, S24, S25, S26, S34, S35, S36],
+ Square3 = [S17, S18, S19, S27, S28, S29, S37, S38, S39],
+ Square4 = [S41, S42, S43, S51, S52, S53, S61, S62, S63],
+ Square5 = [S44, S45, S46, S54, S55, S56, S64, S65, S66],
+ Square6 = [S47, S48, S49, S57, S58, S59, S67, S68, S69],
+ Square7 = [S71, S72, S73, S81, S82, S83, S91, S92, S93],
+ Square8 = [S74, S75, S76, S84, S85, S86, S94, S95, S96],
+ Square9 = [S77, S78, S79, S87, S88, S89, S97, S98, S99],
+
+ valid([Row1, Row2, Row3, Row4, Row5, Row6, Row7, Row8, Row9]),
+ valid([Col1, Col2, Col3, Col4, Col5, Col6, Col7, Col8, Col9]),
+ valid([Square1, Square2, Square3,
+ Square4, Square5, Square6,
+ Square7, Square8, Square9]).
View
42 prolog/sudoku4.pl
@@ -0,0 +1,42 @@
+#---
+# Excerpted from "Seven Languages in Seven Weeks",
+# published by The Pragmatic Bookshelf.
+# Copyrights apply to this code. It may not be used to create training material,
+# courses, books, articles, and the like. Contact us if you are in doubt.
+# We make no guarantees that this code is fit for any purpose.
+# Visit http://www.pragmaticprogrammer.com/titles/btlang for more book information.
+#---
+valid([]).
+valid([Head|Tail]) :-
+ fd_all_different(Head),
+ valid(Tail).
+
+sudoku(Puzzle, Solution) :-
+ Solution = Puzzle,
+ Puzzle = [S11, S12, S13, S14,
+ S21, S22, S23, S24,
+ S31, S32, S33, S34,
+ S41, S42, S43, S44],
+
+ fd_domain(Solution, 1, 4),
+
+ Row1 = [S11, S12, S13, S14],
+ Row2 = [S21, S22, S23, S24],
+ Row3 = [S31, S32, S33, S34],
+ Row4 = [S41, S42, S43, S44],
+
+ Col1 = [S11, S21, S31, S41],
+ Col2 = [S12, S22, S32, S42],
+ Col3 = [S13, S23, S33, S43],
+ Col4 = [S14, S24, S34, S44],
+
+ Square1 = [S11, S12, S21, S22],
+ Square2 = [S13, S14, S23, S24],
+ Square3 = [S31, S32, S41, S42],
+ Square4 = [S33, S34, S43, S44],
+
+ valid([Row1, Row2, Row3, Row4,
+ Col1, Col2, Col3, Col4,
+ Square1, Square2, Square3, Square4]).
+
+
View
10 prolog/sudoku4_step_1.pl
@@ -0,0 +1,10 @@
+#---
+# Excerpted from "Seven Languages in Seven Weeks",
+# published by The Pragmatic Bookshelf.
+# Copyrights apply to this code. It may not be used to create training material,
+# courses, books, articles, and the like. Contact us if you are in doubt.
+# We make no guarantees that this code is fit for any purpose.
+# Visit http://www.pragmaticprogrammer.com/titles/btlang for more book information.
+#---
+sudoku(Puzzle, Solution) :-
+ Solution = Puzzle.
View
16 prolog/sudoku4_step_2.pl
@@ -0,0 +1,16 @@
+#---
+# Excerpted from "Seven Languages in Seven Weeks",
+# published by The Pragmatic Bookshelf.
+# Copyrights apply to this code. It may not be used to create training material,
+# courses, books, articles, and the like. Contact us if you are in doubt.
+# We make no guarantees that this code is fit for any purpose.
+# Visit http://www.pragmaticprogrammer.com/titles/btlang for more book information.
+#---
+sudoku(Puzzle, Solution) :-
+ Solution = Puzzle,
+ Puzzle = [S11, S12, S13, S14,
+ S21, S22, S23, S24,
+ S31, S32, S33, S34,
+ S41, S42, S43, S44],
+ fd_domain(Puzzle, 1, 4).
+
View
33 prolog/sudoku4_step_3.pl
@@ -0,0 +1,33 @@
+#---
+# Excerpted from "Seven Languages in Seven Weeks",
+# published by The Pragmatic Bookshelf.
+# Copyrights apply to this code. It may not be used to create training material,
+# courses, books, articles, and the like. Contact us if you are in doubt.
+# We make no guarantees that this code is fit for any purpose.
+# Visit http://www.pragmaticprogrammer.com/titles/btlang for more book information.
+#---
+sudoku(Puzzle, Solution) :-
+ Solution = Puzzle,
+ Puzzle = [S11, S12, S13, S14,
+ S21, S22, S23, S24,
+ S31, S32, S33, S34,
+ S41, S42, S43, S44],
+
+ fd_domain(Puzzle, 1, 4),
+
+ Row1 = [S11, S12, S13, S14],
+ Row2 = [S21, S22, S23, S24],
+ Row3 = [S31, S32, S33, S34],
+ Row4 = [S41, S42, S43, S44],
+
+ Col1 = [S11, S21, S31, S41],
+ Col2 = [S12, S22, S32, S42],
+ Col3 = [S13, S23, S33, S43],
+ Col4 = [S14, S24, S34, S44],
+
+ Square1 = [S11, S12, S21, S22],
+ Square2 = [S13, S14, S23, S24],
+ Square3 = [S31, S32, S41, S42],
+ Square4 = [S33, S34, S43, S44].
+
+
View
56 scala/TicTacToe.scala
@@ -0,0 +1,56 @@
+object TicTacToe {
+ def main(args: Array[String]): Unit = {
+ println("hello world")
+
+ args.foreach {arg =>
+ println(arg)
+ }
+ // rows = args.map
+
+ val ourBoard = new Board(args)
+ }
+
+
+ class Board (rows: Array[String]) {
+ val board = Array (
+ rows(0),rows(1),rows(2),
+ "" + rows(0)(0) + rows(1)(0) +rows(2)(0),
+ "" + rows(0)(1) + rows(1)(1) +rows(2)(1),
+ "" + rows(0)(2) + rows(1)(2) +rows(2)(2),
+
+ "" + rows(0)(2) + rows(1)(1) +rows(2)(0),
+ "" + rows(0)(0) + rows(1)(1) +rows(2)(2))
+
+
+
+
+
+ val wins = board.filter{ s => s(0) == s(1) && s(1) == s(2) && s(0) != ' '}
+
+ if(wins.length > 0)
+ println(wins(0) + " wins!");
+ else {
+
+ val dones = board.filter{ s => s(0) != ' ' && s(1) != ' '&& s(2) != ' '}
+ if(dones.length == board.length) println ("tied")
+ else println("still moves to make")
+ }
+
+
+
+ def status = {
+ "don't know yet"
+ }
+
+
+
+ }
+
+
+}
+
+/*
+ a fairly unsatisfying solution, hindered by syntax and library naivite
+
+*/
+

No commit comments for this range

Something went wrong with that request. Please try again.