So, I'm very new to lisp and really don't have much idea what to do! I come from a java background so think in "things" but I don't want to write java in Lisp.

How do I go about making a deck of cards in lisp?
Thanks!
In a room of N CL programmers, I would expect to get O(N*(N+1)/2) approaches.titanium_geek wrote:How do I go about making a deck of cards in lisp?
Code: Select all
(defun make-deck ()
(let ((deck (make-array '(52)))
(index 0))
(dolist (suite '(:spades :hearts :diamonds :clubs))
(loop for number from 2 upto 10
do (setf (aref deck index) (cons number suite)
index (1+ index)))
(dolist (face '(:jack :queen :king :ace))
(setf (aref deck index) (cons face suite)
index (1+ index))))
deck))
Excellent!titanium_geek wrote:I don't want to write java in Lisp.
Ramarren's question is probably the most apt: what do you want to do with the deck? This isn't a facetious question; it's useful for guiding you to the right implementation. At least, it'll guide you to a good starting implementation; one of the nice things about CL is how easy it is to re-prototype things.titanium_geek wrote:How do I go about making a deck of cards in lisp?
Fast hacking? Sounds funnuntius wrote:Code: Select all
(defun make-deck () (let ((deck (make-array '(52))) (index 0)) (dolist (suite '(:spades :hearts :diamonds :clubs)) (loop for number from 2 upto 10 do (setf (aref deck index) (cons number suite) index (1+ index))) (dolist (face '(:jack :queen :king :ace)) (setf (aref deck index) (cons face suite) index (1+ index)))) deck))
Code: Select all
(defun shuffle (deck)
(sort (copy-sequence 'vector deck)
#'(lambda (x y)
(declare (ignore x y))
(zerop (random 2)))))
Code: Select all
cl-user> (make-deck)
#((2 . :spades) (3 . :spades) (4 . :spades) (5 . :spades) (6 . :spades)
(7 . :spades) (8 . :spades) (9 . :spades) (10 . :spades)
(:jack . :spades) (:queen . :spades) (:king . :spades) (:ace . :spades)
(2 . :hearts) (3 . :hearts) (4 . :hearts) (5 . :hearts) (6 . :hearts)
(7 . :hearts) (8 . :hearts) (9 . :hearts) (10 . :hearts)
(:jack . :hearts) (:queen . :hearts) (:king . :hearts) (:ace . :hearts)
(2 . :diamonds) (3 . :diamonds) (4 . :diamonds) (5 . :diamonds)
(6 . :diamonds) (7 . :diamonds) (8 . :diamonds) (9 . :diamonds)
(10 . :diamonds) (:jack . :diamonds) (:queen . :diamonds)
(:king . :diamonds) (:ace . :diamonds) (2 . :clubs) (3 . :clubs)
(4 . :clubs) (5 . :clubs) (6 . :clubs) (7 . :clubs) (8 . :clubs)
(9 . :clubs) (10 . :clubs) (:jack . :clubs) (:queen . :clubs)
(:king . :clubs) (:ace . :clubs))
cl-user> (shuffle *)
#((7 . :hearts) (6 . :spades) (8 . :diamonds) (5 . :clubs) (9 . :diamonds)
(10 . :spades) (10 . :diamonds) (4 . :diamonds) (9 . :spades)
(8 . :hearts) (:king . :clubs) (:ace . :diamonds) (5 . :diamonds)
(9 . :hearts) (2 . :clubs) (4 . :clubs) (6 . :hearts) (2 . :hearts)
(10 . :hearts) (:king . :hearts) (5 . :hearts) (10 . :clubs)
(:queen . :hearts) (7 . :diamonds) (:ace . :clubs) (8 . :clubs)
(3 . :diamonds) (3 . :clubs) (:king . :diamonds) (4 . :hearts)
(:queen . :diamonds) (4 . :spades) (3 . :hearts) (:queen . :clubs)
(:jack . :diamonds) (3 . :spades) (6 . :diamonds) (2 . :spades)
(:queen . :spades) (7 . :spades) (:ace . :spades) (:jack . :spades)
(6 . :clubs) (7 . :clubs) (:jack . :hearts) (5 . :spades) (8 . :spades)
(:jack . :clubs) (9 . :clubs) (:king . :spades) (:ace . :hearts)
(2 . :diamonds))
cl-user>
Probably a mistake in Java too.titanium_geek wrote:I guess I'm thinking too real world (like java).
That is what I did aboveDestruct1 wrote:Here is a basic framework (/ snippet of code):
http://rosettacode.org/wiki/Playing_cards#Common_Lisp
What I find a neat trick for shuffling is using a sort applicative operator with
a random function as key.