Prolog powerset predicate [closed] - prolog

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).

Related

Mathematic resolution [closed]

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

How puts variable in iterators code Ruby [closed]

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.
How puts variable in iterators code Ruby
EX: (1,2,3).select { |v| puts v > 2}
Seems syntax error, I think you want something like this
[1,2,3].select { |v| puts v > 2 }
output would be :
false
false
true
Take a look here

Scheme: creating a random range [closed]

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.
In scheme I have to use random to define a procedure that accepts no arguments and returns an integer in the range 1 to 10, inclusive and i cant use if. im lost =(
If your Scheme provides a random function, you want either
(define (1-10-rand)
(+ 1 (random 10)))
or
(define (1-10-rand)
(floor (* 10 (random))))
depending on whether you have (random n) --> integer in [0, n-1]) or (random) -> float in [0,1]
Be advised that this isn't standards-compliant. For absolute portability, write your own RNG.

Convert the expression ((A + B) * C – (D – E) ^ (F + G)) to equivalent Prefix and Postfix notations [closed]

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 want to convert the expression ((A + B) * C – (D – E) ^ (F + G)) to equivalent Prefix and Postfix notations. What answers do you get?
Well, there are many steps. Since the question does not reflet your evolution so far, I'll just give you some hints
Define the operations taken in account
Give them a priority
Define what an expression is
What should you do when an expression has parenthesis and when its not
This should get you on track

Prolog challenge [closed]

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".

Resources