Created
March 21, 2013 07:32
-
-
Save draftcode/5211296 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/gprolog --consult-file | |
| fib(1, 1). | |
| fib(2, 1). | |
| fib(N, R) :- N1 is N-1, fib(N1, R1), N2 is N-2, fib(N2, R2), !, R is R1 + R2. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/gprolog --consult-file | |
| likes(wallace, cheese). | |
| likes(grommit, cheese). | |
| likes(wendolene, sheep). | |
| friend(X, Y) :- \+(X == Y), likes(X, Z), likes(Y, Z). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/gprolog --consult-file | |
| hanoi(1, From, _, To) :- | |
| write(From), | |
| write(' -> '), | |
| write(To), | |
| nl. | |
| hanoi(N, From, Via, To) :- | |
| N > 1, | |
| M is N-1, | |
| hanoi(M, From, To, Via), | |
| hanoi(1, From, _, To), | |
| hanoi(M, Via, From, To). | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/gprolog --consult-file | |
| my_minimum([X], X). | |
| my_minimum([X|Xr], X) :- | |
| my_minimum(Xr, Y), | |
| Y >= X. | |
| my_minimum([X|Xr], Y) :- | |
| my_minimum(Xr, Y), | |
| Y < X. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/gprolog --consult-file | |
| valid_queen((_, 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], [Diagonal1|DiagonalsTail]) :- | |
| Diagonal1 is Col - Row, | |
| diags1(QueensTail, DiagonalsTail). | |
| diags2([], []). | |
| diags2([(Row, Col)|QueensTail], [Diagonal2|DiagonalsTail]) :- | |
| Diagonal2 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). | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/gprolog --consult-file | |
| my_reverse([], []). | |
| my_reverse([X], [X]). | |
| my_reverse([X|Xr], Ys) :- | |
| my_reverse(Xr, RevXr), | |
| append(RevXr, [X], Ys). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/gprolog --consult-file | |
| lesser([], _, []). | |
| lesser([X|Xr], Y, [X|Zs]) :- | |
| X < Y, | |
| lesser(Xr, Y, Zs). | |
| lesser([X|Xr], Y, Zs) :- | |
| X >= Y, | |
| lesser(Xr, Y, Zs). | |
| greater([], _, []). | |
| greater([X|Xr], Y, [X|Zs]) :- | |
| X >= Y, | |
| greater(Xr, Y, Zs). | |
| greater([X|Xr], Y, Zs) :- | |
| Y > X, | |
| greater(Xr, Y, Zs). | |
| my_sort([], []). | |
| my_sort([X], [X]). | |
| my_sort([X|Xr], Rs) :- | |
| lesser(Xr, X, Lesser), | |
| greater(Xr, X, Greater), | |
| my_sort(Lesser, SortedLesser), | |
| my_sort(Greater, SortedGreater), | |
| append(SortedLesser, [X], R), | |
| append(R, SortedGreater, Rs). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/gprolog --consult-file | |
| valid([]). | |
| valid([Head|Tail]) :- | |
| fd_all_different(Head), | |
| valid(Tail). | |
| print_puzzle([S11, S12, S13, S14, | |
| S21, S22, S23, S24, | |
| S31, S32, S33, S34, | |
| S41, S42, S43, S44]) :- | |
| print([S11, S12, S13, S14]), | |
| print('\n'), | |
| print([S21, S22, S23, S24]), | |
| print('\n'), | |
| print([S31, S32, S33, S34]), | |
| print('\n'), | |
| print([S41, S42, S43, S44]), | |
| print('\n'). | |
| 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, S33, S34], | |
| Square4 = [S41, S42, S43, S44], | |
| valid([Row1, Row2, Row3, Row4, | |
| Col1, Col2, Col3, Col4, | |
| Square1, Square2, Square3, Square4]), | |
| print_puzzle(Solution). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/gprolog --consult-file | |
| valid([]). | |
| valid([Head|Tail]) :- | |
| fd_all_different(Head), | |
| valid(Tail). | |
| sudoku(Puzzle, Solution) :- | |
| Solution = Puzzle, | |
| Puzzle = [S11, S12, S13, S14, S15, S16, | |
| S21, S22, S23, S24, S25, S26, | |
| S31, S32, S33, S34, S35, S36, | |
| S41, S42, S43, S44, S45, S46, | |
| S51, S52, S53, S54, S55, S56, | |
| S61, S62, S63, S64, S65, S66], | |
| fd_domain(Solution, 1, 6), | |
| Row1 = [S11, S12, S13, S14, S15, S16], | |
| Row2 = [S21, S22, S23, S24, S25, S26], | |
| Row3 = [S31, S32, S33, S34, S35, S36], | |
| Row4 = [S41, S42, S43, S44, S45, S46], | |
| Row5 = [S51, S52, S53, S54, S55, S56], | |
| Row6 = [S61, S62, S63, S64, S65, S66], | |
| Col1 = [S11, S21, S31, S41, S51, S61], | |
| Col2 = [S12, S22, S32, S42, S52, S62], | |
| Col3 = [S13, S23, S33, S43, S53, S63], | |
| Col4 = [S14, S24, S34, S44, S54, S64], | |
| Col5 = [S15, S25, S35, S45, S55, S65], | |
| Col6 = [S16, S26, S36, S46, S56, S66], | |
| Square1 = [S11, S12, S13, S21, S22, S23], | |
| Square2 = [S14, S15, S16, S24, S25, S26], | |
| Square3 = [S31, S32, S33, S41, S42, S43], | |
| Square4 = [S34, S35, S36, S44, S45, S46], | |
| Square5 = [S51, S52, S53, S61, S62, S63], | |
| Square6 = [S54, S55, S56, S64, S65, S66], | |
| valid([Row1, Row2, Row3, Row4, Row5, Row6, | |
| Col1, Col2, Col3, Col4, Col5, Col6, | |
| Square1, Square2, Square3, Square4, Square5, Square6]). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment