It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I have the below implementation for the sublist algorithm.
Problem: Given 2 lists, determine whether one is a sublist of the other.
I would really need another distinct solution in Prolog.
Solution one:
sublist([H1|T1], L, [H2|T2]):-
H1 = H2,
sublist(T1, L, T2).
sublist([], _, _)
sublist([H1|T1],L,[H2|T2]):-
sublist(L,L,T2).
Solution two:
sublist([H|T], [H|L]):- check(T,L),
sublist(S, [H|T]):- sublist(S,T).
check([H|T], [H|R]):-
check(T,R).
check([],_).
Solution three:
sublist(S,L):-
append(_,R,L),
append(S,_,R).
Solution three':
sublist(S,L):-
append3(_,S,_,L).
?- phrase((...,seq(Sublist),...),List).
with:
... --> [] | [_], ... .
seq([]) --> [].
seq([E|Es]) --> [E], seq(Es).
(Warning: In order to be able to explain this solution, you need to understand DCGs first!)
sublist([], _).
sublist([H|T], List) :-
select(H, List, R),!,
sublist(T, R).
You could make it more efficient if the lists were ordered to begin with.
Depending on your dialect of Prolog, select/3 may have a different name.
Caveat: as per false's comment, this is rather "subset" than "sublist".
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I just heard something very insteresting but cannot find any ressources.
The story is that when faced with an algorithmic problem that requiered multiple ifs, one student of a friend did answer with a mathematic one liner.
Now I already knew that you could do anyting with math but I want to be able to do it.
From what I know it might be possible to do a loop given the shape of those function, but conditions?
Does someone know how to resolve something like:
IF boolean
THEN expression
ELSE expression2
in math terms (without Bool algebra)?
Best regards,
Sarfraz
Assuming boolean is either 0 or 1, and expressions are mathematical:
expr = boolean * expression + (1-boolean) * expression2
Provided that boolean is 0 or 1:
result = boolean*expression+(1-boolean)*expression2
Are you referring to a multiplexer?
If the boolean is S, expression is A and expression2 is B, then the formula for result Z is
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
Here is the Question following:
For this question we consider binary expression-trees whose leaves are either of the form tree(empty, Num, empty) where Num is a number, or tree(empty, z, empty) in which case we will think of the letter z as a kind of "variable".
Every tree is either a leaf or of the form tree(L, Op, R) where L and R are the left and right subtrees, and Op is one of the arithmetic operators '+', '-', '*', '/' (signifying addition, subtraction, multiplication and division).
Write a predicate tree_eval(Value, Tree, Eval) that binds Eval to the result of evaluating the expression-tree Tree, with the variable z set equal to the specified Value. For example:
?- tree_eval(2, tree(tree(empty,z,empty),
'+',tree(tree(empty,1,empty),
'/',tree(empty,z,empty))), Eval).
Eval = 2.5 ;
false.
?- tree_eval(5, tree(tree(empty,z,empty),
'+',tree(tree(empty,1,empty),
'/',tree(empty,z,empty))), Eval).
Eval = 5.2 ;
false.
Some good ideas?
Could we achieve it without using cut(!)?
Thanks guys!
It is a shame you wouldn't even try to solve it before asking for help.
Your question almost directly translates to a solution. When there is a Num in the middle of the tree:
tree_eval(_Value, tree(empty,Num,empty), Num).
When there is a variable:
tree_eval(Value, tree(empty,z,empty), Value).
And the general case:
tree_eval(Value, tree(tree(LL,LOp,LR),Op,tree(RL,ROp,RR)), Eval) :-
tree_eval(Value, tree(LL,LOp,LR), LEval),
tree_eval(Value, tree(RL,ROp,RR), REval),
Expr =.. [Op,LEval,REval], % is there an easier way to do this?
Eval is Expr.
Now as you notice, this solution has no cuts. They are not necessary, because at a time, only one of the three clauses can be true. For one of the clauses, however, I couldn't come up with a way to make the head unambiguous. This might be of help.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I would like to create a sorting function based on user input.
Assume that you have:
Ordered integer list or vector, X, from 1 to 10.
Function F
Parameter t
Output list Y
What I would to do 'is a consistent function between
F(X, t) = Y
I mean for same t, it must provide same Y.
Any idea?
Seed your random generator with t and shuffle the list.
I guarantee there is a better answer than this, and I admit I don't quite understand #GeorgScholly's answer, but maybe the function you're looking for is a polynomial, which can be obtained via interpolation.
For example, say your vector X has two integers, x0, x1, and so your output vector Y must have two integers, y0, y1. There is a line y=ax+b that fits any two points (x0, y0) and (x1, y1).
In general, n points, (x0, y0) ... (xn-1, yn-1), can always be perfectly fit by a polynomial of degree n-1, i.e. a function, y = an-1xn-1 + an-2xn-2 + ... + a2x2 + a1x + a0.
These coefficients, an-1 ... a0, can be found by many methods of "polynomial interpolation." Effectively, the coefficients in vector form would define your function, F(x).
Finally, to incorporate a parameter t, you might simply add am "unnecessary" term, i.e. match two points with a parabola (not a line), match three points with a cubic (not a parabola), etc., in order to have an additional an to use as a fixed point, t.
I'll be very surprised if there isn't a much simpler way (if #GoergScholly's answer isn't already one).
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Could you help me regarding the next three Prolog programs?
Summary of the elements in a list, and check that is divided or not divided with 3?
For example, the list is [1, 2, 3] --> and the sum of the element is divided with 3, because 1+2+3=6, and 6/3=2 --> so the output should be true.
If the 7 is in a list, doubles it. For example: the input list --> [1,7,3,7,7], the outputs should be [1,7,7,3,7,7,7,7].
If the 7 is in a list, change it for 2,7,2. For example: the input list -->[1,7,2,1], the output should be [1,2,7,2,2,1]
What is the program and how to test it with SWI-Prolog?
Thank you in anticipation!
I will give you a couple of tips:
You need a) to calculate the sum, b) check whether it divides by 3. If you use SWI-Prolog there is a predicate sum_list in the library lists that does a) and the ... is ... mod ... construction to solve b) If you need to use recursion rather than the built-in predicate to calculate the sum:
sum([X|Xs], Acc, Sum) :- Acc1 is Acc + X, sum(Xs, Acc1, Sum).
sum([], Acc, Acc).
sum(List, Sum) :- sum(List, 0, Sum).
and 3. These are recursive procedures. You should traverse the list and if 7 is encountered you should replace it with 7,7 for question 2 and with 2,7,2 for question 3.
traverse_list([],[]).
traverse_list([7|Xs], [7,7|Ps]) :-
!,
traverse_list(Xs,Ps).
traverse_list([X|Xs], [X|Ps]) :-
traverse_list(Xs,Ps).
Think about modifying this fragment for 3.
I'd recommend giving it a little thought and figuring it out on your own. That said, I'll give you something to help get started:
1) I'm assuming your instructor wants to see you do the recursion rather than use the built-in sum_list predicate. That would look something like this:
sum([FirstNum | Rest], Sum) :-
sum(Rest, Sum1),
Sum is FirstNum + Sum1.
Then use the "mod" operator to check divisibility. Remember to include a base case for when the recursion reaches the empty set []. If you can understand this, the rest should be easy. Good luck.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I wish to define a predicate powerset(X, P) which is true when P is the powerset of X. Should work whether or not P is ground.
Since you use SICStus Prolog you can use the subseq0(+Sequence, ?SubSequence) from library(lists), which "is true when SubSequence is a subsequence of Sequence, but may be Sequence itself" (Quoting from the manual http://www.sics.se/sicstus/docs/4.0.2/html/sicstus/lib_002dlists.html).
?- setof(X, subseq0([a,b,c],X), Xs).
Xs = [[],[a],[a,b],[a,b,c],[a,c],[b],[b,c],[c]]
If you are not allowed to use library predicates you can implement the subseteq0 as explained in gnu Prolog powerset modification, which I quote here for the sake of completeness (with thanks to gusbro)
powerset([], []).
powerset([H|T], P) :- powerset(T,P).
powerset([H|T], [H|P]) :- powerset(T,P).