If you're trying to perform multiple actions with side-effects in one arm of the if
, then you'll need to put them into a begin
, like this:
(if (char=? (string-ref str loc) #\a)
(begin (set! count (+ count 1))
(set! reference (+ reference 1)))
~else action, etc.~
If, instead of causing changes in variables, you want to return two values at once, then you'll either need to combine the expressions into a single object, like this:
(if (char=? (string-ref str loc) #\a)
(cons (+ count 1) (+ reference 1)))
~else expression~
in which case to pull out the count and reference, you'll need to apply car
and cdr
to the result of the if
—or you could actually return multiple values, like this:
(if (char=? (string-ref str loc) #\a)
(values (+ count 1) (+ reference 1)))
~else expression~
in which case to pull out the count and reference, you'll need to bind the multiple values to variables somehow in the code that calls the if
. One way to do that is with let-values
, possibly something like this:
(define count&ref
(λ (str ch)
(let loop ([loc 0] [count 0] [reference 0])
; whatever other stuff you're doing
(if (char=? (string-ref str loc) ch)
(values (+ count 1) (+ reference 1)))
~else expression~ )))
(let-values ([(new-count new-ref) (count&ref "some stuff" #\u)])
;in here, new-count and new-ref are bound to whatever count&ref returned
)
On the other hand, if count
and reference
are variables that you're just keeping track of within a loop, the simplest way might be to call the next iteration of the loop inside the if
, like this:
(let loop ([loc 0] [count 0] [reference 0])
; whatever other stuff you're doing
(if (char=? (string-ref str loc) #\a)
(loop (+ loc 1) (+ count 1) (+ reference 1))
~else expression~ ))