Reputation: 3137
For example I have the following code, I define a variable v1
then check the value of v1
. If v1 == 1
, I want to (print-list q2)
and read another input and store to v2
, something like this: (define v2 (read))
.
(define v1 (read))
(cond
[(null? v1) (printf "No input..\n")]
[(= v1 1) (print-list q2)]
How do I achieve my solution above?
Upvotes: 0
Views: 1728
Reputation: 24623
One can also use recursion to read repeatedly:
(define (f)
(let loop ((v (read)))
(cond [(= 0 v) "End."]
[(= 1 v) (println '(a b c))]
; [.. other options ..]
[else
(println '(1 2 3))
(loop (read))])))
Testing:
(f)
1
'(a b c)
0
"End."
>
Upvotes: 0
Reputation: 236170
You can write more than one expression after a cond
's condition:
(define v1 (read))
(cond
[(null? v1) (printf "No input..\n")]
[(= v1 1)
(define v2 (read))
(print-list q2)]
[else (error "Unexpected value")])
Of course, the above will only work if print-list
and q2
were previously defined, but it illustrates the general idea of what you want to do. Just remember that although all expressions after the condition will be executed sequentially, only the value of the last expression will be returned, which in this example is (print-list q2)
.
Upvotes: 1