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 PROLOG: Obstetrics, Ninth Edition (Assessment & Critique)

PROLOG: Obstetrics, Ninth Edition (Assessment & Critique)

BUY & SAVE
$200.00
PROLOG: Obstetrics, Ninth Edition (Assessment & Critique)
2 Prolog: The Standard: Reference Manual

Prolog: The Standard: Reference Manual

  • AFFORDABLE PRICES FOR QUALITY READS - GREAT SAVINGS FOR BOOK LOVERS!
  • ECO-FRIENDLY CHOICE: REDUCE WASTE BY BUYING USED, NOT NEW.
  • WIDE SELECTION: FIND HIDDEN GEMS AND RARE TITLES IN GOOD CONDITION!
BUY & SAVE
$73.53 $119.99
Save 39%
Prolog: The Standard: Reference Manual
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 PRICES ON QUALITY USED BOOKS FOR BUDGET-SAVVY READERS.
  • CAREFULLY INSPECTED FOR CONDITION, ENSURING ENJOYABLE READING.
  • ECO-FRIENDLY CHOICE: REDUCE WASTE BY BUYING PRE-LOVED BOOKS.
BUY & SAVE
$66.14 $74.99
Save 12%
Programming in Prolog: Using The Iso Standard
5 Learn Prolog Now! (Texts in Computing, Vol. 7)

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

  • AFFORDABLE PRICING FOR QUALITY READS AT BUDGET-FRIENDLY RATES.
  • ECO-FRIENDLY CHOICE: PROMOTE SUSTAINABILITY WITH PRE-LOVED BOOKS.
  • TRUSTED SELECTION: QUALITY-CHECKED FOR A SATISFYING READING EXPERIENCE.
BUY & SAVE
$22.21 $31.00
Save 28%
Learn Prolog Now! (Texts in Computing, Vol. 7)
6 Adventure in Prolog

Adventure in Prolog

BUY & SAVE
$14.00
Adventure in Prolog
+
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].