tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
58
Task/N-queens-problem/Curry/n-queens-problem-1.curry
Normal file
58
Task/N-queens-problem/Curry/n-queens-problem-1.curry
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
-- 8-queens implementation with the Constrained Constructor pattern
|
||||
-- Sergio Antoy
|
||||
-- Fri Jul 13 07:05:32 PDT 2001
|
||||
|
||||
-- Place 8 queens on a chessboard so that no queen can capture
|
||||
-- (and be captured by) any other queen.
|
||||
|
||||
-- Non-deterministic choice operator
|
||||
|
||||
infixl 0 !
|
||||
X ! _ = X
|
||||
_ ! Y = Y
|
||||
|
||||
-- A solution is represented by a list of integers.
|
||||
-- The i-th integer in the list is the column of the board
|
||||
-- in which the queen in the i-th row is placed.
|
||||
-- Rows and columns are numbered from 1 to 8.
|
||||
-- For example, [4,2,7,3,6,8,5,1] is a solution where the
|
||||
-- the queen in row 1 is in column 4, etc.
|
||||
-- Any solution must be a permutation of [1,2,...,8].
|
||||
|
||||
-- The state of a queen is its position, row and column, on the board.
|
||||
-- Operation column is a particularly simple instance
|
||||
-- of a Constrained Constructor pattern.
|
||||
-- When it is invoked, it produces only valid states.
|
||||
|
||||
column = 1 ! 2 ! 3 ! 4 ! 5 ! 6 ! 7 ! 8
|
||||
|
||||
-- A path of the puzzle is a sequence of successive placements of
|
||||
-- queens on the board. It is not explicitly defined as a type.
|
||||
-- A path is a potential solution in the making.
|
||||
|
||||
-- Constrained Constructor on a path
|
||||
-- Any path must be valid, i.e., any column must be in the range 1..8
|
||||
-- and different from any other column in the path.
|
||||
-- Furthermore, the path must be safe for the queens.
|
||||
-- No queen in a path may capture any other queen in the path.
|
||||
-- Operation makePath add column n to path c or fails.
|
||||
|
||||
makePath c n | valid c && safe c 1 = n:c
|
||||
where valid c | n =:= column = uniq c
|
||||
where uniq [] = True
|
||||
uniq (c:cs) = n /= c && uniq cs
|
||||
safe [] _ = True
|
||||
safe (c:cs) k = abs (n-c) /= k && safe cs (k+1)
|
||||
where abs x = if x < 0 then -x else x
|
||||
|
||||
-- extend the path argument till all the queens are on the board
|
||||
-- see the Incremental Solution pattern
|
||||
|
||||
extend p = if (length p == 8)
|
||||
then p
|
||||
else extend (makePath p x)
|
||||
where x free
|
||||
|
||||
-- solve the puzzle
|
||||
|
||||
main = extend []
|
||||
34
Task/N-queens-problem/Curry/n-queens-problem-2.curry
Normal file
34
Task/N-queens-problem/Curry/n-queens-problem-2.curry
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
-- N-queens puzzle implemented with "Distinct Choices" pattern
|
||||
-- Sergio Antoy
|
||||
-- Tue Sep 4 13:16:20 PDT 2001
|
||||
-- updated: Mon Sep 23 15:22:15 PDT 2002
|
||||
|
||||
import Integer
|
||||
|
||||
queens x | y =:= permute x & void (capture y) = y where y free
|
||||
|
||||
capture y = let l1,l2,l3,y1,y2 free in
|
||||
l1 ++ [y1] ++ l2 ++ [y2] ++ l3 =:= y & abs (y1-y2) =:= length l2 + 1
|
||||
|
||||
-- negation as failure (implemented by encapsulated search):
|
||||
void c = (findall \_->c) =:= []
|
||||
|
||||
-- How does this permutation algorithm work?
|
||||
-- Only the elements [0,1,...,n-1] can be permuted.
|
||||
-- The reason is that each element is used as an index in a list.
|
||||
-- A list, called store, of free variables of length n is created.
|
||||
-- Then, the n iterations described below are executed.
|
||||
-- At the i-th iteration, an element, say s,
|
||||
-- of the initial list is non-deterministically selected.
|
||||
-- This element is used as index in the store.
|
||||
-- The s-th variable of the store is unified with i.
|
||||
-- At the end of the iterations, the elements of the store
|
||||
-- are a permutation of [0,1,...,n-1], i.e., the elements
|
||||
-- are unique since two iterations cannot select the same index.
|
||||
|
||||
permute n = result n
|
||||
where result n = if n==0 then [] else pick n store : result (n-1)
|
||||
pick i store | store !! k =:= i = k where k = range n
|
||||
range n | n > 0 = range (n-1) ! (n-1)
|
||||
store = free
|
||||
-- end
|
||||
59
Task/N-queens-problem/Curry/n-queens-problem-3.curry
Normal file
59
Task/N-queens-problem/Curry/n-queens-problem-3.curry
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
-- 8-queens implementation with both the Constrained Constructor
|
||||
-- and the Fused Generate and Test patterns.
|
||||
-- Sergio Antoy
|
||||
-- Fri Jul 13 07:05:32 PDT 2001
|
||||
|
||||
-- Place 8 queens on a chessboard so that no queen can capture
|
||||
-- (and be captured by) any other queen.
|
||||
|
||||
-- Non-deterministic choice operator
|
||||
|
||||
infixl 0 !
|
||||
X ! _ = X
|
||||
_ ! Y = Y
|
||||
|
||||
-- A solution is represented by a list of integers.
|
||||
-- The i-th integer in the list is the column of the board
|
||||
-- in which the queen in the i-th row is placed.
|
||||
-- Rows and columns are numbered from 1 to 8.
|
||||
-- For example, [4,2,7,3,6,8,5,1] is a solution where the
|
||||
-- the queen in row 1 is in column 4, etc.
|
||||
-- Any solution must be a permutation of [1,2,...,8].
|
||||
|
||||
-- The state of a queen is its position, row and column, on the board.
|
||||
-- Operation column is a particularly simple instance
|
||||
-- of a Constrained Constructor pattern.
|
||||
-- When it is invoked, it produces only valid states.
|
||||
|
||||
column = 1 ! 2 ! 3 ! 4 ! 5 ! 6 ! 7 ! 8
|
||||
|
||||
-- A path of the puzzle is a sequence of successive placements of
|
||||
-- queens on the board. It is not explicitly defined as a type.
|
||||
-- A path is a potential solution in the making.
|
||||
|
||||
-- Constrained Constructor on a path
|
||||
-- Any path must be valid, i.e., any column must be in the range 1..8
|
||||
-- and different from any other column in the path.
|
||||
-- Furthermore, the path must be safe for the queens.
|
||||
-- No queen in a path may capture any other queen in the path.
|
||||
-- Operation makePath add column n to path c or fails.
|
||||
|
||||
makePath c n | valid c && safe c 1 = n:c
|
||||
where valid c | n =:= column = uniq c
|
||||
where uniq [] = True
|
||||
uniq (c:cs) = n /= c && uniq cs
|
||||
safe [] _ = True
|
||||
safe (c:cs) k = abs (n-c) /= k && safe cs (k+1)
|
||||
where abs x = if x < 0 then -x else x
|
||||
|
||||
-- extend the path argument till all the queens are on the board
|
||||
-- see the Incremental Solution pattern
|
||||
|
||||
extend p = if (length p == 8)
|
||||
then p
|
||||
else extend (makePath p x)
|
||||
where x free
|
||||
|
||||
-- solve the puzzle
|
||||
|
||||
main = extend []
|
||||
Loading…
Add table
Add a link
Reference in a new issue