# Prolog代写｜ECS 140A – Programming Languages Project 3: Prolog

• ALL

## Part 1: Warm Up

You will need to define five predicates in this part.

Write all of them in a single file named “part1.pl”.

1. concat(L1, L2, L): meaning the concatenation of two lists L1 and L2 is L.
Examples:

• concat([1, 2, 3], , [1, 2, 3, 4]) is true.
• concat([1, [2, 3]], , [1, 2, 3, 4]) is false.
• concat([1, [2, 3]], , [1, [2, 3], 4]) is true.

You may assume the given arguments are always lists.

2. element_at(X, N, L): meaning the element X is the Nth element in list L.
Examples:

• element_at(2, 1, [2, 3, 4]) is true.
• element_at(2, 1, [3, 2, 4]) is false.
• element_at(2, 1, [, 3, 4]) is false.
• element_at(5, 2, [3, 2, 4]) is false.

You may assume N ≥ 1 and L is non-empty.

3. my_reverse(L1, L2): meaning the reverse of list L1 is L2.
Examples:

• my_reverse([1,2,3], [3,2,1]) is true.
• my_reverse([1,2,3], [2,1,3]) is false.
• my_reverse([1,[2,3],4,], [,4,[2,3],1]) is true.
• my_reverse([1,[2,3],4,], [,4,[3,2],1]) is false.
• my_reverse([], []) is true.

4. my_flatten(L1, L2): Given a list L1, its flattened version is L2.
Examples:

• my_flatten([1, 2, 3], [1, 2, 3]) is true.
• my_flatten([1, [2, 3]], [1, 2, 3]) is true.
• my_flatten([1, , [3, 4]], [1, 2, 3, 4]) is true.
• my_flatten([1, [2, 3, [4, 5], 6]], [1, 2, 3, 4, 5, 6]) is true.

5. compress(L1, L2): Given a list L1, L2 is its compressed version by eliminating the duplicates.
Examples:

• compress([1, 2, 3], [1, 2, 3]) is true.
• compress([1, 2, 2], [1, 2]) is true.
• compress([1, 2, ], [1, 2]) is false.
• compress([1, 2, [3, 4], , , ], [1, 2, [3, 4], , ]) is true.

## Part 2: N-Queens Problem

Given an N × N chessboard, we want to place N queen on the board so that no two queens can attack
one another. In Chess, a queen can attack another piece vertically, horizontally and diagonally. For more
details, please check https://en.wikipedia.org/wiki/Eight_queens_puzzle. In other words, no two
queens can be placed on the same row, on the same column, or on the same diagnal.

Define a predicate queens(N, Q) where N is the number of rows and columns of the chessboard, and
Q is a list N numbers such that the ith number represents the position of the queen in column i. For
example, Q = [4, 2, 7, 3, 6, 8, 5, 1] means that the queen in the first column is in row 4, the queen in the
second column is in row 2, etc.

Examples:

• It is impossible to place three queens in a 3 × 3 board in such a way.
• Given a 4 × 4 board, there are two ways to place the 4 queens: [2, 4, 1, 3] and [3, 1, 4, 2].
Write your program in a file named “part2.pl”. E-mail: vipdue@outlook.com  微信号:vipnxx 