logo
down
shadow

Get the length of a list in prolog in a non-recursive way


Get the length of a list in prolog in a non-recursive way

By : Juanith
Date : November 22 2020, 01:01 AM
should help you out I have the following code for getting the length of a list in prolog, it works recursively. Is there any other way for getting the length? , Here is an iterative solution that uses repeat/0 predicate:
code :
getlength(L,N) :-
    retractall(getlength_res(_)),
    assert(getlength_res(0)),
    retractall(getlength_list(_)),
    assert(getlength_list(L)),
    repeat,
        (
            getlength_list([]), !, getlength_res(N)
        ;
            retract(getlength_res(V)), W is V + 1, assert(getlength_res(W)),
            retract(getlength_list([_|T])), assert(getlength_list(T)), fail
        ).


Share : facebook icon twitter icon
Prolog - recursive list building

Prolog - recursive list building


By : pankaj bari
Date : March 29 2020, 07:55 AM
will help you So, there are many things to say here. First and foremost, as in most declarative languages, a variable cannot really change value.
What that means is that X = 1. will unify 1 to X as you'd expect, but if you add X = 2. after that in your query (X = 1, X = 2.), Prolog will say false. The reason behind that is that you cannot unify 1 with 2 and that X has truly become 1, therefore X cannot be unified to 2.
code :
multiply(2, 3, Y),
A = Y ,
add(2, 3, Z),
B = Z,
addToEnd([A], [B], X),
multiply(2, 3, Y),
add(2, 3, Z),
addToEnd([Y], [Z], X),
s1(0, []).
s1(0, Q) :- Q = [].
s1(N, [SomeCalculus|Q]) :-
    PreviousN is N - 1,
    s1(PreviousN, Q).
s1(N, [SomeCalculus|Q]) :-
    PreviousN is N - 1,
    X is 2 * 3,
    Y is 2 + 3,
    SomeCalculus = [X, Y],
    s1(PreviousN, Q).
s1(N, [[X, Y]|Q]) :-
    PreviousN is N - 1,
    X is 2 * 3,
    Y is 2 + 3,
    s1(PreviousN, Q).
s1(0, []).
s1(N, [[X, Y]|Q]) :-
    PreviousN is N - 1,
    X is 2 * 3,
    Y is 2 + 3,
    s1(PreviousN, Q).
s1(0, []).
s1(N, [[X, Y]|Q]) :-
    N > 0,
    PreviousN is N - 1,
    X is 2 * 3,
    Y is 2 + 3,
    s1(PreviousN, Q).
Prolog - Recursive List

Prolog - Recursive List


By : Arpit
Date : March 29 2020, 07:55 AM
To fix this issue Accumulating a list is relatively straightforward: first, observe that if A can call B directly, the list is simply [A, B]. Hence, we can rewrite your first rule as
code :
message_route(A,B,[A,B]) :- can_phone(A,B).
message_route(A,B,[A|Tail]) :- can_phone(A,X), message_route(X,B,Tail).
Prolog recursive list split

Prolog recursive list split


By : Sripada Satpathy
Date : March 29 2020, 07:55 AM
I hope this helps . I'm new to Prolog and I'm having trouble with the first part of my programming assignment: Create a predicate split that that takes as input three parameters. The first and third parameters are lists and the second parameter is an element. You can think of the first parameter as being the input and the last two parameters being the output. The method computes all possible way of splitting a list into an element and the rest of the list. Here is an example run.
code :
split([H|T], H, T).

split([H|T], X, [H|Y]) :-
    split(T, X, Y).
How to insert data into a list in recursive Prolog

How to insert data into a list in recursive Prolog


By : Allen Wo
Date : March 29 2020, 07:55 AM
seems to work fine Trying to figure out how to return a list which contains ancestors of person A until person B. For example, I have the following facts: , If it must hold that
code :
ancestorList( john, helen, L) :- L = [paul, henry], L = [paul | [henry      ] ].
ancestorList( paul, helen, L) :- L = [      henry], L =         [henry | [] ]  .  % and,

ancestorList( henry, helen, L) :- L =                                    []    .
ancestorList( henry, helen, L) :- parent( henry, helen),  L =            []    .
%           Parent, Child, List
ancestorList( Henry, Helen, L) :- parent( Henry, Helen),  L =            []    .

%      Ancestor, Descendant, List
ancestorList( Paul,  Helen, L) :- parent( Paul, Henry),   L =   [ Paul |  T ]  ,
                                  ancestorList( Henry, Helen,             T  ) .
Prolog recursive list of lists

Prolog recursive list of lists


By : Waffle
Date : March 29 2020, 07:55 AM
I hope this helps . So, here is my answer - a related question came up in the meantime.
Before answering your original question, I will look at ascending lists of integers. The reason: It's just simpler to distinguish here between the list and its elements.
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org