I wanted to create a "function" that "returns" the even elements of a list :
elements_pairs([], []).
elements_pairs([H|T], R) :- H mod 2 =:= 0, elements_pairs(T,Rn), R is [H|Rn].
elements_pairs([H|T], R) :- not(H mod 2 =:= 0), elements_pairs(T,Rn), R is Rn.
This elements_pairs([1,3,4,6], R) produces this error:
ERROR: Type error: [] expected, found [4|6] (a compound) ("x" must hold one character)
ERROR: In: ERROR: [11] _3884 is [4|6] ERROR: [10]
elements_pairs([4,6],_3918) at path/tp.pl:21 ERROR: [9]
elements_pairs([3,4|...],_3956) at path/tp.pl:21 ERROR: [8]
elements_pairs([1,3|...],_3994) at path/tp.pl:21 ERROR: [7]
What's wrong ?
Thank you
Related
CHR: How do you call Prolog code inside a rule ?
I assume it is like DCG !!! but doesn't seem right
:- use_module(library(chr)).
:- chr_constraint move/2,pos/2, next/0.
:- [utils].
main :- pos(5,5).
rand(X,Y) :- random(0,2,X), random(0,2,Y), say("~w,~w",[X,Y]).
pos(X,Y), move(A,B) <=> pos(6,6).%X1 > 0, X1 < 10, Y1 > 0, Y1 < 10 | pos(X1,Y1), {X1 is X + A, Y1 is Y + B}.
next <=> move(X,Y), {rand(X,Y)}. <<<-----
error :
?- pos(5,5),next.
ERROR: Unknown procedure: {}/1
ERROR: In:
ERROR: [13] {rand(_35671298,_35671300)}
ERROR: [12] next___0__0(suspension(470241,removed,_35671332,0,user: ...,next)) at grid.pl:12
ERROR: [9] <user>
ERROR:
ERROR: Note: some frames are missing due to last-call optimization.
ERROR: Re-run your program in debug mode (:- debug.) to get more detail.
Exception: (13) {rand(_35670676, _35670678)} ? abort
% Execution Aborted
======================
this works, but have to set the position LAST !!! and cant use the GUARD !
:- use_module(library(chr)).
:- chr_constraint move/2,pos/2, next/0.
:- [utils].
rand(X,Y) :- random(0,2,X), random(0,2,Y), say("~w,~w",[X,Y]).
next <=> move(X,Y), rand(X,Y).
move(A,B),pos(X,Y) <=> X1 is X + A, Y1 is Y + B , X1 > 0, X1 < 10, Y1 > 0, Y1 < 10, pos(X1,Y1).
first generate the moves !! weird ... any time I put next after pos() it fails..
?- next,next,next,pos(5,5).
1,0
1,1
0,0
pos(7, 6).
Seems like I got it ..!! the prolog calls have to be first in this case rand() i.e.
next <=> rand(X,Y), move(X,Y).
except the guard, still cant use the updated values ... probably cause they are calculated afterwards.
I am very new to Prolog. I need to find all the paths from Source to Destination in a maze.
I wrote the following clause so that I do not have to write all the edge relations explicitly:
neighbour(X,Y) :-
( X =:= Y+1
; Y =:= X+1
; X =:= Y+6
; Y =:= X+6
).
Next, for finding path I do the following:
path(Source,Source,_).
path(Source,Destination,PathList) :-
neighbour(Source,Z), % find a neighbour of Source
not(member(Z,PathList)), % that is not already in the list
% and see if there is a path from Z to Destination
path(Z,Destination,[Source|PathList]).
While running it I queried the following:
?- path(1,2,[]). % "Is there a path from 1 to 2?"
This gives me the following error:
ERROR: Arguments are not sufficiently instantiated
ERROR: In:
ERROR: [10] 1=:=_26424+1
ERROR: [9] neighbour(1,_26452) at /Users/sujitkumar/Desktop/this sem/PL/2.pl:3
ERROR: [8] path(1,2,[]) at /Users/sujitkumar/Desktop/this sem/PL/2.pl:30
ERROR: [7] <user>
I do not understand why the expression is not getting evaluated.
I have question with single quote atom.
While p == 'p' returns true, why is p =\= 'p' not returning false? it gives me an error saying 'p' is not a function.
Because == checks term equality, while =\= checks arithmetical inequality. Being as p is not a number, Prolog treats it as an arithmetic expression, trying to find out its arithmetic value:
1 ?- p == 'p'.
true.
2 ?- p =/= 'p'.
ERROR: Syntax error: Operator expected
ERROR: p
ERROR: ** here **
ERROR: =/= 'p' .
2 ?- p =\= 'p'.
ERROR: =\=/2: Arithmetic: `p/0' is not a function
3 ?-
I made the following program, which is this:
eval([],_,_).
eval([(U, V)| Tail], X, Y):-
Y + evaluate([Tail], X, Y), Y is U * (X ** V).
it returns false, and I dont know why. How can I fix it?
So this eval([(4,3), 4, X) should return 256.
and eval([(4,3),(1,0)], 4, X). should return 257.
Now I get this error"
ERROR: Undefined procedure: (+)/2
ERROR: In:
ERROR: [9] _5562+eval([...],4,_5572)
ERROR: [8] eval([(4,3),...],4,_5606) at c:/users/parya lotfi/desktop/exe2.pl:2
ERROR: [7] <user>
eval([], _,0).
eval([(U,V)|UVs], X, Y0) :-
eval(UVs, X, Y1),
Y0 is Y1 + U*X^V.
?- eval([(4,3),(1,0)], 4, X).
X = 257.
I have this fact at PROLOG (swi)
pos(X,[K|T],Z) :- findall(X, (member(X, [K|T])), Z) .
and then I write this:
pos (5 , [1,4,5] , Z ).
and I get the following error.
ERROR: Syntax error: Operator expected
ERROR: pos
ERROR: ** here **
ERROR: (5 , [1,4,5] , Z ) .
I am new to prolog so I cannot understand what the interpreter tells me about and secondly what I am doing wrong
Thx in advance.