1

Im trying to find the list of all release dates of all intel processors. It should look like this

[1993, 1976, 1974, 1971]

Currently I can only create a list with 1 entry like this

setof(Y,released(pentium,Y),S1) which gives me [1993].

prozessor(i4004).
prozessor(i8080).
prozessor(z80).
prozessor(i8086).
prozessor(a486).
prozessor(pentium).
prozessor(k5).
intel([i4004,i8080,i8086,pentium])
zylog([z80]).
amd([a486,k5]).

released(i4004, 1971).
released(i8080, 1974).
released(z80,1976).
released(i8086,1976).
released(a486,1991).
released(pentium,1993).
released(k5,1996).

influence(i4004,i8080).
influence(i8080,i8086).
influence(i8086,pentium).
influence(i8080,z80).
influence(i8086,a486).
influence(a886,k5).
false
  • 10,264
  • 13
  • 101
  • 209

1 Answers1

2

You can collect the set of all Intel processor release years like this:

setof(Year, intel_release_year(Year), IntelReleaseYears).

For this you must define intel_release_year/1. Given your definitions, this could look like this:

intel_release_year(Year) :-
    intel(IntelProcessors),
    member(Processor, IntelProcessors),
    released(Processor, Year).

Note that facts of the form

somepredicate([a, b, c]).

are not necessarily the best possible style. It is often better to use separate facts for the individual objects, rather than grouping them in a list:

somepredicate(a).
somepredicate(b).
somepredicate(c).

In your example this would be:

intel(i4004).
intel(i8080).
intel(i8086).
intel(pentium).

and then the definition of intel_release_year/1 doesn't need to use member/2 anymore:

intel_release_year(Year) :-
    intel(IntelProcessor),
    released(IntelProcessor, Year).

In either case, you get:

?- setof(Year, intel_release_year(Year), IntelReleaseYears).
IntelReleaseYears = [1971, 1974, 1976, 1993].

(Knowing some commenters here, I expect that some might insist that I mention that there is another, strictly inferior way of using setof/3. So I mentioned it. Do not use setof/3 in that different, strictly inferior way.)

Isabelle Newbie
  • 9,258
  • 1
  • 20
  • 32
  • Thanks, this helped a lot! – Nino Pedergnana Jan 06 '22 at 16:45
  • 1
    could you perhaps clarify what you mean in the last sentence please? – Will Ness Jan 07 '22 at 10:45
  • (I'm asking because I don't understand and I would like to know). – Will Ness Jan 07 '22 at 16:03
  • I was referring to `setof`'s ad hoc "lambda-like" syntax: Using `A^B^C^(Foo, Bar, Baz)` instead of a separate, properly named, properly testable and reusable auxiliary predicate definition. – Isabelle Newbie Jan 08 '22 at 07:52
  • 1
    thanks for the clarification. :) (maybe add this into the answer proper?) of course sometimes the "lambda" is really called for, when it's very simple one-off thing... – Will Ness Jan 08 '22 at 07:53
  • The thing you call "lambda-like syntax" is perfectly fine, especially if you are using the toplevel to query your database. You could of course argue that one should be using findall instead, but sometimes you might want to not bind one of the variables in the goal while still binding the rest. I would avoid such strong language ("strictly inferior" etc) since people might not have enough experience to tell apart opinions from facts. – TA_intern Jan 11 '22 at 08:57