I want to be able to do this. For example, this is my code:
(cond [true AA]
[else BB])
In AA, I want it to do 2 things. 1 is to set the value of a global variable, and then return a string. How would I go about doing that?
In the cond special form, there's an implicit begin after each condition, so it's ok to write several expressions, remembering that only the value of the last one will be returned. Like this:
(cond [<first condition>
(set! global-variable value)
"string to return"]
[else
"other return value"])
This sort of thing can be easily found in the Scheme R5RS specification. It really is worth a read as it is one of the finest language specifications ever written!
Other syntactic keywords that you might find of use:
(if <predicate> <consequent> <alternate>)
(begin <expression-or-declaration> ... <expression>)
(case <expression> <case-clause> ...)
(and <test> ...)
... others ...
create a helper function that does 2 things. have your if statement call said function when appropriate:
(cond [(test? something) (your-function your-data)]
[else (another-function your-data)])
A less straightforward way would be to call two functions and "link" them with an "and":
(cond [(test? something)
(and (do-this data) (do-that data))]
[else (do-something-else data)])
begin, it's withif(another 5% would be when writing macros). Plus, Racket favorscond(ormatch) overif, so if you follow that you'll rarely usebegin. It's good to understandbegin, but it's one of those things you'll learn early on that you'll use surprisingly little in most practical code. (Much like you learn to write loops as recursive functions, and it's important to learn that, and sometimes you'll truly need that, but mostly you'll actually use things likemaporfor/list.) - Greg Hendershott