Phil
Phil

Reputation: 1

How do I search through terms within a list in prolog?

I have a list that looks like this [(Tony, 1234), (Bob, 2345), (Alan, 3456)].

I need to create a function that will search through the list by the name and then show the number. So something like: find_user_id(Tony, [(Tony, 1234), (Bob, 2345), (Alan, 3456)], I). I being the value of the ID of the user found.

Upvotes: 0

Views: 39

Answers (1)

CapelliC
CapelliC

Reputation: 60014

use memberchk/2:

?- memberchk((tony,Val), [(tony, 1234), (bob, 2345), (blan, 3456)]).

Upvotes: 1

Related Questions