% Define a predicate memCount(AList, Blist, Count) that is true if Alist occurs Count times within Blist. Define without using an accumulator. Use "not" as defined in utilities.pro, to make similar cases are unique, or else you may get more than one count as an answer.
% Examples:
% memCount(a.[b,a].N).
% N=1;
% по
% memCount(a.[b, [a,a,[a],c], a],N).
% N=4;
% по
% memCount([a],[b.[a,a,[a],c],a].N).
% N = 1; No

% Custom member predicate to check if an element is in a nested list
member_nested(X, [X|_]).
member_nested(X, [Head|Tail]) :-
    (   is_list(Head),
        member_nested(X, Head)
    ;   member_nested(X, Tail)
    ).

% Predicate memCount(AList, BList, Count)
% True if AList occurs Count times within BList

% Base case: AList does not occur in an empty list
memCount(_, [], 0).

% Recursive case: AList is the head of the list
memCount(AList, [AList|Tail], Count) :-
    memCount(AList, Tail, TailCount),
    Count is TailCount + 1.

% Recursive case: AList is not the head, but is a member of the head if it's a list
memCount(AList, [Head|Tail], Count) :-
    \+ (AList = Head),
    is_list(Head),
    member_nested(AList, Head),
    memCount(AList, Tail, TailCount),
    Count is TailCount + 1.

% Recursive case: AList is not the head and not a member of the head
memCount(AList, [Head|Tail], Count) :-
    \+ (AList = Head),
    (\+ is_list(Head); \+ member_nested(AList, Head)),
    memCount(AList, Tail, Count).

% Initialization and main entry point for testing
:- initialization(main).
main :-
    memCount(a, [b, a], N1),
    write('N1 = '), write(N1), nl,
    memCount(a, [b, [a, a, [a], c], a], N2),
    write('N2 = '), write(N2), nl,
    memCount([a], [b, [a, a, [a], c], a], N3),
    write('N3 = '), write(N3), nl,
    halt. 

Prolog online compiler

Write, Run & Share Prolog code online using OneCompiler’s Prolog online compiler for free. It’s a simple and intuitive platform to experiment with logic programming in Prolog. OneCompiler supports standard Prolog syntax, great for learning, prototyping, and practicing logic-based problems.

About Prolog

Prolog (Programming in Logic) is a logic programming language associated with artificial intelligence and computational linguistics. It works through facts, rules, and queries, using a form of symbolic reasoning known as backward chaining. Prolog is declarative, meaning you describe what you want instead of how to compute it.

Sample Code

The following is a simple Prolog program that prints a greeting:

:- initialization(main).

main :-
    write('Hello, World!').

Syntax Basics

Facts

Facts represent basic assertions about the world.

likes(alice, pizza).
likes(bob, pasta).

Rules

Rules define logical relationships using facts.

friends(X, Y) :- likes(X, Z), likes(Y, Z).

Queries

Queries are used to find information based on facts and rules.

?- likes(alice, What).

Operators

OperatorDescription
:-Rule definition
,Logical AND
;Logical OR
=Unification

Lists

member(X, [X|_]).
member(X, [_|T]) :- member(X, T).

Recursion

Prolog heavily relies on recursion.

factorial(0, 1).
factorial(N, F) :-
  N > 0,
  N1 is N - 1,
  factorial(N1, F1),
  F is N * F1.

This guide provides a quick reference to Prolog programming syntax and features. Start writing Prolog code using OneCompiler’s Prolog online compiler today!