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)
Themake-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 apair 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.
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
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.
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
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. :)
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
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.
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
Just as a random addendum. Above I said you could make your ADT as:
(define (rank card) (car card))
That's fine, but if you really want to wow, forget the fluff and just say: (define rank car)
Heh heh heh...
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
There are no procedures in scheme, only functions.
There are nofunctions in Scheme, only procedures. That is, the word used in the R5RS specification (and probably R6RS, but I haven't read that) is "procedure". That's why there is a procedure named procedure? in Scheme, and no procedure named function? .
However, your help to the OP is stellar.
Rashakil Fol
Super Senior Demiposter
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 176
Thanks. Please keep in mind that the OP (and most other readers here) haven't read (nor will they) the R5RS.
The R5RS committee needed a word for a technical thing. They chose poorly (though, considering the circumstances and the technical issues, I don't blame them).
A "procedure" is a very broad term. In most computing contexts that I am aware of, procedures return value by way of side-effect only, whereas functions explicitly return value.
In Scheme, a combination (a lambda), is a function call: local values are bound to an expression which is evaluated for value. That value then replaces the local context. Please keep in mind that this is a functional operation --hence the term functional languages.
Please understand that I'm not being rude. You are very correct. However, you must be careful not to confuse those reading here with those who care about all the technicalities and terminologies specific to functional languages and Scheme in particular.
To be short: in Scheme it is meaningless to talk about the difference between a procedure and a function. But in the minds of programmers everywhere, unfamiliar with Scheme, the difference is in "how do I then get this value to be passed out of the procedure". The answer is, there are no procedures in scheme (or any other functional language). Only functions (or lambdas) that evaluate to (or "return") a new value. You pass a value out by evaluating to that value.
I hope I've explained my choice in language to everyone's satisfaction. If you are a Scheme junkie, know that I am too (it is my all-time favorite functional language, and I've used it in professional contexts). I know as well as you that in Scheme combinations are called "procedures". I don't take this as an excuse to use terms that confuse rather than help the poor saps struggling to figure this stuff out. By the time they get to the point where they read the R5RS about procedure?, they will already know enough to bridge the gap.
Hope this helps.
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
In Scheme, a combination (a lambda), is a function call: local values are bound to an expression which is evaluated for value. That value then replaces the local context. Please keep in mind that this is a functional operation --hence the term functional languages.
Well, yeah. I agree with calling functions (when used functionally in Scheme) functions. And calling procedures (when used procedurally, with side effects) procedures.
Of course, Scheme isn't really a functional language. :-) troll>
I don't understand why you have "(a lambda)" there, saying that a 'lambda (expression?)' is a synonym for the term 'combination', though.
Rashakil Fol
Super Senior Demiposter
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 176
I think that's actually part of the reason they called them procedures, because Scheme allows side effects...
The use of the word "combination" is another one of those Scheme goodies... But in an evaluative/transformational way of thinking it makes sense... (as Scheme is all about list transformation).
Are you in to program transformation too? (All the Haskell junkies I know are into that.)
They all worry about me because I like my side effects being transparent. I don't want to have to dink with monads for every function that uses a function that may have side effects... That's what compilers are for IMO. :S :)
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229