User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Legacy and Other Languages section within the Software Development category of DaniWeb, a massive community of 456,234 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,817 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Legacy and Other Languages advertiser: Programming Forums
Views: 2063 | Replies: 15 | Solved
Reply
Join Date: Nov 2007
Posts: 8
Reputation: nanaman is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
nanaman nanaman is offline Offline
Newbie Poster

Scheme Help

  #1  
Nov 14th, 2007
Ok I have to write a program that creats an ADT to store a set of cards that are imput so that i can use the command (a-card rank suit) and (rank card) (suit card) to firstly construct the card and then return its rank and suit. Unfortunately I am struggling alot with this and wondering if someone could prod me in the correct direction.

(define suit (list 'clubs 'diamonds 'hearts 'spades))
(define rank (list 1 2 3 4 5 6 7 8 9 10 11 12 13))
(define (a-card rank suit)
          (list cons rank suit))

any and all prods greatly appreciated, as this is just giving me an error
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2007
Posts: 8
Reputation: nanaman is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
nanaman nanaman is offline Offline
Newbie Poster

Re: Scheme Help

  #2  
Nov 14th, 2007
(define (make-card rank suit) (list rank suit))
(define (a-card rank suit) (make-card rank suit))

Using this I can get (a-card) to return the correct value, how do I then get this value to be passed out of the procedure (i.e. so I can recall it and also use it for (card) ?

Thanks
Reply With Quote  
Join Date: May 2006
Location: Birmingham, AL
Posts: 33
Reputation: azimuth0 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 5
azimuth0 azimuth0 is offline Offline
Light Poster

Re: Scheme Help

  #3  
Nov 14th, 2007
Try defining accessor functions that return particular elements of the list using car and cdr.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Scheme Help

  #4  
Nov 14th, 2007
There are no procedures in scheme, only functions. Whenever you say define, you are naming an expression that evaluates to something.

Hence, follow along:
(a-card 13 'spades) becomes
(make-card 13 'spades) becomes
(list 13 'spades) becomes
'(13 'spades)
The make-card and a-card functions do the same thing: take a rank and suit and return a card. Why not get rid of one of them?

I think you need to re-read your class notes about the difference between a list and a pair.

Personally, I would store the card as a pair:
(define make-card (lambda (rank suit) (cons rank suit)))
Or, using the shortcut syntax:
(define (make-card rank suit) (cons rank suit))

Now, you know that a card is a pair of '(rank . suit), so to get out the rank or suit you only need a new function get knows what a card looks like:
(define (rank card) (car card))
(define (suit card) (cdr card))

The purpose is to make it so that no one knows that a card is just a pair (or a list or something else). You know, because that is how you are storing it. But anyone else using your program only needs to know three functions:
make-card rank suit: return a card object
rank card: return the rank of a card object
suit card: return the suit of a card object

Hope this helps.
Last edited by Duoas : Nov 14th, 2007 at 2:07 pm.
Reply With Quote  
Join Date: Nov 2007
Posts: 8
Reputation: nanaman is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
nanaman nanaman is offline Offline
Newbie Poster

Re: Scheme Help

  #5  
Nov 14th, 2007
That is fantastic, however, the reason I was playing with a list was that I need to store 5 cards (a poker hand), would the best way be to make the pair and then store the pair in a list along with the other 5 then?

thanks again
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Scheme Help

  #6  
Nov 14th, 2007
Yes. A card is a single object. If you want a list of cards, then store a list of cards, not a list of suits and ranks.

(define dead-mans-hand (list
  (make-card 13 'spades)
  (make-card 13 'clubs)
  (make-card 8 'spades)
  (make-card 8 'clubs)
  (make-card 2 'hearts)
  ))
Thereafter, you can get, say, the second card's suit with:
(suit (second dead-mans-hand))

Hope this helps.
Last edited by Duoas : Nov 14th, 2007 at 7:13 pm.
Reply With Quote  
Join Date: Nov 2007
Posts: 8
Reputation: nanaman is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
nanaman nanaman is offline Offline
Newbie Poster

Re: Scheme Help

  #7  
Nov 15th, 2007
(define c1 (a-card 7 'clubs))
(define c2 (a-card 8 'spades))
(define c3 (a-card 9 'hearts))
(define c4 (a-card 10 'clubs))
(define c5 (a-card 11 'diamonds))
(define (a-hand c1 c2 c3 c4 c5) (list c1 c2 c3 c4 c5))

The help so far has been fantastic, I now have the single cards working, and am working on constructing the hand, the a-hand function works fine, however the next thing is to impliment "(contents hand) - returns a list of card objects (as constructed by a-card in the hand."

Now i have no problem doing this as i could just simply put

 
(define (hand c1 c2 c3 c4 c5) (a-hand c1 c2 c3 c4 c5))
(define (contents hand) (list (car hand) (car (cdr hand)) (car (cdr (cdr hand))) (car (cdr (cdr (cdr hand)))) (cdr (cdr (cdr (cdr hand)))))

That the correct way to do it you think?

Thanks again for all the help

*Edit* just tried this way and it is giving me an error of "wrong type in arg1" >.<
*Edit* Just removed the (Define (hand c1 c2 .....) statement and simply type (contents (a-hand c1 c2 c3 c4 c5)) and it works fine

Just wondering though, how can i seperate the two things in a card in a hand? would it be

 (suit (car hand)) etc?
Last edited by nanaman : Nov 15th, 2007 at 7:55 am.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Scheme Help

  #8  
Nov 15th, 2007
Since a "hand" object is a list of cards, and you want to get a list of cards from a hand, all you need to do is return the hand:
(define (contents hand) hand)
Now, if a "hand" object were something else, you'd need to do some work to turn it into a list...


Just wondering though, how can i seperate the two things in a card in a hand? would it be
(suit (car hand))
etc?
Exactly.
Reply With Quote  
Join Date: Nov 2007
Posts: 8
Reputation: nanaman is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
nanaman nanaman is offline Offline
Newbie Poster

Re: Scheme Help

  #9  
Nov 15th, 2007
Is there anyway to sort a list of numbers (and words alphabetically) into order?

thanks
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Scheme Help

  #10  
Nov 16th, 2007
Of course. You'll need to read up on recursion.

There is a basic principle: if you have a list
(a b c d)
(which, if you remember, is really:
(a . (b . (c . (d . ()))))
and if you can do something to the car of the list (or a) and the cdr of the list (that is, the rest of the list: (b c d)),
then you can do something to the whole list.

Remember, your list is a list of 'card's. So you'll have to use the card ADT to get the rank of each card and compare its value.

Hope this gets you started. Using scheme (and other functional languages) require you to think pretty hard.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Legacy and Other Languages Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Legacy and Other Languages Forum

All times are GMT -4. The time now is 5:12 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC