Re: prolog predicates



repairman2003@xxxxxxxxx writes:

> This should be an easy question.  

Then why isn't it?  

Sorry, just couldn't resist the straight line.

> I'm writing this predicate:
> 
> high_card(Hand, Card) is true iff
>   Hand is a list of length > 0 containing any
>   of the terms: ace, king, queen, jack,
>   and Card is the term in Hand that has the
>   highest value (ace > king > queen > jack).
>   Test cases:
>   ?- high_card([ace], C).
>   C=ace.
>   ?- high_card([ace, jack, jack], C).
>   C=ace.
>   ?- high_card([queen, jack, king, jack], C).
>   C=king.
>   ?- high_card([jack, queen], C).
>   C=queen.

Yes.  To build test cases first is a good approach, and something I
highly recommend.
 
> I've got this so far:
> 
> high_card([],C):-
> 	C=none.
> 
> high_card([ace|Y],C):-
> 	C=ace.
> 
> high_card([X|ace],C):-
> 	C=ace.
> 
> 
> With this I'm not sure how to check for the end of the list for an ace
> and if one exists set C to = ace but if not then C = king:
> 
> high_card([king|Y],C):-
> 	high_card(Y,D),
> 	C=king.

You can approach this in several different ways:

Look for each denomination of card, highest first, stopping when you
find any.  Straightforward with member/2, inefficient.  I'd prefer
this, at least until there's some reason to optimize.

Look for each denomination, lowest first and keep the most recent one
("The painter's algorithm").  Ugly and kind of pointless in Prolog.

Traverse the list once, comparing the tail's best card with the
current card.  You need to either detect the empty list before you
process it or have a dummy value to bind C to in that case.

Traverse the list once, remembering the highest card seen so far, and
bind it to the answer (C) when you see the empty list.  Same as above
with an accumulator - a variable whose only purpose is to pass the
tentative answer down the call tree.

For the last two, I'd suggest you construct a helper predicate that
compares ace, king etc for greaterness, giving the canonical values
">", "=" and "<".


-- 
Tom Breton, the calm-eyed visionary
.



Relevant Pages

  • Re: Simple combinatoric problem
    ... is adjacent to a king? ... You want the probability that at least one ace is adjacent to at least ... card is an ace, and Rthe probability given that the top card is a ... Then add back in all the ways 3 adjacency events can take place, ...
    (sci.math)
  • Re: KR evaluations and....
    ... undervalues jack and overvalues queens. ... a king and between a king and an ace as 1 HCP. ...
    (rec.games.bridge)
  • Re: Simple combinatoric problem
    ... is adjacent to a king? ... You want the probability that at least one ace is adjacent to at least ... card is an ace, and Rthe probability given that the top card is a ...
    (sci.math)
  • Re: Texas Holdem Plus
    ... seems an ace with a bad, unsuited kicker would justify it in late ... extra card more often than it is justified. ... King or Queen ...
    (rec.gambling.poker)
  • Re: [Newbie] Immutable objects
    ... thinking the same might be the case for rank (e.g. "card value" from 2 ... up to ace). ... JACK = card "jack", 11 ...
    (comp.lang.ruby)