Skip to main content
freelanceshack.com

Back to all posts

How to Split List In Prolog?

Published on
3 min read
How to Split List In Prolog? image

Best Prolog Books to Buy in October 2025

1 Learn Prolog Now! (Texts in Computing, Vol. 7)

Learn Prolog Now! (Texts in Computing, Vol. 7)

  • AFFORDABLE PRICES ON QUALITY PRE-OWNED BOOKS.
  • THOROUGHLY INSPECTED FOR READABILITY AND QUALITY ASSURANCE.
  • ECO-FRIENDLY CHOICE: PROMOTE SUSTAINABILITY WITH USED BOOKS!
BUY & SAVE
$22.21 $31.00
Save 28%
Learn Prolog Now! (Texts in Computing, Vol. 7)
2 PROLOG: Obstetrics, Ninth Edition (Assessment & Critique)

PROLOG: Obstetrics, Ninth Edition (Assessment & Critique)

BUY & SAVE
$200.00
PROLOG: Obstetrics, Ninth Edition (Assessment & Critique)
3 PROLOG: Patient Management in the Office, Eighth Edition

PROLOG: Patient Management in the Office, Eighth Edition

BUY & SAVE
$190.00
PROLOG: Patient Management in the Office, Eighth Edition
4 Programming in Prolog: Using The Iso Standard

Programming in Prolog: Using The Iso Standard

  • AFFORDABLE PRICING FOR QUALITY USED BOOKS.
  • ECO-FRIENDLY CHOICE: SUSTAINABLE READING OPTIONS.
  • EACH BOOK INSPECTED FOR GOOD CONDITION AND QUALITY.
BUY & SAVE
$71.24 $74.99
Save 5%
Programming in Prolog: Using The Iso Standard
5 Clause and Effect: Prolog Programming for the Working Programmer

Clause and Effect: Prolog Programming for the Working Programmer

  • QUALITY ASSURANCE: EACH BOOK IS INSPECTED FOR GOOD CONDITION.
  • AFFORDABLE PRICES: SAVE MONEY WITHOUT COMPROMISING ON QUALITY.
  • ECO-FRIENDLY CHOICE: SUPPORT SUSTAINABILITY BY BUYING USED BOOKS.
BUY & SAVE
$80.06 $84.99
Save 6%
Clause and Effect: Prolog Programming for the Working Programmer
6 PROLOG: Gynecologic Oncology and Critical Care, Eighth Edition (Assessment & Critique)

PROLOG: Gynecologic Oncology and Critical Care, Eighth Edition (Assessment & Critique)

BUY & SAVE
$170.99 $190.00
Save 10%
PROLOG: Gynecologic Oncology and Critical Care, Eighth Edition (Assessment & Critique)
+
ONE MORE?

In Prolog, you can split a list into two parts by using the built-in predicate append/3 along with the length/2 and append/2 predicates.

First, use the length/2 predicate to determine the length of the first part of the list. Then, use the append/3 predicate to split the list into two parts based on the determined length. Finally, you can use the append/2 predicate to concatenate the two parts back into a single list.

This process effectively splits the original list into two separate lists.

How to split a list into two in Prolog?

To split a list into two in Prolog, you can use the following predicate:

split_list([], [], []). split_list([X], [X], []). split_list([X,Y|T], [X|T1], [Y|T2]) :- split_list(T, T1, T2).

Here is an example of how to use this predicate:

split_list([1,2,3,4,5,6], L1, L2).

This will split the list [1,2,3,4,5,6] into two lists L1 = [1,3,5] and L2 = [2,4,6].

How to extract elements from a list based on a condition in Prolog?

To extract elements from a list based on a condition in Prolog, you can use the built-in predicates such as findall/3 or bagof/3. Here is an example using findall/3:

% Define a predicate to filter elements based on a condition filter_list([], _, []). filter_list([X|Xs], Condition, [X|Ys]) :- call(Condition, X), filter_list(Xs, Condition, Ys). filter_list([_|Xs], Condition, Ys) :- filter_list(Xs, Condition, Ys).

% Example predicate to filter even numbers is_even(X) :- 0 is X mod 2.

% Usage: Filter even numbers from a list filter_even_numbers(InputList, OutputList) :- filter_list(InputList, is_even, OutputList).

% Example query: % ?- filter_even_numbers([1,2,3,4,5,6], Result). % Result = [2, 4, 6].

In this example, filter_list/3 predicate recursively filters elements from the input list based on the specified condition (in this case, the is_even/1 predicate) and returns the output list. Finally, the filter_even_numbers/2 predicate can be used to filter even numbers from a list.

You can modify the condition predicate (is_even/1 in this example) to filter elements based on different conditions.

How to divide a list into alternating elements in Prolog?

You can divide a list into alternating elements in Prolog by writing a predicate that takes a list as input and returns two lists, one containing the elements at even positions and the other containing elements at odd positions. Here is an example predicate that achieves this:

split_alternating([], [], []). split_alternating([X], [X], []). split_alternating([X, Y | Rest], [X | Odds], [Y | Evens]) :- split_alternating(Rest, Odds, Evens).

You can use this predicate by calling split_alternating/3 with a list and two empty lists as arguments. For example:

?- split_alternating([1, 2, 3, 4, 5, 6], Odds, Evens). Odds = [1, 3, 5], Evens = [2, 4, 6].