[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: A "killer" macro

William James

12/29/2015 10:10:00 AM

> could fit on one page of Prolog. Better still, when I ran the new
> code against the old code for the same inputs (given 7 cards,
> evaluate, and return the best 5 card hand), it "disagreed" with the
> old code on about 5 hands out of several million randomly generated
> hands, and spit out different solutions for those hands. When I
> analyzed the differences, I found that the Prolog solution was correct
> in those cases. It was due to a few hard-to-find bugs in the old
> solution, which was to be expected simply because that code was much
> longer and more complex. So the new Prolog solution, because of its
> simplicity, was also more correct, and uncovered bugs in the old
> solution.
>
> To illustrate the conciseness of the Prolog solution, here is the code
> to recognize a "4 of a kind":
>
> quad(A,B,C,D,E,F,G) :- permutation([A,B,C,D,E,F,G],[X,X,X,X,_,_,_]).
>
> That's it - one line of code, and the input doesn't even need to be
> sorted beforehand, as the old solution's input did, so I get to remove
> an unnecessary sort.

I don't know Prolog, but it seems that only the ranks of the cards
are being used (not the suits).

MatzLisp (Ruby):

def four? hand
hand.group_by{|x| x}.map{|k,v| v.size}.max > 3
end

[[2,2,2,5,6,7,:a],
[8,8,8,8,8,8,8],
[2,3,2,4,2,2,:k],
[:k,:q,:j,9,9,9,9],
[:a,2,:a,:a,:a,5,8]].each{|hand| printf "%-5s %s\n", four?(hand), hand}

false [2, 2, 2, 5, 6, 7, :a]
true [8, 8, 8, 8, 8, 8, 8]
true [2, 3, 2, 4, 2, 2, :k]
true [:k, :q, :j, 9, 9, 9, 9]
true [:a, 2, :a, :a, :a, 5, 8]

--
Amazon bans book. After nearly a month on the site, all traces of the book and
its 80 reviews have been removed.
http://jamesfetzer.blogspot.com/2015/11/debunking-sandy-hook-debunk...
https://www.youtube.com/watch?v=E...